Python and plot (): how to limit the number of x-axis ticks (labels)

I have two lists, the same size, one is y_data and one is x_data x_data is the time hh: mm: ss during the day, in fact every minute every series has a length of 1440.

problem:

fig = Figure(figsize=(4,3)) a = gif.add_subplot(111) a.plot(x_data, y_data) 

give unreadable x_axis (too many labels) if I reduce x_data to [range(24)] , for example, the graph gives an error.

Question:

I would like to have only 24 elements on the x_axis scale (every hour, so 1 point every 60 x_data points) is there an easy way to achieve this? set_autoscale_on(False) and then manually setting limits is a very difficult way to achieve this (and I will lose the benefits of autoscaling along the y axis). Another solution seems to be related to a.xaxis.set_ticks() , but I have to create a new series.

o I would like to use x_data, but just limit the number of ticks shown on x_axis, is there any way to do this?

+4
source share
1 answer

The pyplot interface matplotlib provides a locator_params function where you can set this option:

 fig = Figure(figsize=(4,3)) a = fig.add_subplot(111) a.plot(x_data, y_data) a.locator_params(nbins=4) 
+6
source

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


All Articles