简体   繁体   中英

how to plot arbitrary markers on a pandas data series?

I'm trying to place marks along a pandas data series (to show buy/sell events on a stock market graph)

I'm able to do this on a simple array that I create using pyplot, however I can't find reference as to how to indicate arbitrary events in a pandas time series.

Perhaps pandas doesn't have this functionality built in. Could someone provide help in the way one would take this series and add some arbitrary marks along the curve...

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

Use the option

ts.plot(marker='o')

or

ts.plot(marker='.')

I had to take a slightly different approach avoiding pandas plotting methods altogether. That's somewhat a shame, since they format the x-axis so nicely. Nonetheless:

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

Gives me: ts图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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