Skip to content
Advertisement

Select plot linestyle with plotly pandas backend

I want to be able to select the linestyle with the pandas plot method with the plotly backend.

Matplotlib:

When I use the matplotlib backend in pandas, I can do:

pd.options.plotting.backend = "matplotlib"
df = pd.DataFrame({"a": [1,2,3,4], "b": [4,3,1,2]})
df.plot(style={"a":"--", "b":"-"}) 

which allows me to select the linestyle for each column. The output is:

enter image description here

Plotly backend:

With plotly I can do

pd.options.plotting.backend = "plotly"
df = pd.DataFrame({"a": [1,2,3,4], "b": [4,3,1,2]})
df.plot()

enter image description here

How can I select the linestyle of a given line (or even a single line), with the plotly backend?

Advertisement

Answer

In your example just use:

fig = df.plot()
fig.data[0].line.dash = 'dash'

And you’ll get:

enter image description here

Other options are:

['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']

Complete code:

import pandas as pd
pd.options.plotting.backend = "plotly"
df = pd.DataFrame({"a": [1,2,3,4], "b": [4,3,1,2]})
fig = df.plot()
fig.data[0].line.dash = 'dash'
fig.show()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement