簡體   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