Home > Software engineering >  how do I plot an array in python?
how do I plot an array in python?

Time:12-01

I have an array a with shape (43,9). I want to plot all rows of this array in one diagram. for this I used the following code:

plt.plot(a)

it produces this diagram but I think it is wrong: enter image description here

I put part of a here:

30.2682 30.4287 30.4531 30.4675 30.4784 30.4893 30.5002 30.511  30.5219
28.3204 29.4246 30.5289 31.5486 31.8152 31.9301 32.0395 32.1488 32.2582
29.884  30.4592 31.0343 31.4055 31.4843 31.5157 31.549  31.5823 31.6157
29.5203 30.0669 30.6135 30.9845 31.0889 31.1244 31.1599 31.1954 31.2309
30.2158 30.6971 31.1784 31.4935 31.5697 31.6017 31.6336 31.6655 31.6974

how can I show all rows of a as a curve in one plot in python?

CodePudding user response:

Try this:

data = []
for arr in a:
    data.extend(arr)

plt.plot(list(range(len(data))), data)

It will combine all arrays from a into data array

CodePudding user response:

From what I understand, you want to reshape the array such that each dataset is plotted into one line. If this is the correct interpretation, all you need is result

The reshaped version looks like

result2

It is better to use numpy for these operations as it vectorizes the reshaping procedure which is much faster than looping over a list.

CodePudding user response:

The usual stuff

import matplotlib.pyplot as plt
import numpy as np

Let's make an array whose plot will be predictable, butwith a shape similar to the sjhape of your array a

a = np.zeros((41,5))
for i, val in enumerate((2, 4, 6, 8, 10)):
    a[:,i] = val

The array a is

2 4 6 8 10 
2 4 6 8 10 
2 4 6 8 10 
2 4 6 8 10 
2 4 6 8 10 
...
2 4 6 8 10 

so I expect 5 horizontal lines, at y=2, y=4, etc

Finally, we plot a using exactly the same idiom that you have used

plt.plot(a)
plt.grid()
plt.show()

The plot is exactly what I expected

enter image description here

it produces this diagram but I think it is wrong

NO, Matplotlib plotted exactly your data in the ordinates, and in the abscissae simply used the range from 0 to 43-1.

  • Related