如何在熊猫数据系列上绘制任意标记?

P-R*_*Rod 7 python matplotlib pandas

我正试图在熊猫数据系列上放置标记(以显示股市图表上的买/卖事件)

我能够在一个使用pyplot创建的简单数组上执行此操作,但是我无法找到有关如何在pandas时间序列中指示任意事件的参考.

也许熊猫没有内置的这种功能.有人可以提供帮助,以便采用这个系列并在曲线上添加一些任意标记......

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()
Run Code Online (Sandbox Code Playgroud)

Kyu*_*Kim 15

使用该选项

ts.plot(marker='o')
Run Code Online (Sandbox Code Playgroud)

要么

ts.plot(marker='.')
Run Code Online (Sandbox Code Playgroud)


Pau*_*l H 8

我不得不采取略微不同的方法,完全避免使用大熊猫绘图方法.这有点遗憾,因为他们很好地格式化了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()
Run Code Online (Sandbox Code Playgroud)

给我: ts情节