Posts: 327
Threads: 82
Joined: Apr 2019
Hi,
I'm still trying to figure out on how to get the percentage on the y axis instead of the number of occurences; i'm not familier with histograms in matplotlib, but for sure i'm missing something!
Thanks for your advices
# range [0, 5[ = 5 => 71.4 %
# range [5, 10[ = 1 => 14.3%
# range [10, 15[ = 0 => 0%
# range [15, 20[ = 1 => 14.3%
data = np.asarray([1, 1, 5, 3, 4, 16, 2])
interval = [0, 5, 10, 15, 20]
hx, hy, tmp = plt.hist(data, bins = interval, color="red", linestyle='-', linewidth=1, align = 'mid')
# hx = count
# hy = interval
summ = np.sum(hx)
percentage = (np.asarray(hx) / summ)
# plt.gca().yaxis.set_major_formatter(PercentFormatter(xmax = 100))
plt.grid()
plt.savefig("tmp.png", bbox_inches='tight')
plt.show()![]() ![[Image: tmp.png]](https://i.postimg.cc/5y04yFkJ/tmp.png) ';" src=" ![[Image: tmp.png]](https://i.postimg.cc/5y04yFkJ/tmp.png) " alt="İmage" id="maximage" title="Click Photo To Enlarge">
Posts: 327
Threads: 82
Joined: Apr 2019
Mar-22-2026, 05:54 PM
(This post was last modified: Mar-22-2026, 08:32 PM by paul18fr.)
Hi,
I'm still trying to figure out on how to get the percentage on the y axis instead of the number of occurences; i'm not familier with histograms in matplotlib, but for sure i'm missing something!
(I'll also need to rework the grid size :-)
Thanks for your advices
# range [0, 5[ = 5 => 71.4 %
# range [5, 10[ = 1 => 14.3%
# range [10, 15[ = 0 => 0%
# range [15, 20[ = 1 => 14.3%
data = np.asarray([1, 1, 5, 3, 4, 16, 2])
interval = [0, 5, 10, 15, 20]
hx, hy, tmp = plt.hist(data, bins = interval, color="red", linestyle='-', linewidth=1, align = 'mid')
# hx = count
# hy = interval
summ = np.sum(hx)
percentage = (np.asarray(hx) / summ)
# plt.gca().yaxis.set_major_formatter(PercentFormatter(xmax = 100))
plt.grid()
plt.savefig("tmp.png", bbox_inches='tight')
plt.show()
Posts: 327
Threads: 82
Joined: Apr 2019
ok i got it
# range [0, 5[ = 5 => 71.4 %
# range [5, 10[ = 1 => 14.3%
# range [10, 15[ = 0 => 0%
# range [15, 20[ = 1 => 14.3%
data = np.asarray([1, 1, 5, 3, 4, 16, 2])
interval = [0, 5, 10, 15, 20]
weights2apply = (np.ones_like(data) / float(len(data))) * 100
hx, hy, tmp = plt.hist(data, bins = interval, weights = weights2apply, color="red", linestyle='-', linewidth=1, align = 'mid')
plt.title('histogramme with percentages')
plt.xticks(interval)
plt.gca().yaxis.set_major_formatter(PercentFormatter(xmax = 100))
plt.grid()
plt.savefig("tmp.png", bbox_inches='tight')
plt.show()
Posts: 12,137
Threads: 496
Joined: Sep 2016
Answered by XGrok:
Here's the corrected and improved version of your code.
You probably meant to show the **y-axis** as percentages (which is very common for histograms — "relative frequency" or "percentage").
Showing the **x-axis** as a percentage usually doesn't make sense here because the x-axis represents your data values (from 0 to 20).
### Corrected code (y-axis as percentage):
import numpy as np
import matplotlib.pyplot as plt
data = np.asarray([1, 1, 5, 3, 4, 16, 2])
bins = [0, 5, 10, 15, 20]
fig, ax = plt.subplots(figsize=(8, 5))
# Create histogram
n, bin_edges, patches = ax.hist(data, bins=bins, color="red",
edgecolor='black', linewidth=1, align='mid')
# Convert counts to percentages
total = n.sum()
percentages = (n / total) * 100
# Set y-axis to percentage
ax.set_ylabel('Percentage (%)')
ax.set_yticks(np.linspace(0, 100, 6)) # nice ticks: 0, 20, 40, 60, 80, 100
# Optional: add percentage labels on top of each bar
for i, (count, pct) in enumerate(zip(n, percentages)):
if count > 0: # only show label if bar exists
ax.text(bin_edges[i] + (bin_edges[i+1] - bin_edges[i])/2, count + 0.5,
f'{pct:.1f}%', ha='center', va='bottom', fontsize=10)
ax.set_xlabel('Value')
ax.set_title('Histogram with Percentage on Y-axis')
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("histogram_percentage.png", bbox_inches='tight', dpi=200)
plt.show()### If you really wanted the x-axis in percentage (e.g. cumulative percentage or normalized bins):
Let me know your exact goal and I can adjust it.
But in 95% of cases when people say "show as percentage" with a histogram, they actually want the **heights** (y-axis) to be percentages, which is what the code above does.
Would you like a version with both axes labeled nicely, or a cumulative percentage histogram? Just say the word!
|