繁体   English   中英

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

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

我正在尝试使用matplotlib和pandas创建条形图,其中y轴的范围为0%-100%。 我得到的范围仅为0%-50%。 现在,由于我所有的酒吧都达到了10%的最高点,所以这并不是灾难性的。 这只是令人沮丧,并且可能会干扰与完整范围内其他图的比较。

我使用的代码大致如下:

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()

有什么明显的错误或遗漏吗?


我得到的情节样本是这样的:

在此处输入图片说明

奇怪的是,当我省略set_yticklabels语句时,我得到了:

在此处输入图片说明

我现在意识到,第一个图形不仅按比例缩放,而且给出的结果也不正确。 第二张图中显示的值是正确的值。 我猜错误是在set_yticklabels语句中,但我不知道它可能是什么。

看起来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))

给出:

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

暂无
暂无

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

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