Skip to content Skip to sidebar Skip to footer

Matplotlib.pyplot Plot X-axis Ticks In Equal Range

I need to plot a sequence of y values against a sequence of x values. The x values varies in a very large range. pyplot seems using a linear x-axis. So the following code gives me

Solution 1:

For the record, I think this is a bad design choice for a graphic. Having non-regular (be them linear or log) ticks at regular intervals is confusing to the reader.

Okay what you simply have to do is plot against 0, 1, 2, 3, ... but then set the ticks to be your values of x at the same position using plt.xticks()

import numpy as np
import matplotlib.pyplot as plt

x=[1,2,10,100,1000]
y=[5,10,6,7,9]

N = len(x)
x2 = np.arange(N)

plt.plot(x2, y)

plt.xticks(x2, x)

plt.show()

Example

Post a Comment for "Matplotlib.pyplot Plot X-axis Ticks In Equal Range"