【问题标题】:Plot two lists with different color with stem用茎绘制两个不同颜色的列表
【发布时间】:2017-05-23 03:54:41
【问题描述】:

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

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

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

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

s_n_hat = [1, -1, 1, 1, -1, 1, 1, 1, 1]
markerline1, stemlines, _ = plt.stem(s_n_hat[0:5], '-.')
plt.setp(markerline1, 'markerfacecolor', 'b')
markerline2, stemlines, _ = plt.stem(s_n_hat[6:9], '-.')
plt.setp(markerline2, 'markerfacecolor', 'r')
plt.setp(baseline, 'color','r', 'linewidth', 2)
plt.show()

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

有什么想法吗?

【问题讨论】:

  • “随机”是什么意思?同一个 x 有一个蓝点和一个红点?它们重叠吗?
  • @lbellomo 混,更新了一张图!

标签: python matlab list matplotlib machine-learning


【解决方案1】:

来自documentation

如果没有提供 * x * 值,则默认为 (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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2012-02-26
    • 2016-07-27
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多