python - Matplotlib Pyplot 条形图中的 float 条形

标签 python matplotlib

我正在处理一个范围非常大的图表,我发现条形图偏离了轴。我怎样才能阻止这个?这是我在教程中编写的一个示例,因为我的数据是通过读取大量文件获取的。这说明了我遇到的问题。我给出了标准差,意味着一个很大的范围,就像我的数据一样。

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27,43,444,44)
menStd =   (2, 3, 4, 1, 2,43,4,33)

ind = np.arange(8)  # the x locations for the groups
width = 0.35       # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 25,322,3,443,3)
womenStd =   (3, 5, 2, 3, 3,32,33,322)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)

# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5','K','K','Z') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

最佳答案

只需向 y 轴添加限制即可:

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27,43,444,44)
menStd =   (2, 3, 4, 1, 2,43,4,33)

ind = np.arange(8)  # the x locations for the groups
width = 0.35       # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 25,322,3,443,3)
womenStd =   (3, 5, 2, 3, 3,32,33,322)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)

# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5','K','K','Z') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
ax.set_ylim(0)

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

ax.set_ylim(0) 行就可以解决问题。

关于python - Matplotlib Pyplot 条形图中的 float 条形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10351763/

相关文章:

python:如何使用 doc _id 检查 Elasticsearch 中是否存在文档

python - 我可以更新文本文件的一部分吗?我已经建立了一个登录系统,我想计算一个用户登录了多少次

matplotlib - 用 Matplotlib 绘制 map ?

python-3.x - 如何在 matplotlib 中创建具有垂直阴影的绘图

python-3.x - 当有多个同名字体时,按家族名称设置 matplotlib 字体

python - django 1.6.5 密码重置电子邮件检查有效链接

python - 如何在 Python 中将这些字符串解析为前缀表示法的元组?

python - 如何在另一个 python 图中添加不同的图(作为插图)

python - 使用 `statsmodels.api.qqplot()` 创建多个子图的最有效方法是什么?

python - 属性错误: module 'tox.config' has no attribute 'parseini'