'How to make fixed ticks for graph using matplotlib [duplicate]

I am trying to make a graph with fixed ticks, regardless to the points values. For example, I want the ticks to be 0-6, and the points values to be 0, 1, 3. I'd use this code:

import matplotlib.pyplot as plt
import numpy as np

x_points = np.array([0, 1, 3])
y_points = np.array([0, 1, 3])
x_ticks = np.arange(0, 7, 1)
y_ticks = np.arange(0, 7, 1)

plt.xticks(x_ticks)
plt.yticks(y_ticks)
plt.plot(x_points, y_points)
plt.show()

But the result is only 0, 1, 3 ticks - which are the ticks for the actual current values, and not the values I set using xticks and yticks:

Current graph

And I would like to have the ticks fixed, regardless to whether the points values are actually represented in the graph or not, something like that:

Example of target graph

How can I make the axis' ticks to be fixed, regardless of the values it has?



Solution 1:[1]

Try this:

import matplotlib.pyplot as plt
import numpy as np


x_ticks = np.arange(0, 7, 1)
y_ticks = np.arange(0, 7, 1)

plt.xticks(x_ticks)
plt.yticks(y_ticks)
plt.plot(range(0, 7))
plt.show()

Then add your points to the figure

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 goku