I'm trying to create a bar graph where the y-axis ranges from 0% - 100% using matplotlib and pandas. The range I get is only 0% - 50%. Now, since all of my bars top out at ~10%, this isn't disastrous. It's just frustrating and may interfere with comparisons to other plots with the complete range.
The code I'm using is (roughly) as follows:
from matplotlib import pyplot as plt
import pandas as pd
labels = list(cm.index) #Where cm is a DataFrame
for curr in sorted(labels):
xa = cm[curr] # Pulls 1 column out of DataFrame to be plotted
xplt = xa.plot(kind='bar', rot = 0, ylim = (0,1))
xplt.set_yticklabels(['{:3.0f}%'.format(x*10) for x in range(11)])
plt.show()
Is there anything obviously wrong or missing?
A sample of a plot I get is this:
Oddly, when I omit the set_yticklabels statement, I get this:
I now realize that the first graph is not just oddly scaled, but is also giving incorrect results. The values shown in the 2nd graph are the correct ones. I guess the error is in the set_yticklabels statement, but I have no idea what it could be.
Looks like the keyword
ylim
works fine for pandas.DataFrame.plot.bar()
:
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(10, 2)), columns=['low', 'high'])
df.high = df.high * 10
low high
0 3 10
1 2 0
2 7 20
3 3 90
4 7 60
5 0 40
6 1 0
7 3 70
8 1 80
9 6 90
for col in df:
df[col].plot.bar(ylim=(0, 100))
gives:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.