【问题标题】:Hide a line on plotly line graph在 plotly 折线图上隐藏一条线
【发布时间】:2020-09-08 23:28:31
【问题描述】:

假设我有线 A、B、C、D 和 E。我希望线 A、B 和 C 出现在曲线图上。我希望用户可以选择添加行 D 和 E,但默认情况下应该隐藏 D 和 E。

关于如何做到这一点的任何建议?

例如,我如何默认隐藏澳大利亚。

import plotly.express as px

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

【问题讨论】:

  • 你需要提供你目前拥有的代码
  • 已更新以包含示例

标签: python pandas plotly


【解决方案1】:

您需要在每个跟踪中使用参数visible 将其设置为legendonly

import plotly.express as px
countries_to_hide = ["Australia"]
df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color='country')

fig.for_each_trace(lambda trace: trace.update(visible="legendonly") 
                   if trace.name in countries_to_hide else ())
fig.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    相关资源
    最近更新 更多