簡體   English   中英

如何在熊貓數據系列上繪制任意標記?

[英]how to plot arbitrary markers on a pandas data series?

我正在嘗試在熊貓數據系列上放置標記(以在股市圖上顯示買/賣事件)

我可以在使用pyplot創建的簡單數組上執行此操作,但是找不到有關如何指示熊貓時間序列中任意事件的參考。

也許熊貓沒有內置此功能。有人可以以一些方式提供幫助嗎,那就是沿用這個系列並在曲線上添加一些任意標記...

import datetime
import matplotlib.pyplot as plt
import pandas
from pandas import Series, date_range
import numpy as np
import random



ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000))
ts = ts.cumsum()

#--  the markers should be another pandas time series with true/false values
#--    We only want to show a mark where the value is True
tfValues = np.random.randint(2, size=len(ts)).astype('bool')
markers = Series(tfValues, index=date_range('1/1/2000', periods=1000))

fig, ax1 = plt.subplots()
ts.plot(ax=ax1)

ax1.plot(markers,'g^')   # This is where I get held up.
plt.show()

使用選項

ts.plot(marker='o')

要么

ts.plot(marker='.')

我不得不采取一種略有不同的方法來完全避免使用熊貓繪圖方法。 有點可惜,因為它們很好地格式化了x軸。 盡管如此:

import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas
from pandas import Series, date_range

markers = Series([True, False, False, True, True, True, False, False, True, True],
                 index=date_range('1/1/2000', periods=10))
ts = Series(np.random.uniform(size=10), index=date_range('1/1/2000', periods=10))
ts = ts.cumsum()
ts2 = ts[markers]    

fig, ax1 = plt.subplots()
ax1.plot(ts.index, ts, 'b-')       
ax1.plot(ts2.index, ts2,'g^')
fig.autofmt_xdate()

給我: ts圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM