简体   繁体   中英

Why can't I set the y-axis range on a plot produced from a Pandas Series?

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:

值范围0-10 值范围为0-100

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM