24

I need to create a line chart from multiple columns of a dataframe. In pandas, you can draw a multiple line chart using a code as follows:

df.plot(x='date', y=['sessions', 'cost'], figsize=(20,10), grid=True)

How can this be done using plotly_express?

HK boy
  • 1,398
  • 11
  • 17
  • 25
Ryan
  • 1,247
  • 1
  • 10
  • 12

3 Answers3

32

With version 4.8 of Plotly.py, the code in the original question is now supported almost unmodified:

pd.options.plotting.backend = "plotly"
df.plot(x='date', y=['sessions', 'cost'])

Previous answer, as of July 2019

For this example, you could prepare the data slightly differently.

df_melt = df.melt(id_vars='date', value_vars=['sessions', 'cost'])

If you transpose/melt your columns (sessions, cost) into additional rows, then you can specify the new column 'variable' to partition by in the color parameter.

px.line(df_melt, x='date' , y='value' , color='variable')

Example plotly_express output

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101
G Hart
  • 506
  • 4
  • 5
7

With newer versions of plotly, all you need is:

df.plot()

As long as you remember to set pandas plotting backend to plotly:

pd.options.plotting.backend = "plotly"

From here you can easily adjust your plot to your liking, for example setting the theme:

df.plot(template='plotly_dark')

Plot with dark theme:

enter image description here

One particularly awesome feature with newer versions of plotly is that you no longer have to worry whether your pandas dataframe is of a wide or long format. Either way, all you need is df.plot(). Check out the details in the snippet below.

Complete code:

# imports
import plotly.express as px
import pandas as pd
import numpy as np

# settings
pd.options.plotting.backend = "plotly"

# sample dataframe of a wide format
np.random.seed(4); cols = list('abc')
X = np.random.randn(50,len(cols))  
df=pd.DataFrame(X, columns=cols)
df.iloc[0]=0; df=df.cumsum()

# plotly figure
df.plot(template = 'plotly_dark')

Answer for older versions:

I would highly suggest using iplot() instead if you'd like to use plotly in a Jupyter Notebook for example:

Plot:

enter image description here

Code:

import plotly
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import numpy as np


# setup
init_notebook_mode(connected=True)
np.random.seed(123)
cf.set_config_file(theme='pearl')

# Random data using cufflinks
df1 = cf.datagen.lines()
df2 = cf.datagen.lines()
df3 = cf.datagen.lines()
df = pd.merge(df1, df2, how='left',left_index = True, right_index = True)
df = pd.merge(df, df3, how='left',left_index = True, right_index = True)

fig = df1.iplot(asFigure=True, kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns')
iplot(fig)
vestland
  • 55,229
  • 37
  • 187
  • 305
  • 3
    Thanks vestland for your help. Cufflinks is a very helpful library for charting plotly graphs directly in the pandas dataframes. But, I'm still wondrering if there is a way to do the same one-liner code using plotly express. – Ryan Apr 30 '19 at 01:06
  • 1
    That does not work. ImportError: The plotly.plotly module is deprecated, please install the chart-studio package and use the chart_studio.plotly module instead. And then it want some login info. – Soerendip Sep 20 '19 at 20:36
  • @vestland is there any wat to control the x axis labels in this case? – Reut Oct 12 '20 at 11:48
  • @Reut Le'ts talk in [chat](https://chat.stackoverflow.com/rooms/222912/room-for-vestland-and-reut) – vestland Oct 12 '20 at 11:49
  • @Reut Just click the link – vestland Oct 12 '20 at 11:49
  • @vestland when I use `df.plot(template='plotly_dark')` I get a ValueError cannot process wide-form data with columns of different types – gansub Dec 30 '22 at 15:02
  • @vestland x axis is datetime and the y axis is float – gansub Dec 30 '22 at 15:17
2

Its also worth pointing out you can combine plotly express with graph_objs. This is a good route when the lines have different x points.

import numpy as np
import pandas as pd

import plotly.graph_objs as go
import plotly.express as px

# data set 1
x = np.linspace(0, 9, 10)
y = x

# data set 2
df = pd.DataFrame(np.column_stack([x*0.5, y]), columns=["x", "y"])

fig = go.Figure(px.scatter(df, x="x", y="y"))
fig.add_trace(go.Scatter(x=x, y=y))

fig.show()
basil_man
  • 322
  • 5
  • 14