matplotlib 如何更改误差线符号的下限

v440hwme  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(159)

我有一个分组条形图,我只想更改误差线下限的标记。这是我目前使用的一个示例。

import matplotlib.pyplot as plt
import numpy as np

sic2_AGB = np.array([3.7e-7, 1.1e-5, 1.9e-6, 1.0e-5, 9.7e-6, 4.0e-6,
        6.8e-6, 6.6e-6, 1.0e-6, 1.3e-5, 6.0e-6, 3.7e-5, 1.7e-6, 4.2e-6, 
        2.7e-6, 1.4e-5, 1.0e-5, 8.8e-7, 3.6e-6, 2.0e-5, 1.6e-6, 6.0e-7, 
        2.7e-5, 3.0e-6, 4.0e-6])
sic2_g0693 = 7.9e-11

# Calculate the average
sic2_AGB_mean = np.mean(sic2_AGB)

labels = ['SiC$_{2}$']
data_AGB_crich =[sic2_AGB_mean]

x = np.arange(len(labels)) 
width = 0.15 

y_errmin = data_AGB_crich - np.array([3.7e-7])
y_errmax = np.array([3.7e-5]) - data_AGB_crich
y_error = [y_errmin, y_errmax]

fig, ax = plt.subplots(figsize=(5,4))
ax.bar(x - 0.15, sic2_g0693, width, label='G+0.693', color = 'sandybrown')

ax.bar(x, data_AGB_crich, width, label='C-rich AGB', color = 'crimson')
ax.errorbar(x, data_AGB_crich, yerr=y_error, fmt='o', linewidth=0.5, capsize=2, 
            ecolor = 'black', marker='None')

plt.xticks(x, labels)
plt.xlabel('Molecules', fontsize=9, fontweight='bold')
plt.yscale('log', nonposy='clip')
plt.ylabel('Abundance with respect to H$_2$', fontsize=9, fontweight='bold')
plt.tick_params(axis='x', labelrotation=0, labelsize= 8)
plt.tick_params(axis="y",direction="in")
ax.legend(loc=(0.2, 0.8), prop={'size': 6})

plt.tight_layout()
plt.savefig('sic2.jpg',bbox_inches='tight', dpi=300)
plt.show()

添加

uplims=True

到ax.errorbar(),改变了errorbar值的范围,这不是我想要的,我想要类似于下图的东西。

1l5u6lss

1l5u6lss1#

您可以绘制不带上限的误差条(capsize=0),并使用散点图添加所需的标记:

import matplotlib.pyplot as plt
import numpy as np

sic2_AGB = np.array([3.7e-7, 1.1e-5, 1.9e-6, 1.0e-5, 9.7e-6, 4.0e-6, 6.8e-6, 6.6e-6, 1.0e-6, 1.3e-5, 6.0e-6, 3.7e-5, 1.7e-6, 4.2e-6, 2.7e-6, 1.4e-5, 1.0e-5, 8.8e-7, 3.6e-6, 2.0e-5, 1.6e-6, 6.0e-7, 2.7e-5, 3.0e-6, 4.0e-6])
sic2_g0693 = 7.9e-11

# Calculate the average
sic2_AGB_mean = np.mean(sic2_AGB)

labels = ['SiC$_{2}$']
data_AGB_crich =[sic2_AGB_mean]

x = np.arange(len(labels))
width = 0.15

y_errmin = data_AGB_crich - np.array([3.7e-7])
y_errmax = np.array([3.7e-5]) - data_AGB_crich
y_error = [y_errmin, y_errmax]

fig, ax = plt.subplots(figsize=(5, 4))
ax.bar(x - 0.15, sic2_g0693, width, label='G+0.693', color='sandybrown')

ax.bar(x, data_AGB_crich, width, label='C-rich AGB', color='crimson')
ax.errorbar(x, data_AGB_crich, yerr=y_error, linewidth=0.5, capsize=0,
            ecolor='black', fmt='None')
ax.scatter(x, data_AGB_crich + y_errmax, marker='_', color='black')
ax.scatter(x, data_AGB_crich - y_errmin, marker='v', color='black')

ax.set_xticks(x, labels)
ax.set_xlabel('Molecules', fontsize=9, fontweight='bold')
ax.set_yscale('log')
ax.set_ylabel('Abundance with respect to H$_2$', fontsize=9, fontweight='bold')
ax.tick_params(axis='x', labelrotation=0, labelsize=8)
ax.tick_params(axis="y", direction="in")
ax.legend(loc=(0.2, 0.8), prop={'size': 6})

plt.tight_layout()
plt.savefig('sic2.jpg', bbox_inches='tight', dpi=300)
plt.show()

相关问题