如何使用 plotly express 向折线图添加点或标记?

Posted

技术标签:

【中文标题】如何使用 plotly express 向折线图添加点或标记?【英文标题】:How to add points or markers to line chart using plotly express? 【发布时间】:2020-02-26 11:03:10 【问题描述】:

plotly.express 非常方便生成漂亮的交互式绘图。下面的代码生成按国家/地区着色的折线图。现在我需要在情节中添加点。有谁知道如何在折线图中添加点?

import plotly.express as px

gapminder = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(gapminder, x="year", y="lifeExp", color='country')
fig.show()

【问题讨论】:

【参考方案1】:

更新:

5.2.1 版本开始,您可以在以下位置使用markers=True

px.line(df, x='year', y='lifeExp', color='country', markers=True)

旧版本的上一个答案:

使用fig.update_traces(mode='markers+lines')

剧情:

代码:

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()

【讨论】:

【参考方案2】:

从 Plotly 版本 5.2.1 开始,现在可以使用 px.linemarkers 参数来实现。即

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()

【讨论】:

以上是关于如何使用 plotly express 向折线图添加点或标记?的主要内容,如果未能解决你的问题,请参考以下文章