用不同的颜色绘制两个列表

2024-04-25 17:51:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我用类似于stem()的Matlba绘制一个列表(s_n_hat),如下所示:

markerline, stemlines, _ = plt.stem(s_n_hat, '-.')
plt.setp(markerline, 'markerfacecolor', 'b')
plt.setp(baseline, 'color','r', 'linewidth', 2)
plt.show()

在我的实际应用程序中,我想用蓝色绘制命中,用红色绘制未命中,我该怎么做?所以,有些元素应该是蓝色的,有些是红色的。在

假设向量的第一部分是命中,第二部分是未命中,我尝试这样做:

^{pr2}$

我希望第一个元素是蓝色的,其他元素都是红色的,但它们似乎是混合的:

enter image description here

有什么想法吗?在


Tags: 元素列表hat绘制pltcolor蓝色红色
1条回答
网友
1楼 · 发布于 2024-04-25 17:51:49

documentation

If no * x * values are provided, the default is (0, 1, ..., len (y) -1)

然后你需要传递一个x,但是这些点是重叠的。比如:

s_n_hat = [1, -1, 1, 1, -1, 1, 1, 1, 1]
x1 = list(range(0, 5))
x2 = list(range(5, 8))
markerline1, stemlines, _ = plt.stem(x1, s_n_hat[0:5], '-.')
plt.setp(markerline1, 'markerfacecolor', 'b')
markerline2, stemlines, _ = plt.stem(x2, s_n_hat[6:9], '-.')
plt.setp(markerline2, 'markerfacecolor', 'r')
plt.show()

enter image description here

相关问题 更多 >