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
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?
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With