1

I have a dataframe called Prices1, which I have pasted below. I would like to plot all the columns against the index in seaborn.

Please note that in this example I have four columns but in other problems I will have different number of columns.

    AMZN    BABA    COST    WMT
716 0.1256  1.62168 1.1572  1.35279
717 0.117824    1.6236  1.19329 1.36988
718 0.120359    1.65928 1.2042  1.38496
719 0.119176    1.64181 1.18629 1.39703
720 0.123233    1.67813 1.20476 1.36385

I have tried to use this but I keep getting an error:


sns.lineplot(data= Prices1.iloc[:, :2])

The error is as follows:

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-123-d006d5480255> in <module>
----> 1 sns.lineplot(data= df.iloc[:, :2])

~/opt/anaconda3/lib/python3.8/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

~/opt/anaconda3/lib/python3.8/site-packages/seaborn/relational.py in lineplot(x, y, hue, size, style, data, palette, hue_order, hue_norm, sizes, size_order, size_norm, dashes, markers, style_order, units, estimator, ci, n_boot, seed, sort, err_style, err_kws, legend, ax, **kwargs)
    694     p._attach(ax)
    695 
--> 696     p.plot(ax, kwargs)
    697     return ax
    698 

~/opt/anaconda3/lib/python3.8/site-packages/seaborn/relational.py in plot(self, ax, kws)
    469         # Loop over the semantic subsets and add to the plot
    470         grouping_vars = "hue", "size", "style"
--> 471         for sub_vars, sub_data in self.iter_data(grouping_vars, from_comp_data=True):
    472 
    473             if self.sort:

~/opt/anaconda3/lib/python3.8/site-packages/seaborn/_core.py in iter_data(self, grouping_vars, reverse, from_comp_data)
    979                 grouping_keys.append(self.var_levels.get(var, []))
    980 
--> 981             iter_keys = itertools.product(*grouping_keys)
    982             if reverse:
    983                 iter_keys = reversed(list(iter_keys))

TypeError: 'NoneType' object is not iterable

Any suggestions? Thank you in advance.

Zephyr
  • 11,891
  • 53
  • 45
  • 80
DSG
  • 13
  • 5

1 Answers1

0

You should check the format of data in your Prices1 dataframe with:

print(Prices1.info())

You should something like:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 716 to 720
Data columns (total 4 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   AMZN    5 non-null      float64
 1   BABA    5 non-null      float64
 2   COST    5 non-null      float64
 3   WMT     5 non-null      float64
dtypes: float64(4)
memory usage: 200.0 bytes
None

Pay attention to Dtype column: all of your columns should be float64 type. Otherwise those values are not recognized by seaborn as numbers.
If that is the case, you can convert columns type with:

Prices1 = Prices1.apply(pd.to_numeric)

Then you can plot data trends with:

sns.lineplot(data = Prices1)

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80
  • 1
    Big help. So detailed explaination. Also Thank You for showing me how to convert data from object to float! – DSG Aug 11 '21 at 05:36