[英]Error bars in different colors with matplotlib
我有以下代码,它生成一个带有垂直和水平误差条的简单图形:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pylab as pl
import matplotlib.pyplot as plt
x=[2,3]
error_x=[0.5,0.4]
y=[25,28]
error_y=[0.6,0.8]
lines={'linestyle': 'None'}
plt.rc('lines', **lines)
pl.plot(x, y, 'ro', markersize=6)
pl.errorbar(x, y, xerr=error_x, yerr=error_y, fmt='b')
pl.xlim(1.3,3.6)
pl.ylim(24.0,29.0)
pl.show()
但是,两个误差条(垂直和水平)都是蓝色的,我不知道如何为它们指定不同的颜色。 这可能吗? 例如,我希望所有水平误差条为蓝色,所有垂直误差条为绿色。
使用ecolor
关键字:
import pylab as pl
import matplotlib.pyplot as plt
x=[2,3]
error_x=[0.5,0.4]
y=[25,28]
error_y=[0.6,0.8]
lines={'linestyle': 'None'}
plt.rc('lines', **lines)
pl.plot(x, y, 'ro', markersize=6)
el = pl.errorbar(x, y, xerr=error_x, yerr=error_y, fmt='b', ecolor='yellow')
pl.xlim(1.3,3.6)
pl.ylim(24.0,29.0)
pl.show()
输出:
每个条形和每个方向的不同颜色线:
import pylab as pl
import matplotlib.pyplot as plt
x=[2,3]
error_x=[0.5,0.4]
y=[25,28]
error_y=[0.6,0.8]
lines={'linestyle': 'None'}
plt.rc('lines', **lines)
pl.plot(x, y, 'ro', markersize=6)
el = pl.errorbar(x, y, xerr=error_x, yerr=error_y, fmt='b', ecolor=['yellow','blue'])
elines = el.get_children()
elines[1].set_color('green')
pl.xlim(1.3,3.6)
pl.ylim(24.0,29.0)
pl.show()
输出:
对于使用旧版 matplotlib 的人的另一个“技巧”:
import pylab as pl
import matplotlib.pyplot as plt
x=[2,3]
error_x=[0.5,0.4]
y=[25,28]
error_y=[0.6,0.8]
lines={'linestyle': 'None'}
plt.rc('lines', **lines)
pl.plot(x, y, 'ro', markersize=6)
pl.errorbar(x, y, xerr=error_x, yerr=0, fmt='b', ecolor='b')
pl.errorbar(x, y, xerr=0, yerr=error_y, fmt='g', ecolor='g')
pl.xlim(1.3,3.6)
pl.ylim(24.0,29.0)
pl.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.