python - Issue with matplotlib formatting using pandas -
i'm looking on issue have graphs.
i'm trying plot financial data contained inside dataframe dates , hours index, however, matplotlib standard dates formatting doesn't trick here don't want evenly spread ticks on x axis, shows big horizontal lines between business hours.
the solution came plot graph using np.arange on x axis, , use index label on x axis, not using matplotlib date formatting, still displaying dates on graph.
my code following:
l = np.arange(len(dax_m15.index)) def format_date(x, pos=none): return dax_m15.index[x].strftime("%y-%m-%d %h-%m-%s") fig, ax = plt.subplots() ax.plot(l, dax_m15["open"]) ax.xaxis.set_major_formatter(ticker.funcformatter(format_date)) ax.set_title("custom tick formatter") fig.autofmt_xdate()
however, following error when using this:
file "d:\anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 197, in __draw_idle_agg figurecanvasagg.draw(self) file "d:\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 464, in draw self.figure.draw(self.renderer) file "d:\anaconda\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs) file "d:\anaconda\lib\site-packages\matplotlib\figure.py", line 1144, in draw renderer, self, dsu, self.suppresscomposite) file "d:\anaconda\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images a.draw(renderer) file "d:\anaconda\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs) file "d:\anaconda\lib\site-packages\matplotlib\axes\_base.py", line 2426, in draw mimage._draw_list_compositing_images(renderer, self, dsu) file "d:\anaconda\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images a.draw(renderer) file "d:\anaconda\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper draw(artist, renderer, *args, **kwargs) file "d:\anaconda\lib\site-packages\matplotlib\axis.py", line 1136, in draw ticks_to_draw = self._update_ticks(renderer) file "d:\anaconda\lib\site-packages\matplotlib\axis.py", line 969, in _update_ticks tick_tups = [t t in self.iter_ticks()] file "d:\anaconda\lib\site-packages\matplotlib\axis.py", line 969, in <listcomp> tick_tups = [t t in self.iter_ticks()] file "d:\anaconda\lib\site-packages\matplotlib\axis.py", line 916, in iter_ticks i, val in enumerate(majorlocs)] file "d:\anaconda\lib\site-packages\matplotlib\axis.py", line 916, in <listcomp> i, val in enumerate(majorlocs)] file "d:\anaconda\lib\site-packages\matplotlib\ticker.py", line 386, in __call__ return self.func(x, pos) file "d:/finance python/test_data_formatter.py", line 40, in format_date return dax_m15.index[x].strftime("%y-%m-%d %h-%m-%s") file "d:\anaconda\lib\site-packages\pandas\tseries\base.py", line 247, in __getitem__ raise valueerror valueerror
would have idea on issue here , how solve it?
thanks.
as said in comments, problem index of list or series needs integer between 0 , length of list. dax_m15.index[4.5]
not work.
in order ensure locations have datapoint associated them ticked, may use matplotlib.ticker.indexlocator
. e.g. if want label every fifth point of list,
ax.xaxis.set_major_locator(ticker.indexlocator(5,0)) ax.xaxis.set_major_formatter(ticker.funcformatter(format_date))
inside formatter need make sure index integer , within range of allowed values.
def format_date(x, pos=none): if int(x) >= len(df.index) or int(x) < 0: return "" return df.index[int(x)].strftime("%y-%m-%d %h-%m-%s")
a complete example:
import matplotlib.pyplot plt import matplotlib.ticker ticker import numpy np import pandas pd inx = pd.datetimeindex(start="2017-05-05", freq="7h", periods=45) df = pd.dataframe({"open" : np.random.rand(len(inx))}, index=inx) l = np.arange(len(df.index)) def format_date(x, pos=none): return df.index[int(x)].strftime("%y-%m-%d %h-%m-%s") fig, ax = plt.subplots() ax.plot(l, df["open"]) ax.xaxis.set_major_locator(ticker.indexlocator(5,0)) ax.xaxis.set_major_formatter(ticker.funcformatter(format_date)) ax.set_title("custom tick formatter") fig.autofmt_xdate() plt.show()
Comments
Post a Comment