0

I am trying to create a line figure with four different line styles. I have mean values for y axis and error for each y value. Because I want it to be all same color (black), I want to differentiate them by different line styles but with my code below, I get fine solid line for all four so the line styles do not look so distinct. What am I doing wrong?

import matplotlib.pyplot as plt
import numpy as np

xl = [1, 2, 3, 4, 5, 6]

a_mean = [17, 15, 20, 22, 18, 16]
a_se = [1, 2, 1, 3, 1.5, 2]
b_mean = [5, 6, 2, 5, 1, 9]
b_se = [1, 2, 0.3, 1, 2, 1]
c_mean = [2, 4, 6, 8, 10, 12]
c_se = [1, 2, 2, 1, 2, 1.5]
d_mean = [12, 10, 8, 6, 4, 2]
d_se = [3, 2, 1, 2, 1, 1]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('X', fontsize = 16)
ax.set_ylabel('Y', fontsize = 16)
plt.axis([0, 7, 0, 30])
x = np.linspace(0, 1)
y = np.linspace(0, 30)
ax.plot(xl, a_mean, linestyle = '-', color = 'k', linewidth = 3, marker  = '', label = 'A')
ax.errorbar(xl, a_mean, yerr = a_se, color = 'k', linewidth = 1)
ax.plot(xl, b_mean, linestyle = '--', color = 'k', linewidth = 3, marker = '', label = 'B')
ax.errorbar(xl, b_mean, yerr = b_se, color = 'k', linewidth = 1)
ax.plot(xl, c_mean, linestyle = ':', color = 'k', linewidth = 3, marker = '', label = 'C')
ax.errorbar(xl, c_mean, yerr = c_se, color = 'k', linewidth = 1)
ax.plot(xl, d_mean, linestyle = '-.', color = 'k', linewidth = 3, marker = '', label = 'D')
ax.errorbar(xl, d_mean, yerr = d_se, color = 'k', linewidth = 1)
ax.legend(loc = 0, frameon = False, prop = {'size': 12})
plt.show()

enter image description here

Also, I want to make the error bars to have same line width as for the lines but when I make it thicker, the lines all look the same.

Mel
  • 5,837
  • 10
  • 37
  • 42
owl
  • 1,841
  • 6
  • 20
  • 30

2 Answers2

2

your

errorbar()

should have a

linestyle = ''

else this will draw the line for you.

Svend Feldt
  • 758
  • 4
  • 17
  • Thank you for your quick response! It did fix the fine line issue and now I can make the error bars thicker as well. – owl Jan 04 '16 at 13:31
2

errorbar plots the error bar and the data.

In the code bellow,

ax.plot(xl, a_mean, linestyle = '--', color = 'k', linewidth = 3)
ax.errorbar(xl, a_mean, yerr = a_se, color = 'k', linewidth = 1)

the data is plot twice. Once with the linestyle "--" (ax.plot), and once with the default linestyle "-" (ax.errorbar). When you increase the linewidth of errorbar, you only see the default linestyle.

To fix this, only use one call to errobar:

ax.errorbar(xl, a_mean, linestyle = '--', yerr = a_se, color = 'k', linewidth = 1)
Mel
  • 5,837
  • 10
  • 37
  • 42
  • Thanks for the explanation, although in this case, could you change the width of the data independently of the error line width? – tglaria Jan 04 '16 at 13:31
  • 1
    I don't think you could. If you want to different linewidth, the other answer is the way to go. – Mel Jan 04 '16 at 13:36
  • Thank you, I didn't know that error bar draw the line again. – tglaria Jan 04 '16 at 13:42
  • I did not know that errorbar plots both the error bar and the data. Thank you so much for the further clarification! – owl Jan 05 '16 at 06:56