Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make matplotlib show all x coordinates?

For example in the following code:

import numpy as np import matplotlib.pyplot as plt  N = 10 x = [1,2,3,4,5,6,7,8,9,10] y = np.random.rand(N)  plt.scatter(x, y) plt.show() 

I get the following plot

enter image description here

as you can see, in the x axis only the even values appear. How to force matplotlib to show all values, that is 1 2 3 4 5 6 7 8 9 10?

like image 772
jsguy Avatar asked Feb 13 '15 19:02

jsguy


People also ask

How do you force MatPlotLib to show the values on the X-axis as integers?

Make a new list for only integers tick on X-axis. Use math. floor() and math. ceil() to remove the decimals and include only integers in the list.

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).


1 Answers

Use plt.xticks(x). See the documentation.

Note that using only the input values for ticks will usually be confusing to a viewer. It makes more sense to use evenly spaced ticks.

like image 134
BrenBarn Avatar answered Sep 19 '22 05:09

BrenBarn