[英]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.