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()
plot() method, we can get the plot of pandas series by using Series. plot() method.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With