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

我使用类似Matlab的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()

我期望前几个元素是蓝色的,其余的都是红色的,但它们似乎混合在一起了:

enter image description here

有什么想法吗?


回答:

根据文档

如果没有提供 * 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()

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注