Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 256
P-Rod Avatar asked Nov 12 '13 20:11

P-Rod


People also ask

Can you plot a Pandas series?

plot() method, we can get the plot of pandas series by using Series. plot() method.


2 Answers

Use the option

ts.plot(marker='o')

or

ts.plot(marker='.')
like image 61
KyungHoon Kim Avatar answered Oct 12 '22 13:10

KyungHoon Kim


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 plot

like image 37
Paul H Avatar answered Oct 12 '22 15:10

Paul H