5

Is there a quick way to scale axis in matplotlib?

Say I want to plot

import matplotlib.pyplot as plt
c= [10,20 ,30 , 40]
plt.plot(c)

it will plot

enter image description here

How can I scale x-axis quickly, say multiplying every value with 5? One way is creating an array for x axis:

x = [i*5 for i in range(len(c))]
plt.plot(x,c)

enter image description here

I am wondering if there is a shorter way to do that, without creating a list for x axis, say something like plt.plot(index(c)*5, c)

12
  • 2
    If you want to change just the axis, but not the data, you can set the axis limits. But in your above example, you change the actual data, so your question about scaling the axis doesn't really make sense. Commented Dec 4, 2015 at 3:13
  • @Evert wrong, that is a pretty common way of doing things in ggplot, stackoverflow.com/questions/11470579/… Commented Dec 4, 2015 at 3:39
  • This is not R. You're changing the data, just look at your two figures. Commented Dec 4, 2015 at 4:05
  • @Evert, oh since it is not R, it does not make sense to make such plots? ggplot also available in python. Yes I am aware I am changing the data, jeeez. Commented Dec 4, 2015 at 4:36
  • 1
    Perhaps you can find something in the matplotlib gallery that gets close to what you want. The multiple y-axis example may be what you want, other than being the wrong axis. Commented Dec 7, 2015 at 9:25

2 Answers 2

2

It's been a long time since this question is asked, but as I searched for that, I write this answer. IIUC, you are seeking a way to just modify x ticks without changing the values of that axis. So, as the unutbu answer, in another way using arrays:

plt.plot(c)
plt.xticks(ticks=plt.xticks()[0][1:], labels=5 * np.array(plt.xticks()[0][1:], dtype=np.float64))
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Use a numpy.array instead of a list,

c = np.array([10, 20, 30 ,40])   # or `c = np.arange(10, 50, 10)`
plt.plot(c)
x = 5*np.arange(c.size)  # same as `5*np.arange(len(c))`

This gives:

>>> print x
array([ 0,  5, 10, 15])

Comments

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.