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?

5
  • Are you sure plt.plot(A) works like that? Commented Oct 27, 2016 at 13:19
  • It draws a line between those tuples in any case. Commented Oct 27, 2016 at 13:20
  • I have been reading this stackoverflow.com/questions/34080270/… and it seems like I need to create a new matrix with the x axis coordinates Commented Oct 27, 2016 at 13:20
  • I ran it and it gave me three plots ((0,4), (1,2)), ((0,5), (1,3)), ((0,6), (0,6)) Commented Oct 27, 2016 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. Commented Oct 27, 2016 at 13:23

2 Answers 2

1

Specify the x axis

scaling_factor = 120.
x = np.arange(A.shape[0])/scaling_factor
plt.plot(x, A)
Sign up to request clarification or add additional context in comments.

7 Comments

A.shape[1] probably?
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.
Or with A.shape[1] and then plt.plot(A, x) maybe?
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())
The original answer with shape[0] seems to work fine for me.
|
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)

4 Comments

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.
Thanks. A [] was missing. My original code actually reads in the np array from a huge file.
@kameranis Python3 is out for 8 years now and does not require the . to prevent integer division.
yes, but some people still prefer to write in python2. You could be explicit about it.

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.