简体   繁体   English

为什么不能在Pandas系列生成的绘图上设置y轴范围?

[英]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. 我正在尝试使用matplotlib和pandas创建条形图,其中y轴的范围为0%-100%。 The range I get is only 0% - 50%. 我得到的范围仅为0%-50%。 Now, since all of my bars top out at ~10%, this isn't disastrous. 现在,由于我所有的酒吧都达到了10%的最高点,所以这并不是灾难性的。 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: 奇怪的是,当我省略set_yticklabels语句时,我得到了:

在此处输入图片说明

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. 我猜错误是在set_yticklabels语句中,但我不知道它可能是什么。

Looks like the keyword ylim works fine for pandas.DataFrame.plot.bar() : 看起来keyword ylim适用于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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