How to build custom markers in pandas data series?

I am trying to place tags along a pandas data series (to show buy / sell events on the stock market chart)

I can do this on a simple array that I create using pyplot, however I cannot find a link to how to specify arbitrary events in the pandas time series.

Perhaps pandas does not have built-in functions. Can anyone help on how to take this series and add some arbitrary labels 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() 
+6
source share
2 answers

Use option

 ts.plot(marker='o') 

or

 ts.plot(marker='.') 
+9
source

I had to take a slightly different approach, while avoiding pandas construction methods. This is a little embarrassing as they format the x axis so well. 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

+6
source

Source: https://habr.com/ru/post/957946/


All Articles