Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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() 

Here is my plotted data

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?

share|improve this question
1  
What do you mean by two discrete curves? In your code you only try to plot one curve. Does column 2 contain the set of y-coordinates for the other curve? To make the plot for this curve work you need to sort by the x-coordinate. See this question for an example: stackoverflow.com/questions/20655246/… –  Mr E Jan 19 at 19:21
 
The plot() command plots points according to their order in the array. Perhaps you want to sort the array first? –  Bitwise Jan 19 at 19:29
1  
@Mr E ,sorry for the ambiguity. I wanted to plot a graph with two separate curves. Its not the case in image where curves are conjoined, thus the diagonal line coming straight down back to the origin. In the array, the top half data is for one curve, the bottom half is for the other. I clearly know I need to separate the data within the array somehow, but I'm still not sure how to do it after thinking for hours. This is also important for me to do scipy.optimize() to fit the curve to my model equation, where its data argument, somehow only allows one array. Your help is greatly appreciated –  user3211991 Jan 19 at 20:15
 
@user3211991 I noticed that you have not placed a check mark on the answer that best solves your problem for any of the questions that you've asked. If you could do that, it helps both the people who answer your question and those who later have a similar question find the right solution. Thanks. –  cosmosis Jan 19 at 20:27
add comment

2 Answers

up vote 2 down vote accepted

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() 

enter image description here

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.

share|improve this answer
 
Thanks for your solution, just wondering, how did you copied and paste the data into python for processing such that it is recognised by the programme? –  user3211991 Jan 20 at 10:38
 
I used the function %cpaste in ipython. It seems like you are rather new to python and still trying to understand the basics. Might I suggest that you go over a tutorial before you go much further in your analysis. I imagine it would help you quite a bit. –  cosmosis Jan 20 at 18:14
 
Thanks for the feedback and also advice to the question. I have tried interactive exercises like codecademy and I couldn't find any interactive exercises specifically for Scipy. So I thought the learning by doing approach would be useful although the learning curve is steep. My real issue is the curve fitting which I posted on stackoverflow.com/questions/21264227/… . Do comment if you know something on it. Thanks. –  user3211991 Jan 21 at 17:06
add comment

You can insert NANs rows into A, then the line will be split into parts by NANs.

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:

enter image description here

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:

enter image description here

share|improve this answer
 
Thanks for your solution, if I use the NAN insertion method into my data, and fit it to the model function using optimize.curvefit, would the NAN cells affect the final solution, or would python simply ignore the NAN cell and move on to the next cell for calculation? –  user3211991 Jan 20 at 10:32
 
the array with NAN is for plot, you should use the original array for curvefit. –  HYRY Jan 20 at 11:53
 
@user3211991 if you're asking if curvefit will fit two curves (as in, if it'll treat nan values as some kind of delimited) the answer is no. You should call the function separately on each relevant slice of the array if you want to fit multiple curves. –  Mr E Jan 20 at 17:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.