python修改误差棒的样式_用 matplotlib 绘制误差棒图(Errorbar)

本文介绍如何使用Python的matplotlib库绘制误差棒图,并分别展示标准偏差、标准误和置信区间的误差棒。通过示例代码展示了如何生成随机数据,计算平均值、标准差、标准误和95%置信区间,以及如何在图表中显示这些信息。

标准偏差(Standard Deviation)、标准误(Standard Error)、置信区间(Confidence Intervals)

import numpy as np

from matplotlib import pyplot as plt

from scipy.stats import t

# 在5-15范围内生成15个随机数据点

X = np.random.randint(5, 15, 15)

# 样本大小

n = X.size

# 平均

X_mean = np.mean(X)

# standard deviation

X_std = np.std(X)

# standard error

X_se = X_std / np.sqrt(n)

# alternatively:

# from scipy import stats

# stats.sem(X)

# 95% Confidence Interval

dof = n - 1 # degrees of freedom

alpha = 1.0 - 0.95

conf_interval = t.ppf(1-alpha/2., dof) * X_std*np.sqrt(1.+1./n)

fig = plt.gca()

plt.errorbar(1, X_mean, yerr=X_std, fmt='-o')

plt.errorbar(2, X_mean, yerr=X_se, fmt='-o')

plt.errorbar(3, X_mean, yerr=conf_interval, fmt='-o')

plt.xlim([0,4])

plt.ylim(X_mean-conf_interval-2, X_mean+conf_interval+2)

# axis formatting

fig.axes.get_xaxis().set_visible(False)

fig.spines["top"].set_visible(False)

fig.spines["right"].set_visible(False)

plt.tick_params(axis="both", which="both", bottom="off", top="off",

labelbottom="on", left="on", right="off", labelleft="on")

plt.legend(['Standard Deviation', 'Standard Error', 'Confidence Interval'],

loc='upper left',

numpoints=1,

fancybox=True)

plt.ylabel('random variable')

plt.title('15 random values in the range 5-15')

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值