osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pyplot: change the number of x labels (from 6)


On 2018-01-10 05:22, Paulo da Silva wrote:
> Hi all.
> 
> I want to have dates as major ticks labels of X axis.
> This fragment of code works fine except that I need more dates to appear
> instead the 6 I am getting. The number of dates in dtsd is for ex. 262.
> 
> Thanks for any help.
> 
> BTW, I got most of this code from some site on the internet.
> 
> ...
> fig=plt.figure(figsize=(12,9))
> gs=gridspec.GridSpec(2,1,height_ratios=[4,1])
> ...
> ax0=plt.subplot(gs[0])
> ...
> plt.xlim(-0.1,dts[-1]+0.1)
> dtsd=pd.to_datetime(ds.getIndexes())
> def format_date(x,__pos=None):
> ? thisind=np.clip(int(x+0.5),0,dtslen-1)
> ? return dtsd[thisind].strftime('%Y-%m-%d')
> ax0.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
> fig.autofmt_xdate()
> ...
> 


It's a bit hard to tell without a working example, but I think you'll
want to set a tick locator, e.g. something like
ax0.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(1))

Read this: https://matplotlib.org/api/ticker_api.html

The old-fashioned way would be to set to tick locations manually with
https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_xticks.html