0

I graphed 2 histograms on a single plot, how do i get the specific values for each bar? i need to figure out the specific numbers and compare them

x = ['positive','negative','positve','positve','positve','positve','negative']
y = ['negative','positive','positve','negative','positve','positve','positive']

values, bins, patches = plt.hist([x, y], bins=np.arange(3)-0.5, label=['Retold Stories', 'Recalled Stories'],density=True)

plt.legend(loc='upper right')
plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
plt.show()

UPDATE: How do I make the the values on top of the bar into percentages instead of the decimals?

fig, ax = plt.subplots()
x = ['positive','negative','positve','positve','positve','positve','negative']
y = ['negative','positive','positve','negative','positve','positve','positive']

values, bins, patches = plt.hist([x, y], bins=np.arange(3)-0.5, label=['Retold Stories', 'Recalled Stories'],density=True)

plt.legend(loc='upper right')
plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
for container in ax.containers:
ax.bar_label(container)
plt.show()

enter image description here

mango
  • 3
  • 3
  • 1
    Please see [ask], which specifically says to avoid screenshots of code/data/errors, then [edit] your question, thanks! – BigBen Mar 03 '22 at 20:12
  • Maybe you should be using `numpy.histogram`. – BigBen Mar 03 '22 at 20:13
  • 1
    Right now you're throwing away the values with `_, _, patches = ...` so just store them as `values, bins, patches = ...` – tdy Mar 03 '22 at 20:22
  • Related: [How to reuse the return values of plt.hist](https://stackoverflow.com/a/70949207/13138364) – tdy Mar 03 '22 at 20:24
  • But in your edit, you've already changed it to `values, bins, patches = ...` which gives you the values, so what is your question exactly? – tdy Mar 03 '22 at 20:43
  • Yeah to clarify, I wanted to get the exact values of each bin in percentage! I just figured out how to get the values on top of the bar, but it gives me the decimal values rather than the percentage – mango Mar 03 '22 at 20:51
  • 1
    Ah ok, use the `labels` param (of `ax.bar_label`) with percentage f-strings, e.g. https://stackoverflow.com/a/71014041/13138364 – tdy Mar 03 '22 at 20:52

0 Answers0