Reputation: 461
I have an array containing 2 data curves, imported from excel. Below is my array. Column 1 is the x axis, while column 3 is the y axis.
[[ 0.00000000e+00 8.57250668e-06 0.00000000e+00]
[ 1.88000000e+03 8.57250668e-06 1.88617039e-01]
[ 8.01000000e+03 8.57250668e-06 3.42702439e-01]
[ 8.16300000e+04 8.57250668e-06 4.43486869e-01]
[ 0.00000000e+00 1.49761692e-05 0.00000000e+00]
[ 2.09000000e+03 1.49761692e-05 1.58760000e-01]
[ 8.22000000e+03 1.49761692e-05 2.54700000e-01]
[ 8.18400000e+04 1.49761692e-05 2.92848750e-01]]
Here is my code
import numpy as np
import matplotlib.pyplot as plt
A = np.array(
[[0.00000000e+00, 8.57250668e-06, 0.00000000e+00],
[1.88000000e+03, 8.57250668e-06, 1.88617039e-01],
[8.01000000e+03, 8.57250668e-06, 3.42702439e-01],
[8.16300000e+04, 8.57250668e-06, 4.43486869e-01],
[0.00000000e+00, 1.49761692e-05, 0.00000000e+00],
[2.09000000e+03, 1.49761692e-05, 1.58760000e-01],
[8.22000000e+03, 1.49761692e-05, 2.54700000e-01],
[8.18400000e+04, 1.49761692e-05, 2.92848750e-01]])
print A
x= A[:,0]
c0= A[:,1]
y_meas= A[:,2]
plt.plot(x,y_meas,'-b')
plt.title('Reaction')
plt.legend(['Data'], loc='lower right')
plt.show()
Obviously this is not what I want. How do I keep the 2 curves within the array separately, such that I can have 2 discrete curves?
Upvotes: 1
Views: 3831
Reputation: 97271
You can insert NAN
s rows into A
, then the line will be split into parts by NAN
s.
import numpy as np
import matplotlib.pyplot as plt
A = np.array(
[[0.00000000e+00, 8.57250668e-06, 0.00000000e+00],
[1.88000000e+03, 8.57250668e-06, 1.88617039e-01],
[8.01000000e+03, 8.57250668e-06, 3.42702439e-01],
[8.16300000e+04, 8.57250668e-06, 4.43486869e-01],
[0.00000000e+00, 1.49761692e-05, 0.00000000e+00],
[2.09000000e+03, 1.49761692e-05, 1.58760000e-01],
[8.22000000e+03, 1.49761692e-05, 2.54700000e-01],
[8.18400000e+04, 1.49761692e-05, 2.92848750e-01]])
idx = np.where(np.diff(A[:, 0]).ravel() < 0)[0] + 1
A2 = np.insert(A, idx, np.nan, axis=0)
x, c0, y_meas = A2.T
plt.plot(x,y_meas,'-b')
plt.title('Reaction')
plt.legend(['Data'], loc='lower right')
plt.show()
output:
If you want each line with different color, you can split A
:
idx = np.where(np.diff(A[:, 0]).ravel() < 0)[0] + 1
for A2 in np.split(A, idx):
x, c0, y_meas = A2.T
plt.plot(x,y_meas)
output:
Upvotes: 1
Reputation: 6257
It's a little hard to tell what you are trying to produce. But looking at the x- and y-axis data points, it's clear that you are dealing with data that begins at zero, increases, then goes back to zero. So, assuming that these are the two curves that you could have to plot, you can separate the array as follow:
x1= A[:,0][:4]
x2= A[:,0][4:]
c0= A[:,1]
y_meas1= A[:,2][:4]
y_meas2= A[:,2][4:]
plt.plot(x1,y_meas1,'-b')
plt.plot(x2,y_meas2,'-g')
plt.title('Reaction')
plt.legend(['Data1', 'Data2'], loc='lower right')
plt.show()
If you have more data than just these 8 data points within the array, you could create a loop to automatically parse the array by checking for when the x- or y-coordinates (or both) are equivalent to zero and saving the previous x- and y-values (within a range) in order to plot them. In this way you wouldn't have to create all of the arrays by hand. Hope this helps.
Upvotes: 2