繁体   English   中英

如何使用线和标记绘制重叠序列?

[英]How to plot overlapping series using line and markers?

鉴于以下数据:

df1

               a       b    c
 1/1/2017   -162    1525     -41
 1/2/2017    192    1530      86
 1/3/2017     33    1520    -124
 1/4/2017    173    1502    -108
 1/5/2017    194    1495     -31
 1/6/2017    -15    1520     -46
 1/7/2017     52    1525     181
 1/8/2017     -2    1530    -135
 1/9/2017     37    1540      65
1/10/2017    197    1530      73

df2

              a
1/3/2017     33
1/6/2017    -15
1/7/2017     52
1/8/2017     -2
1/9/2017     37

我如何使用matplotlib在图表中生成,绘制df1 'b'列,并在其上方,将标记放在相同的绘图线上,但使用df2的索引点。

所需的图表看起来像这样:

在此输入图像描述

我看了这个答案,但不能很好地适应它。 问题是在示例中他们使用值,但在我的情况下,两个数据集之间共同的部分是索引

这是我尝试的引用问题的代码:

xs = df1['b']
ys = df2['a'] # ---> this doesn't make sense here....
markers_on = df2.index
plt.plot(xs, ys, '-gD', markevery=markers_on)
plt.show()

但该图表显示为空:

TypeError: <class 'NoneType'> type object None

我也试过了

xs = df1['b']
markers_on = list(df2.index)
plt.plot(xs, '-gD', markevery=markers_on)
plt.show()

但我明白了

ValueError: `markevery` is iterable but not a valid form of numpy fancy indexing

市场营销有不同的可能格式。 他们都没有使用实际值来标记。 在这里,使用值的索引来标记,或者与数据长度相同的布尔数组是有意义的。 后者看起来像这样:

import numpy as np 

markers_on = np.isin(df1.index, df2.index) 

plt.plot(df1["b"], '-gD', markevery=list(markers_on))
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM