Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Make all values in range show up on x axis

Tags:

Could someone please guide me on how should I make sure that all ticks (or maybe a better way to specify will be to say all elements in the list passed to plot function) are displayed on the x axis when using matplotlib to plot graphs?

plt.plot(xValues, meanWeekdayArrivalCounts, 'k-') 

I want all the values in the list xValues to show up on the graph. By default, only, 0, 10, 20, 30, 40, 50 show up.

like image 226
Shubham Goyal Avatar asked Jun 14 '13 06:06

Shubham Goyal


People also ask

How do I show all the values on the X-axis in MatPlotLib?

MatPlotLib with Python To show all X coordinates (or Y coordinates), we can use xticks() method (or yticks()).

How do you change the range of the X-axis in Python?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.

How do I show all label values in MatPlotLib?

Steps. Create a list of numbers (x) that can be used to tick the axes. Get the axis using subplot() that helps to add a subplot to the current figure. Set the ticks on X and Y axes using set_xticks and set_yticks methods respectively and list x (from step 1).

How do you spread X-axis in MatPlotLib?

To increase the space for X-axis labels in Matplotlib, we can use the spacing variable in subplots_adjust() method's argument.


Video Answer


2 Answers

Simply add plt.xticks(xValues) to your code. Given the number of points in your graph, the labels might clutter.

You could display them as minor ticks if you set them on the axes object with ax.set_xticks(xValues, minor=True).

like image 78
Rutger Kassies Avatar answered Oct 07 '22 01:10

Rutger Kassies


use this.

fig = plt.figure(figsize=(10,8)) plt.xticks(np.arange(min(x), max(x)+1, 1.0)) 
like image 38
Raghu Ram Avatar answered Oct 07 '22 02:10

Raghu Ram