0

I am drawing a plot based on a numpy array:

A = np.array([[4,5,6],[2,3,6]])

with plt.plot(A) it works fine and draws based on 6 tuples: (0,4), (1,5), (2,6), (0,2) etc.

I want to scale the x-axis though. The units should be divided by 120. So I want to plot:

(0,4), (1/120,5), (2/120, 6), etc. 

Is there any easy way to do it, without looping through the array and manually feeding the tuples to the plot?

dorien
  • 5,265
  • 10
  • 57
  • 116
  • Are you sure `plt.plot(A)` works like that? – kameranis Oct 27 '16 at 13:19
  • It draws a line between those tuples in any case. – dorien Oct 27 '16 at 13:20
  • I have been reading this http://stackoverflow.com/questions/34080270/matplotlib-scale-axis-by-multiplying-with-a-constant and it seems like I need to create a new matrix with the x axis coordinates – dorien Oct 27 '16 at 13:20
  • I ran it and it gave me three plots ((0,4), (1,2)), ((0,5), (1,3)), ((0,6), (0,6)) – kameranis Oct 27 '16 at 13:21
  • Sorry, I should have been more clear. Yes there are three lines drawn in the plot. I want to change the x-axis of the full plot. – dorien Oct 27 '16 at 13:23

2 Answers2

1

Specify the x axis

scaling_factor = 120.
x = np.arange(A.shape[0])/scaling_factor
plt.plot(x, A)
kameranis
  • 284
  • 1
  • 13
  • `A.shape[1]` probably? – Divakar Oct 27 '16 at 13:27
  • Well... I thought so myself, but when I did it, `plt` complained `ValueError: x and y must have same first dimension`. So, I'm guessing that something is off. Either the `plt.plot(A)` or the points the OP gave us. – kameranis Oct 27 '16 at 13:31
  • Or with `A.shape[1]` and then `plt.plot(A, x)` maybe? – Divakar Oct 27 '16 at 13:32
  • nope, that doesn't work either... You would need to transpose `A`, so scaling_factor = 120. x = np.arange(A.shape[1])/scaling_factor plt.plot(x, A.transpose()) – kameranis Oct 27 '16 at 13:33
  • The original answer with shape[0] seems to work fine for me. – dorien Oct 27 '16 at 13:47
  • Consider changing your question to be more clear and make readers understand that you have 3 plots with 2 points each – kameranis Oct 27 '16 at 13:48
  • @kameranis You are modifying the data, not the scaling of the axis... What if you had 50 arrays? Scaling the data is not the solution. – Guimoute Dec 19 '19 at 15:40
0
import numpy as np
A = np.array([4,5,6],[2,3,6])

This fails! What is your actual code?

Anyway have you tried providing the x values explicitely to plt.plot():

x = np.arange(3) / 120
plt.plot(x, whatever)
Balzola
  • 456
  • 3
  • 11
  • There are actually 2 points in each plot, so `x = np.arange(2)/120.` Also, now, your `x` is full of zeroes, because integer division. That's why you add `.` at the end, to make it a float. – kameranis Oct 27 '16 at 13:25
  • Thanks. A [] was missing. My original code actually reads in the np array from a huge file. – dorien Oct 27 '16 at 13:49
  • @kameranis Python3 is out for 8 years now and does not require the . to prevent integer division. – Balzola Oct 28 '16 at 09:41
  • yes, but some people still prefer to write in python2. You could be explicit about it. – kameranis Oct 28 '16 at 12:05