简体   繁体   English

具有分类X轴的Matplotlib功能区图(fill_between)

[英]Matplotlib Ribbon Plot (fill_between) with a Categorical X-Axis

I have the mean and standard deviations of a group and I want to create a ribbon plot with the mean and shadowed by the standard deviation, however I have categorical x_axis columns. 我有一个组的均值和标准差,我想创建一个均值并由标准差遮盖的功能区图 ,但是我有x_axis分类列。 There appears to be an autosorting function that throws everything off. 似乎有一个自动排序功能,可以将所有内容丢弃。

## Environmental Variables
x_axis = ['Hematite','Illmenite','Goethite','Magnetite','Quartz','Gibbsite','1:1 Clay','Maghemite','Carbonate Minerals','Plagioclase','2:1 Clays','Dolomite','Pyroxene','Calcite','Olivine','Glass']

negative_means = numpy.array([0.14335727,0.05707763,-0.25710322,-0.31085691,0.45552229,0.0092398,0.33358032,-0.31261271,-0.34325373,-0.32826959,-0.22494553,-0.13867505,0.42883104,0.52948655, -0.13867505,0.52948655])

negative_std = numpy.array([0.9218578779912541,1.0417523903222377,0.7225001946958459,0.6634664468936872,1.8400873597252276,1.2279905419059247,1.3735242455660657,0.721879847038041,0.5543207488394122,0.7817647212788771,0.0,0.0,2.088217480513372,2.2160413265187904,0.0,2.216041326518791])


fig, ax = plt.subplots(1)

ax.plot(negative_means, lw=2, label='mean population 1', color='blue')
plt.fill_between(x_axis, negative_means+negative_std, negative_means-negative_std, facecolor='blue', alpha=0.5)

## Set x labels
ax.set_xticklabels(x_axis)
x_max = int(max(plt.xticks()[0]))
# manually set  xtick labels
plt.xticks(range(0, x_max + 1), x_axis, rotation=45) 

The resulting plot is shown below: 结果图如下所示: 在此处输入图片说明

How can I formulate my plot to appear similar to the question below with a categorical x_axis. 如何将我的绘图表述为类似于x_axis的以下问题。

Plot yerr/xerr as shaded region rather than error bars 将yerr / xerr绘制为阴影区域而不是误差线

When I add x_axis to the ax.plot function the resulting graph is produced: 当我将x_axis添加到ax.plot函数中时,将生成结果图:

在此处输入图片说明

I just changed the x parameter for the fill_between using a range which specifies the x-values for your categorical variables. 我只是使用为您的分类变量指定x值的range来更改fill_betweenx参数。 Rest everything was fine in your code. 休息一切都很好在您的代码。 As per the official docs here , the x value should be the cx-oordinates defining your curves. 根据此处的官方文档, x值应为定义曲线的cx x You can stick to not passing the x_axis while plotting which is not the best way as per @ImportanceOfBeingErnest's comment above 您可以坚持在绘图时不要通过x_axis ,这并不是按照x_axis的上述评论的最佳方法

plt.fill_between(range(len(x_axis)), negative_means+negative_std, negative_means-negative_std, facecolor='blue', alpha=0.3)
plt.tight_layout()

Output 产量

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM