我试图为沿x轴的每个点制作一个不同颜色的误差线图。错误条形图本身很好,但是当我尝试使用色彩映射时它会爆炸,所以我一定做错了。这是我尝试做的一个人为的例子:
import matplotlib.pyplot as plt
colors = ["b","g","c","m","y","k","r","g","c","m","y","k",
"b","g","c","m","y","k","r","g","c","m","y","k"]
xlabels = ['A','B','8','14']
xval = [0, 1, 2, 3]
yval = [0, 1, 4, 9]
yerr = [0.5, 0.4, 0.6, 0.9]
cmap = dict(zip( xval,colors))
现在我跑完之后我可以去:
plt.errorbar(xval, yval, yerr=yerr, color='b')
这给了我下面显示的图表(即它有效。但是当我尝试这样做时:
plt.errorbar(xval, yval, yerr=yerr, color=cmap)
它给了我一个错误,比如" ValueError:to_rgba:无效的rgba arg" {0:' b',1:' g',2:' c',3:&#39 ;米'}"
有可能做我尝试做的事情吗?我想做的是让错误栏图表中的每个点都有不同的颜色。我真的不需要连接点的线。感谢任何帮助/建议。
答案 0 :(得分:2)
我已经提到过你可以遍历每个特定的点/颜色。
另一种解决方案是在scatter
图中使用errorbar
图,就像在此question中一样。代码在
import matplotlib.pyplot as plt
colors = ["b","g","c","m","y","k","r","g","c","m","y","k",
"b","g","c","m","y","k","r","g","c","m","y","k"]
xlabels = ['A','B','8','14']
xval = [0, 1, 2, 3]
yval = [0, 1, 4, 9]
yerr = [0.5, 0.4, 0.6, 0.9]
plt.scatter(xval, yval, c=colors, s=50, zorder=3)
plt.errorbar(xval, yval, yerr=yerr, zorder=0, fmt="none",
marker="none")
plt.savefig("scatter_error.png", dpi=300)
plt.show()
具有以下结果