How to add points or markers to line chart using plotly express?
Question:
plotly.express is very convenient to produce nice interactive plots. The code below generates a line chart colored by country. Now what I need is to add points to the plot. Does anyone know how I can add points to the line chart?
import plotly.express as px
gapminder = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(gapminder, x="year", y="lifeExp", color='country')
fig.show()
Answers:
Update:
As of version 5.2.1
you can use markers=True
in:
px.line(df, x='year', y='lifeExp', color='country', markers=True)
Previous answer for older versions:
Use fig.update_traces(mode='markers+lines')
Plot:
Code:
import plotly.express as px
gapminder = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(gapminder, x="year", y="lifeExp", color='country')
fig.update_traces(mode='markers+lines')
fig.show()
As of Plotly version 5.2.1 this can now be achieved using the markers
argument of px.line
. I.e.
import plotly.express as px
gapminder = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(gapminder, x="year", y="lifeExp", color='country', markers=True)
fig.show()
If there is a way to specify marker?
For example, using seaborn I could pass a list of symbols as a parameter and gets a specific marker for each line:
Using fig = sns.lineplot(data=data, markers=[",", "*", "X", "<", "^", "."])
I will receive this picture: enter image description here
Thank you for your answers!
plotly.express is very convenient to produce nice interactive plots. The code below generates a line chart colored by country. Now what I need is to add points to the plot. Does anyone know how I can add points to the line chart?
import plotly.express as px
gapminder = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(gapminder, x="year", y="lifeExp", color='country')
fig.show()
Update:
As of version 5.2.1
you can use markers=True
in:
px.line(df, x='year', y='lifeExp', color='country', markers=True)
Previous answer for older versions:
Use fig.update_traces(mode='markers+lines')
Plot:
Code:
import plotly.express as px
gapminder = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(gapminder, x="year", y="lifeExp", color='country')
fig.update_traces(mode='markers+lines')
fig.show()
As of Plotly version 5.2.1 this can now be achieved using the markers
argument of px.line
. I.e.
import plotly.express as px
gapminder = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(gapminder, x="year", y="lifeExp", color='country', markers=True)
fig.show()
If there is a way to specify marker?
For example, using seaborn I could pass a list of symbols as a parameter and gets a specific marker for each line:
Using fig = sns.lineplot(data=data, markers=[",", "*", "X", "<", "^", "."])
I will receive this picture: enter image description here
Thank you for your answers!