Imagine I have lines A, B, C, D, and E. I want lines A, B, and C to appear on the plotly line chart. I want the user to have the option to add lines D and E but D and E should be hidden by default.
Any suggestions on how to do this?
Example, how would I hide Australia by default.
import plotly.express as px
df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color='country')
fig.show()
You need to play with the parameter visible setting it as legendonly within every trace
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()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With