首页 > 解决方案 > 在 matplotlib 错误栏中,如何通过不同颜色的颜色传递不同的点?

问题描述

我有

fig, axs = plt.subplots(shape[1], shape[0], figsize=(9, 3 * shape[1]), sharey=True)
    for idx, ax in enumerate(axs.reshape(-1)):

        categorical_labels = ...
        values = ...
        errors = ...
        colours = ...


        ax.errorbar(categorical_labels, values, yerr=errors, fmt='.', ecolor=colours, mfc=colours, mec=colours) 

这不起作用,因为ecolormfc并且,mec不接受颜色列表(每个点一种颜色),一旦它看到颜色列表,它就会假定它是 RGBA 颜色。因此它给出了错误:

ValueError:RGBA 参数无效:('lime'、'magenta'、'darkorange'、'cyan'、'cornflowerblue'、'cornflowerblue'、'lime'、'magenta'、'darkorange'、'cyan')

我可以通过遍历所有点并单独绘制它们来解决这个问题:

for i, v in enumerate(values):
    ax.errorbar([categorical_labels[i]], [values[i]], yerr=[errors[i]], fmt='.', ecolor=colours[i], mfc=colours[i], mec=colours[i]) 

这给了我类似的东西: 在此处输入图像描述

这就是我想要的。

但是有没有办法在不循环所有点的情况下做到这一点?stackoverflow 上的所有其他答案都使用cmapcolormaperrorbar没有这些参数。

标签: pythonmatplotlibcolorserrorbar

解决方案


推荐阅读