2

I have a dataframe. Each cell in two columns contains a list. I want to draw a seaborn-relplot with two columns as x and y. I am running in to issue

My code:

xdf = pd.DataFrame({'v':[[1,2,10,20],[3,4]],'i':[[5,6,50,60],[7,8]]})
xdf['nor_iv'] = ['True','False']
print(xdf)
xdf = 
                v               i nor_iv
0  [1, 2, 10, 20]  [5, 6, 50, 60]   True
1          [3, 4]          [7, 8]  False

# Seaborn plot
hue_colors = {'True': 'red', 
              'False': 'green'}
sns.relplot(data=xdf,x="v", y="i",hue='nor_iv',
             markers=True,kind='line',
                       palette=hue_colors,height=4, aspect=1.5)
plt.show()

Present output:

TypeError: unhashable type: 'list'
9
  • how do you plan to plot the list? You can't plot a series that is a list. Commented May 13, 2021 at 0:45
  • @DavidErickson you mean, it is not possible to plot the list vs list? It is a new attempt. Commented May 13, 2021 at 0:46
  • What are you attempting to visualize with the list is my question I guess. Are those coordinates? If those are coordinates those should under x and y,. for first and second value of the coordinate, respectively. Then the hue or columns should be v and i, so you would need to manipulate the dataframe. Commented May 13, 2021 at 0:47
  • @DavidErickson oho! I see. This is a demo question and I just made it. My actual data frame is big. Each list contains a few hundred samples. No, these are measurement data. If a cell in v has list of 100 samples, then i will have list of 100 samples. These two are voltage and current data. I just edited the question with few more samples. Commented May 13, 2021 at 0:49
  • you can explode the list into four rows. Is the list relevant at all or just the values inside of them? Also, are the lists of equal length for v and i? That is important to make sure that explode works properly Commented May 13, 2021 at 0:50

1 Answer 1

2

First use xdf = xdf.apply(pd.Series.explode). Please note that you then need to convert the required columns from strings i.e. object to data type int after the explosion. Three ways to visualize below:

If you want an overlay (no rows or columns):

import matplotlib.pyplot as plt
import seaborn as sns
xdf = pd.DataFrame({'v':[[1,2,10,20],[3,4]],'i':[[5,6,50,60],[7,8]]})
xdf['nor_iv'] = ['True','False']
xdf = xdf.apply(pd.Series.explode)
xdf['v'] = xdf['v'].astype(int)
xdf['i'] = xdf['v'].astype(int)
sns.relplot(data=xdf,x="v", y="i",hue='nor_iv',
             markers=True,kind='line',
                       palette=hue_colors,height=4, aspect=1.5)
plt.show()

enter image description here

If you want columns, you can do:

import matplotlib.pyplot as plt
import seaborn as sns
xdf = pd.DataFrame({'v':[[1,2,10,20],[3,4]],'i':[[5,6,50,60],[7,8]]})
xdf['nor_iv'] = ['True','False']
xdf = xdf.apply(pd.Series.explode)
xdf['v'] = xdf['v'].astype(int)
xdf['i'] = xdf['v'].astype(int)
sns.relplot(data=xdf,x="v", y="i",col='nor_iv', hue='nor_iv',
             markers=True,kind='line',
                       palette=hue_colors,height=4, aspect=1.5)
plt.show()

enter image description here

And if you want rows, then:

import matplotlib.pyplot as plt
import seaborn as sns
xdf = pd.DataFrame({'v':[[1,2,10,20],[3,4]],'i':[[5,6,50,60],[7,8]]})
xdf['nor_iv'] = ['True','False']
xdf = xdf.apply(pd.Series.explode)
xdf['v'] = xdf['v'].astype(int)
xdf['i'] = xdf['v'].astype(int)
sns.relplot(data=xdf,x="v", y="i",row='nor_iv', hue='nor_iv',
             markers=True,kind='line',
                       palette=hue_colors,height=4, aspect=1.5)
plt.show()

enter image description here

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

3 Comments

A quick question: my original data frame has the lists as string, example, xdf['v'].loc[0] = '1,2,10,20'. So, how do I convert it to a list?
@Mainland xdf['v'] = xdf['v'].str.split(',')
Thanks a ton. It is resolved. You have no idea the kind of impact this plot is going to have on our project. Thanks a lot again for spending your time to help me.

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.