matplotlib:每个x轴刻度线的分组误差线 [英] matplotlib: grouping error bars for each x-axes tick
问题描述
我正在尝试使用matplotlib绘制误差线,但要求略有不同.所以,设置如下:
I am trying to use matplotlib to plot error bars but have a slightly different requirements. So, the setup is as follows:
我有 3 种不同的方法,我正在比较 10 种不同的参数设置.因此,在 y 轴上,我有 3 种方法给出的模型拟合误差,在 x 轴上,我有不同的参数设置.
I have 3 different methods that I am comparing across 10 different parameter setting. So, on the y-axes I have the model fitting errors as given by the 3 methods and on the x-axes, I have the different parameter settings.
因此,对于每个参数设置,我想获得与这三种方法相对应的3个误差线图.理想情况下,我想为每个参数设置绘制 95% 置信区间以及每种方法的最小值和最大值.
So, for each parameter setting, I would like to get 3 error bar plots corresponding to the three methods. Ideally, I would like to plot the 95% confidence interval and also the minimum and maximum for each method for each parameter setting.
一些示例数据可以模拟为:
Some example data can be simulated as:
parameters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
mean_1 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_1 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]
mean_2 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_1 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]
mean_3 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_3 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]
我将这些值保持不变,因为从绘图的角度来看,它没有任何改变.我看到了 matplotlib.errorbar 方法,但我不知道如何像在我的情况下那样在一个 x 轴值上为多种方法扩展它.另外,我不确定如何为每种方法添加 [min,max]
标记.
I have kept the values same as it does not change anything from the plotting point of view. I see matplotlib.errorbar method but I do not know how to extend it for multiple methods over one single x-axes value as I have in my case. Additionally, I am not sure how to add the [min, max]
markers for each of the methods.
推荐答案
将您的参数
列表作为x轴,将 mean_1
作为y值并将 std_1
作为错误,您可以使用
Taking your parameters
list as x axis, mean_1
as y value and std_1
as errors you can plot an errorbar chart with
pylab.errorbar(parameters, mean_1, yerr=std_1, fmt='bo')
如果误差条不对称,即您有 lower_err
和 upper_err
,则语句读取
In case the error bars are not symmetric, i.e. you have lower_err
and upper_err
, the statement reads
pylab.errorbar(parameters, mean_1, yerr=[lower_err, upper_err], fmt='bo')
对于x方向错误,关键字 xerr
也可以使用,现在希望可以不言自明.要显示几个(在您的情况下为3个)不同的数据集,您可以采用以下方式:
The same works with keyword xerr
for errors in x direction, which is now hopefully self-explanatory.
To show several (in your case 3) different datasets, you can go the following way:
# import pylab and numpy
import numpy as np
import pylab as pl
# define datasets
parameters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
mean_1 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_1 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]
mean_2 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_2 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]
mean_3 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_3 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]
# here comes the plotting;
# to achieve a grouping, two things are extra here:
# 1. Don't use line plot but circular markers and different marker color
# 2. slightly displace the datasets in x direction to avoid overlap
# and create visual grouping
pl.errorbar(np.array(parameters)-0.01, mean_1, yerr=std_1, fmt='bo')
pl.errorbar(parameters, mean_2, yerr=std_2, fmt='go')
pl.errorbar(np.array(parameters)+0.01, mean_3, yerr=std_3, fmt='ro')
pl.show()
这是关于 pylab.errorbar,您必须在其中明确给出错误.另一种方法是使用 pylab.boxplot 并为每个模型制作一个箱形图,但是因此,我想我需要每个模型每个参数的完整分布,而不仅仅是均值和标准差.
This is about pylab.errorbar, where you have to give the errors explicitly. An alternative approach is to use pylab.boxplot and to prodice a boxplot for each model, but therefore I guess I'll need the full distribution per model per parameter instead of just mean and std.
这篇关于matplotlib:每个x轴刻度线的分组误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!