Matplotlib:除了指定的线条样式外,还有很好的实线

时间:2016-01-04 13:01:56

标签: python matplotlib errorbar linestyle

我正在尝试使用四种不同的线条样式创建线条图。我有y轴的平均值和每个y值的误差。因为我希望它是全部相同的颜色(黑色),我想通过不同的线条样式区分它们,但是我的代码在下面,我得到所有四个的细线,所以线条样式看起来不那么明显。我做错了什么?

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

另外,我想让误差线的线宽与线条相同,但是当我使它变粗时,线条看起来都一样。

2 个答案:

答案 0 :(得分:2)

你的

errorbar()

应该有

linestyle = ''

否则这会为你画线。

答案 1 :(得分:2)

errorbar绘制错误栏数据。

在下面的代码中,

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

数据是两次。一旦使用linestyle“ - ”(ax.plot),一次使用默认的linestyle“ - ”(ax.errorbar)。当您增加errorbar的线宽时,您只会看到默认的线型。

要解决此问题,请仅使用一次errobar

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