Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Could not interpret input 'index' when using index with seaborn lineplot

Tags:

I want the use the index of a pandas DataFrame as x value for a seaborn plot. However, this raises a value error.

A small test example:

import pandas as pd import seaborn as sns sns.lineplot(x='index',y='test',hue='test2',data=pd.DataFrame({'test':range(9),'test2':range(9)})) 

It raises:

ValueError: Could not interpret input 'index' 

Is it not possible to use the index as x values? What am I doing wrong? Python 2.7, seaborn 0.9

like image 572
MaxS Avatar asked Sep 10 '18 11:09

MaxS


People also ask

What is Factorplot in Seaborn?

Factor Plot is used to draw a different types of categorical plot . The default plot that is shown is a point plot, but we can plot other seaborn categorical plots by using of kind parameter, like box plots, violin plots, bar plots, or strip plots.

How do I index a column in pandas?

In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.

What is the use of Seaborn in Python?

Seaborn is a library for making statistical graphics in Python. It builds on top of matplotlib and integrates closely with pandas data structures. Seaborn helps you explore and understand your data.

How do I add a legend to my Seaborn plot?

By default, seaborn automatically adds a legend to the graph. Notice the legend is at the top right corner. If we want to explicitly add a legend, we can use the legend() function from the matplotlib library. In this way, we can add our own labels explicitly.


2 Answers

I would rather prefer to use it this way. You need to remove hue as I assume it has a different purpose which doesn't apply in your current DataFrame because you have a single line. Visit the official docs here for more info.

df=pd.DataFrame({'test':range(9),'test2':range(9)}) sns.lineplot(x=df.index, y='test', data=df) 

Output

enter image description here

like image 193
Sheldore Avatar answered Sep 20 '22 19:09

Sheldore


You would need to make sure the string you provide to the x argument is actually a column in your dataframe. The easiest solution to achieve that is to reset the index of the dataframe to convert the index to a column.

sns.lineplot(x='index', y='test', data=pd.DataFrame({'test':range(9),'test2':range(9)}).reset_index()) 
like image 27
ImportanceOfBeingErnest Avatar answered Sep 20 '22 19:09

ImportanceOfBeingErnest