简体   繁体   English

如何设置y轴的范围?

[英]How do i set up the range for y axis?

Im having i bit of a hard time figuring out how to plot this graph correctly, so what im doing is:我很难弄清楚如何正确地 plot 这个图表,所以我在做的是:

    names = ['Graves', 'Fallecidos', 'Moderados', 'Asintomaticos', 'Leves']
    values = [str(df_2035_Gra), str(df_2035_fal), str(df_2035_Mod), str(df_2035_Asin), str(df_2035_leve)]
    #Values: 69, 85, 876, 3593, 27572
    
    plt.figure(figsize=(9, 3))
    plt.bar(names, values)
    plt.suptitle('Pacientes x estado')
    plt.ylabel('Num. pacientes')
    plt.show()

And what im getting is:我得到的是:

在此处输入图像描述

So what i dont get is how to get the range on Y to be from 0 to my higher value (27572)所以我不明白的是如何让 Y 的范围从 0 到我的更高值(27572)

Ivan:)伊万:)

You can use plt.ylim(0, 27572)您可以使用plt.ylim(0, 27572)

You can check the documentation here您可以在此处查看文档

I hope it helps!我希望它有帮助!

The order matters.顺序很重要。 Here这里

plt.ylim(0, 27572)
plt.figure(figsize=(9, 3))
plt.bar(names, values)
plt.suptitle('Pacientes x estado')
plt.ylabel('Num. pacientes')
plt.show()

You put plt.ylim(0, 27572) before creating the figure with plt.figure .您在使用plt.figure创建图形之前放置plt.ylim(0, 27572) Try this:尝试这个:

plt.figure(figsize=(8, 8))
plt.bar(names, values)
plt.suptitle('Pacientes x estado')
plt.ylabel('Num. pacientes')
plt.ylim(0, 27572)
plt.yticks(np.append(np.arange(0, 25001, step = 5000), 27572))
plt.show()

I added plt.yticks to get a better range.我添加了plt.yticks以获得更好的范围。

You can also try this:你也可以试试这个:

figure, ax = plt.subplots(figsize=(8, 8))
bar_plot = ax.bar(names, values)
plt.suptitle('Pacientes x estado')
plt.ylabel('Num. pacientes')
plt.ylim(0, 27572)
plt.yticks(np.arange(0, 30001, step = 5000))

for idx, rect in enumerate(bar_plot):
    height = rect.get_height() + 0.3
    x_coord = rect.get_x() + rect.get_width()/2
    ax.text(x_coord, 1.02*height,
                values[idx],
                ha='center', va='bottom')


plt.show()

Would look like this:看起来像这样:

image图片

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

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