使用python和seaborn在errorbar上的上下行

2024-04-20 04:12:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python和seaborn绘制错误条,但我对它们的外观并不完全满意。

默认的seaborn错误栏如下所示:

errobar default style

但我希望在错误栏上添加这样的底行和顶行(为了区分这两个错误栏,这是默认的matplotlib样式):

defaultmatplolib

我怎么能在西伯恩做到这一点?

代码如下:

import matplotlib.pyplot as plt
import seaborn as sns

fig1 = plt.figure(figsize=(20, 12))


x_values = [1,2,3,4]
y_values = [1,2,3,4]

y_error = [1,0.5,0.75,0.25]

plt.errorbar(x_values, y_values,  yerr=y_error ,fmt='o', markersize=8)

plt.show()

Tags: 代码importmatplotlibas错误绘制plt样式
2条回答
import matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.rcParams.update({'lines.markeredgewidth': 1})

加上第三行就解决了。

检查其他rcParams设置可能也很有趣:在python安装的子目录中查找所需的stylelib/style.mplstyle文件。对于我的系统,它位于“/usr/lib/python3.7/site packages/matplotlib/mpl data/stylelib/”中。seaborn的markeredgewidth默认为0。

参数capsize应该足够了,但是由于某些原因,您必须指定cap.set_markeredgewidth以便它们也显示出来。。基于:Matplotlib Errorbar Caps Missing

(_, caps, _) = plt.errorbar(
    x_values, y_values, yerr=y_error, fmt='o', markersize=8, capsize=20)

for cap in caps:
    cap.set_markeredgewidth(1)

返回:

enter image description here

相关问题 更多 >