gpt4 book ai didi

python - 如何增加 matplotlib 条形图中错误线的粗细?

转载 作者:太空狗 更新时间:2023-10-29 22:12:32 27 4
gpt4 key购买 nike

请引用最小工作示例here .

条形图绘制如下所示,但我找不到增加错误线粗细的方法。 elinewidth 选项在 ax.bar

中不可用
> rects1 = ax.bar(..., elinewidth=3)

AttributeError: Unknown property elinewidth

enter image description here

以下链接需要使用ax.errorbar()

但是有没有可以直接提供给 ax.bar() 的选项?

最佳答案

您可以按如下方式使用error_kw参数:

error_kw=dict(lw=5, capsize=5, capthick=3)

因此在示例中,它将是:

import numpy as np
import matplotlib.pyplot as plt

N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)

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

fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std, error_kw=dict(lw=5, capsize=5, capthick=3))

women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std, error_kw=dict(lw=5, capsize=5, capthick=3))

# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))

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


def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width() / 2,
1.05 * height,
f'{height:.0f}',
ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

给你:

bar with thick err


对此的改进是在每个误差条上方显示值。这可以通过首先获取误差条高度来完成,如下所示:

import numpy as np
import matplotlib.pyplot as plt

N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)

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

fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std, error_kw=dict(lw=5, capsize=5, capthick=3))

women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std, error_kw=dict(lw=5, capsize=5, capthick=3))

# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))

ax.set_ylim(0, 45) # Add space for errorbar height
ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))


def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""

data_line, capline, barlinecols = rects.errorbar

for err_segment, rect in zip(barlinecols[0].get_segments(), rects):
height = err_segment[1][1] # Use height of error bar

ax.text(rect.get_x() + rect.get_width() / 2,
1.05 * height,
f'{height:.0f}',
ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

给你:

Text above error barrs

注意:需要计算 y 轴限制。

关于python - 如何增加 matplotlib 条形图中错误线的粗细?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42619012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com