I have the following code, that produces a simple graph with both vertical and horizontal error bars:
#!/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()
However, both error bars (vertical and horizontal) are blue, and I don't see how I could specify different colors for both of them. Is this possible? I would like, for example, for all horizontal error bars to be blue and for all vertical error bars to be green.
Use ecolor
keyword:
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()
Output:
Different color lines, per bar and per orientation:
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()
Output:
Another "trick" for people with an older matplotlib version:
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With