0

我正在尝试创建具有四种不同线条样式的线条图。我有 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()

在此处输入图像描述

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

4

2 回答 2

2

您的

errorbar()

应该有一个

linestyle = ''

否则,这将为您划清界限。

于 2016-01-04T13:17:52.173 回答
2

errorbar绘制误差线数据。

在下面的代码中,

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

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

要解决此问题,只需调用一次errobar

ax.errorbar(xl, a_mean, linestyle = '--', yerr = a_se, color = 'k', linewidth = 1)
于 2016-01-04T13:25:52.307 回答