简体   繁体   English

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

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

Given the following data: 鉴于以下数据:

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

How can I produce at chart using matplotlib, that plots 'b' column of df1 and on top of that, puts markers on same plot line, but using the index points from df2 . 我如何使用matplotlib在图表中生成,绘制df1 'b'列,并在其上方,将标记放在相同的绘图线上,但使用df2的索引点。

The desired chart would look something like this: 所需的图表看起来像这样:

在此输入图像描述

I looked at this answer but can't quite adapt it. 我看了这个答案,但不能很好地适应它。 The issue is that in the example they use the values, but in my case the part that is common between the two data-sets is the index 问题是在示例中他们使用值,但在我的情况下,两个数据集之间共同的部分是索引

This is code from the referenced question that I tried: 这是我尝试的引用问题的代码:

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()

But the chart comes back empty: 但该图表显示为空:

TypeError: <class 'NoneType'> type object None

I also tried 我也试过了

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

But I get 但我明白了

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

The there are different possible formats for markevery. 市场营销有不同的可能格式。 None of them uses the actual values to mark. 他们都没有使用实际值来标记。 Here, it makes sense to either use the indices of the values to mark, or a boolean array of the same length as the data. 在这里,使用值的索引来标记,或者与数据长度相同的布尔数组是有意义的。 The latter would look like this: 后者看起来像这样:

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