0

I am trying to draw two data lines with error bars, each having the same color as the data line. However, I get another thin line with a color I have not specified in each data line when I add an error bar.

Also, I would like to make the caps of the error bars thicker but the option capthick is not valid here.

Could anybody please help me fix these issues?

This is my code.

import matplotlib.pyplot as plt
from pylab import *
import numpy as np

xaxis = [1, 2, 3]
mean1 = [1,2,3.6]
se1 = [0.2, 0.5, 0.9]
mean2 = [10, 29, 14]
se2 = [3, 4, 2]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('X', fontsize = 16)
ax.set_ylabel('Y', fontsize = 16)

ax.axis([0, 5, 0, 35])
ax.plot(xaxis, mean1, 'r--', linewidth = 4)
ax.errorbar(xaxis, mean1, yerr = se1, ecolor = 'r', elinewidth = 2, capsize = 5)
ax.plot(xaxis, mean2, 'b--', linewidth = 4)
ax.errorbar(xaxis, mean2, yerr = se2, ecolor = 'b', elinewidth = 2, capsize = 5)
plt.show()

owl
  • 1,841
  • 6
  • 20
  • 30
  • That's because you're plotting the data twice. You can make `errorbar` draw the lines too. – will Oct 15 '14 at 11:21
  • Thank you for your reply! Could I have errorbar to draw the data line too? – owl Oct 15 '14 at 11:31

1 Answers1

4

The extra thin line is coming from the errorbar() call.

errorbar will draw a line too, what you're doing is changing the colour of the error bars, but not the actual lines (hence it using the standard matplotlib first two colours, blue and green.

it's all in the documentaion, here.

To achieve what you want, you only need to use the errorbar() function;

This does what you want i think, maybe jsut tweak the numbers a bit.

import matplotlib.pyplot as plt
from pylab import *
import numpy as np

xaxis = [1, 2, 3]
mean1 = [1,2,3.6]
se1 = [0.2, 0.5, 0.9]
mean2 = [10, 29, 14]
se2 = [3, 4, 2]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('X', fontsize = 16)
ax.set_ylabel('Y', fontsize = 16)

ax.axis([0, 5, 0, 35])
linestyle = {"linestyle":"--", "linewidth":4, "markeredgewidth":5, "elinewidth":5, "capsize":10}
ax.errorbar(xaxis, mean1, yerr = se1, color="r", **linestyle)
ax.errorbar(xaxis, mean2, yerr = se2, color="b", **linestyle)
plt.show()

I put the common line style arguments into a dict which gets unpacked.

enter image description here

will
  • 10,260
  • 6
  • 46
  • 69
  • Great! Your answer solved all the issues I had! Thank you! I had checked the document but I guess I missed that part. I tried to add the legend to the same errorbar line but that included the error bar too. Is it possible to have the legend (and label) of just the lines? I typically have them in the ax.plot line. – owl Oct 15 '14 at 11:59
  • 1
    You can manually change the contents of the legend, yes. But it's a little hacky. Have a look [here](http://stackoverflow.com/q/13303928/432913) – will Oct 15 '14 at 12:03
  • Wow, this seems complicated but I will give a try! Thanks! – owl Oct 15 '14 at 12:06
  • Do not connect data _points_, it implies knowledge you do not have. Straight lines especially are almost always plain wrong. Also you should not do the `from pylab import *` import – MaxNoe Oct 16 '14 at 14:43
  • I was just copying the code that had been used to be honest, it's not even used. and also disagree with doing `from X import *`, it hides too much, and is frustratingly included in so many examples. – will Oct 16 '14 at 15:11
  • And while in general you shouldn't connect points by a line, it depends entirely on what you're plotting. And it's what the question was... – will Oct 16 '14 at 15:14