0

I have a dataframe (df) with 10 rows looking like this

-2.00    [-24.4907, -24.4594, -24.4321, -24.4012, -24.3...
-1.75    [-23.8154, -23.7849, -23.7601, -23.7326, -23.7...
-1.00    [-23.7131, -23.6954, -23.6767, -23.6616, -23.6...
-0.75    [-22.7675, -22.7505, -22.741, -22.7173, -22.70...
-0.50    [-22.0693, -22.0718, -22.0481, -22.0328, -22.0...
 0.50    [-15.8461, -15.8247, -15.7963, -15.7784, -15.7...
 1.00    [-7.32122, -7.27283, -7.2336, -7.19238, -7.153...
 1.25    [-3.44732, -3.37547, -3.30565, -3.23125, -3.15...
 1.75    [0.541327, 0.568081, 0.597821, 0.627494, 0.667...
 2.50    [3.63716, 3.68494, 3.73379, 3.77966, 3.82584, ...
dtype: object

I'm not 100% sure but I think it contains ndarrays, I'll give you the info that I have:

type(df)
pandas.core.series.Series

whos
df  Series  -2.00    [-24.4907, -24.4<...>82584, ...\ndtype: object

Anyway, I would like to plot all of these arrays in one plot. I'm able to plot one array using

plt.plot(df[1])

One row plotted

And since I have a dataframe of type "series" I hoped using

df.plot
plt.plot()

All rows plotted

would be the solution but it doesn't plot anything. Do you know what I do wrong?

4
  • 1
    Is that a neuron action potential I see? Commented Sep 18, 2017 at 16:21
  • Assuming, your data is like what's pointed out in answer, try pd.DataFrame(df['values'].values.tolist(), index=df['id']).plot(subplots=True)? Commented Sep 18, 2017 at 17:35
  • @JonDeaton haha yeah it is ^^ Commented Sep 19, 2017 at 10:37
  • @Zero the dataframe I have is a bit different than the one used in the example I think. And I'm not smart enough to transfer your idea to my dataframe. Commented Sep 19, 2017 at 10:38

1 Answer 1

1

You have to run a for loop and call plt.plot() repeatedly with the data you want. When you come out of the for loop, say plt.show() and all of your plots should be added to the same figure. Here is what worked for me:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

val_list = [np.array([1,2]), np.array([4,3])]
data = {"id": [1,2],
        "values": val_list
       }
df = pd.DataFrame(data)
for i in range(2):
    plt.plot(df.iloc[:, 0], df.iloc[i, 1])
    plt.plot(df.iloc[:, 0], df.iloc[i, 1])
plt.show()

The above code added both plots to my figure

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your idea! I always try to avoid for-loops though, do you think there's a way to plot it without a for-loop?

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.