python - 带有分类 X 轴的 Matplotlib 带状图 (fill_ Between)

标签 python matplotlib plot graph linegraph

我有一个组的平均值和标准差,我想创建一个 ribbon plot平均值和标准差的阴影,但是我有分类 x_axis 列。似乎有一个自动排序功能会抛弃一切。

## 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) 

结果图如下所示: enter image description here

如何用分类 x_axis 来制定我的绘图,使其看起来与下面的问题类似。

Plot yerr/xerr as shaded region rather than error bars

当我将 x_axis 添加到 ax.plot 函数时,会生成结果图:

enter image description here

最佳答案

我刚刚使用 range 更改了 fill_ Betweenx 参数,该范围指定了分类变量的 x 值。其余的代码中一切都很好。根据官方文档herex 值应该是定义曲线的 cx 坐标。您可以坚持在绘图时不传递 x_axis,这不是上面 @ImportanceOfBeingErnest 的评论的最佳方式

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

输出

enter image description here

关于python - 带有分类 X 轴的 Matplotlib 带状图 (fill_ Between),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52391671/

相关文章:

python - 使用标签在数据透视表中绘图

r - 将线图例添加到 geom_sf

r - 如何在 R 中保持分面图中各个图的大小

python - Python 中的双重约束循环

python - Sphinx/Pygments 中有没有一种方法可以在文字包含中强调一行或多行代码?

matplotlib - 使用 Julia 和 PyPlot 绘制球体

python - Matplotlib 图例 : labels above markers

python - Altair 置信区间图

python - 在不使用 break 的情况下跳出 python 循环

python - 独立应用程序上的 Django ORM