2

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?

6
  • What exactly do you mean in one-line ? Without an additional one to your code ? Commented Jan 11, 2016 at 15:08
  • @Moritz : I'd like to change the axis' label to 0.0 ... 50.0 like this : gget.it/hodulb9e/EovWW.png Commented Jan 11, 2016 at 15:15
  • I suggest you scale your data like data - min(data) and do plt.xlim(0,50) Commented Jan 11, 2016 at 15:18
  • But that would be at least two lines Commented Jan 11, 2016 at 15:18
  • I would like to do it without modifying data, but just faking the x-axis label Commented Jan 11, 2016 at 15:19

1 Answer 1

1

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'])
Sign up to request clarification or add additional context in comments.

2 Comments

I understand that this is what the OP requested, but this isn't really a great solution in general, since you've decoupled the tick labels from the data coordinates
Sure, this is making the graph to lie about its content. But if one doesn't have a way to change the generation of the lines but only the figure generation, it may be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.