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)

Ali_Sh
  • 2,667
  • 3
  • 43
  • 66
Emmet B
  • 5,341
  • 6
  • 34
  • 47
  • 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. –  Dec 04 '15 at 03:13
  • @Evert wrong, that is a pretty common way of doing things in ggplot, http://stackoverflow.com/questions/11470579/transforming-axis-labels-with-a-multiplier-ggplot2 – Emmet B Dec 04 '15 at 03:39
  • This is not R. You're changing the data, just look at your two figures. –  Dec 04 '15 at 04: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. – Emmet B Dec 04 '15 at 04:36
  • So ask the right question: how do I scale my xdata, not the axis. Which is exactly what DilithiumMatrix's answer is about. –  Dec 04 '15 at 06:48
  • @Evert I am not trying to scale the data at all. I need to represent a huge data in different units without creating REDUNDANT data. If that does not blink any light, then maybe you should not make any conclusions in seconds about what makes sense. – Emmet B Dec 04 '15 at 07:20
  • You always create a new (redundant) copy of the data. whether you use your suggested list or a numpy array. So is there a reason you don't want to create that extra copy (say, memory bounds)? But you're scaling the data, not the axis. –  Dec 04 '15 at 07:43
  • "represent a huge data in different units". That suggests you have multiple datasets that you want to plot inside the same figure, each with their own units. In which case you should simply introduce a separate x-axis for each dataset, each with their own scaling. –  Dec 04 '15 at 08:02
  • @Evert Right, I was aware, that's why I cannot create the list I suggested. yes your 2nd comment is close, how do I introduce a seperate x axis? – Emmet B Dec 07 '15 at 02:48
  • 1
    Perhaps you can find something in the [matplotlib gallery](http://matplotlib.org/gallery.html) that gets close to what you want. The [multiple y-axis example](http://matplotlib.org/examples/pylab_examples/multiple_yaxis_with_spines.html) may be what you want, other than being the wrong axis. –  Dec 07 '15 at 09:25
  • DilithiumMatrix we still need to create a new matrix with x-axis coordinates. I'm looking to do something similar. – dorien Oct 27 '16 at 13:19
  • You may be interested in the first answer here: http://stackoverflow.com/questions/40285337/changing-the-axis-scale-in-numpy-plot – dorien Oct 27 '16 at 13:49

2 Answers2

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])
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
0

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

Ali_Sh
  • 2,667
  • 3
  • 43
  • 66