首页 > 解决方案 > 如何将条形图的下限误差限制为 0?

问题描述

我计算了 rttMeans 和 rttStds 数组。但是,rttStds 的值使得下限误差小于 0。

rttStds = [3.330311915835426, 3.3189677330174883, 3.3319538853150386, 3.325173772304221, 3.3374145232695813]

如何将较低的错误设置为 0 而不是 -#?

python条形图代码如下。

在此处输入图像描述

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(rc={'figure.figsize':(18,16)},style='ticks',font_scale = 1.5,font='serif')

N = 5
ind = ['RSU1', 'RSU2', 'RSU3', 'RSU4', 'RSU5']   # the x locations for the groups
width = 0.4       # the width of the bars: can also be len(x) sequence

fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(111)

p1 = plt.bar(ind, rttMeans, width, yerr=rttStds, log=False, capsize = 16, color='green', hatch="/", error_kw=dict(elinewidth=3,ecolor='black'))
plt.margins(0.01, 0)

#Optional code - Make plot look nicer
plt.xticks(rotation=0)
i=0.18
for row in rttMeans:
    plt.text(i, row, "{0:.1f}".format(row), color='black', ha="center")
    i = i + 1

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
params = {'axes.titlesize':24,
          'axes.labelsize':24,
          'xtick.labelsize':28,
          'ytick.labelsize':28,
          'legend.fontsize': 24,
          'axes.spines.right':False,
          'axes.spines.top':False}
plt.rcParams.update(params)

plt.tick_params(axis="y", labelsize=28, labelrotation=20, labelcolor="black")
plt.tick_params(axis="x", labelsize=28, labelrotation=20, labelcolor="black")

plt.ylabel('RT Time (millisecond)', fontsize=24)
plt.title('# Participating RSUs', fontsize=24)


# plt.savefig('RSUs.pdf', bbox_inches='tight')
plt.show()

标签: pythonmatplotlibbar-chart

解决方案


yerr您可以作为一对传递[lower_errors, upper_errors],您可以控制lower_errors

lowers = np.minimum(rttStds,rttMeans)
p1 = plt.bar(ind, rttMeans, width, yerr=[lowers,rttStds], log=False, capsize = 16, color='green', hatch="/", error_kw=dict(elinewidth=3,ecolor='black'))

输出:

在此处输入图像描述


推荐阅读