0

I have my data organized into a multiindex dataframe. Ex:

    Sweep  Time   Primary     Secondary  x720nm    x473nm      PMTShutter                                                      
Sweep0001 0.00000 -87.429810  -4.882812  0.000610  0.000305    0.000000
          0.00005 -87.445068  -4.882812  0.000610  0.001221    0.000000
          0.00010 -87.451172  -4.272460  0.000000  0.000916    0.000000
            ...        ...       ...       ...         ...  
Sweep0039 0.68655 -87.261963  -4.272461  0.000305  0.000916    0.000305
          0.68660 -87.258911  -4.272461  0.000305  0.000916    0.000305
          0.68665 -87.252808  -5.493164  0.000000  0.000916    0.000305
          0.68670 -87.261963  -4.272461  0.000305  0.000916    0.000305

Plotting any single sweep works fine, but when I go to plot multiple sweeps I have these artifacts that are basically straight lines (see below).

This is not specific to matplotlib, as it also happens with pyqtgraph.

Issue not present when plotting a single trace:

plt.plot(data.Time['Sweep0001'], data.Primary['Sweep0001'])

enter image description here

Issue present once plotting multiple traces:

plt.plot(data.Time['Sweep0001':'Sweep0002'], data.Primary['Sweep0001':'Sweep0002'])

enter image description here

plt.plot(data.Time['Sweep00-1':'Sweep0010'], data.Primary['Sweep0001':'Sweep0010'])

enter image description here

5
  • It looks like your data may include a few outliers. Have you tried whittling down the data to determine exactly which points cause those spikes? Commented Nov 17, 2014 at 2:54
  • are you referring to the vertical spikes? those are normal (they're in all the traces). I'm referring to the horizontal lines (you can see them around the 3.0 mark where there is the downward transient) Commented Nov 17, 2014 at 3:00
  • Might be good to clarify that in your question, since that is not easy to see in your graphs. Those could arise if the data are not in the right order, so that the plot jumps to the right and then back to plot an out-of-sequence point. Again, can you narrow the problem down and provide specific data that will reproduce it, to ensure that the problem is in the plot and not in your data? Commented Nov 17, 2014 at 3:05
  • edited original post to clarify. I'm not sure what you mean, though, by your second point. If you look at my second example - if I plot either Sweep0001 or Sweep0002 by themselves, that artifact does not occur. There could very well be something wrong with how I'm plotting the data, which would be the answer to this issue. I'll put this in the original post as well, but if I plot them separately (i.e. I plot Sweep0001 and then Sweep0002), this issue does not occur Commented Nov 17, 2014 at 3:15
  • Also, the data you show in your post doesn't include a column Time at all. Commented Nov 17, 2014 at 3:20

1 Answer 1

1

data.Time['Sweep001':'Sweep0002'] is concatenating data.Time['Sweep001'] with data.Time['Sweep002']. Thus the time values are going from 0 to N then 0 to N again. plt.plot is thus drawing a line from t=N back to t=0 causing the artifact.

Instead use one plt.plot call for each line:

for i in range(1, 11):
    col = 'Sweep{:04d}'.format(i)
    plt.plot(data.Time[col], data.Primary[col])
Sign up to request clarification or add additional context in comments.

2 Comments

I was just going to add to the original post - if I plot them separately it doesn't produce this issue. Is using a loop the only way to do this?
Well, you could insert a np.nan value between the two data sets, but that's not the standard way to handle this problem. Usually you'd just use a loop. (Try plt.plot([1,2,np.nan,1,2],[0,1,2,1,2]) to see what I mean.)

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.