42

Plotly Express allows me to easily plot a pandas dataframe, as explained in their examples. Rather than using a named column for x and a named column for y, I would like to use the dataframe's index for x and a named column for y.

Example using named columns

import plotly.express as px
iris = px.data.iris()
fig = px.scatter(iris, x="sepal_width", y="sepal_length")
fig.show()

What i want (bogus example)

import plotly.express as px
iris = px.data.iris()
fig = px.scatter(iris, x="index", y="sepal_length")
fig.show()

This obviously throws:

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species', 'species_id'] but received: index

Ugly fix

import plotly.express as px
iris = px.data.iris().reset_index()
fig = px.scatter(iris, x="index", y="sepal_length")
fig.show()
Laurens Koppenol
  • 2,946
  • 2
  • 20
  • 33
  • 8
    Your 'ugly fix' is the recommended approach today :) – nicolaskruchten Jul 24 '19 at 15:12
  • @nicolaskruchten With my own data (pandaframes resuting from some web scraping) I found the "ugly fix" to be the way to go, too. Hence, I'd suggest making your answer (together with a link to some official documentation (I found nothing on that case)) the official answer. – StefanQ Aug 25 '19 at 10:21
  • 3
    @StefanQ I'm hoping to actually add support for this kind of thing in the next few weeks :) – nicolaskruchten Aug 25 '19 at 18:32
  • Feel free to post your above remarks as answer and I will mark it accordingly. Makes it easy for you to keep the community up to date! – Laurens Koppenol Aug 26 '19 at 10:42

3 Answers3

55

Reference: https://plot.ly/python/px-arguments/#using-the-index-of-a-dataframe

You can pass the index as reference explicitly.

So in your case, this would be:

import plotly.express as px
iris = px.data.iris()
fig = px.scatter(iris, x=iris.index, y="sepal_length")
fig.show()

--

BONUS QUESTION: what if iris has a pd.MultiIndex?

Use pd.MultiIndex.get_level_values.

import plotly.express as px

# dummy example for multiindex
iris = px.data.iris().set_index(['species', 'species_id', iris.index])

fig = px.scatter(
   iris, 
   x=iris.index.get_level_values(2), 
   y="sepal_length"
)

fig.show()
syltruong
  • 2,563
  • 20
  • 33
  • 3
    Does this also work with a DatetimeIndex? I am having some difficulties with this issue at the moment – Eulenfuchswiesel May 13 '20 at 10:29
  • Note that this geature has been added somewhere after plotly version 4.1 (had to update for this to work). – soungalo Jul 04 '20 at 17:29
  • 1
    This results in the x-axis label being "x". Is there a way to assign so that x-axis uses the label of the index being used? – Abhishek Sourabh Feb 15 '23 at 20:27
  • The example in the comment would use the name of the index as x-axis label on the plot. You can try this behaviour with: ``` import plotly.express as px iris = px.data.iris() iris.index.rename("my_custom_index", inplace=True) fig = px.scatter(iris, x=iris.index, y="sepal_length") fig.show() ``` – syltruong Feb 16 '23 at 08:39
8

You can simply leave it blank, like so:

import plotly.express as px
iris = px.data.iris()
fig = px.scatter(iris, y="sepal_length")
fig.show()
PassingThrough
  • 129
  • 1
  • 2
-2

You can only pass the column name at x & y parameters of px.scatter(). Seems there is no column named "index" in the iris dataset.

vestland
  • 55,229
  • 37
  • 187
  • 305