Basj
Basj

Reputation: 46493

Change axes after plot

After having plotted some data:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(np.linspace(-3.0, 3.0, 10), np.random.random(10))
plt.plot(np.linspace(0.0, 7.0, 50),  np.random.random(50))
plt.show()

enter image description here

I'd like to change the x-axis labels to 0.0 ... 50.0 instead of -4.0 ... 8.0 (but keeping exactly the same plot). Is it possible to do it in one-line?

Upvotes: 2

Views: 688

Answers (1)

Joël
Joël

Reputation: 2822

You can use the following function, and give numerical labels:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(np.linspace(-3.0, 3.0, 10), np.random.random(10))
plt.plot(np.linspace(0.0, 7.0, 50), np.random.random(50))

plt.xticks([-4, -2, 0, 2, 4, 6, 8], ['0', '10', '20', '30', '40', '50'])

Upvotes: 1

Related Questions