📌  相关文章
📜  如何在 Plotly Express 中隐藏颜色条和图例?

📅  最后修改于: 2022-05-13 01:54:57.162000             🧑  作者: Mango

如何在 Plotly Express 中隐藏颜色条和图例?

在本文中,我们将学习如何在 plotly express 中隐藏颜色条和图例。

在这里,我们将讨论隐藏颜色条和图例的两种不同方法,使用不同的示例使其更加清晰。

示例 1:

在此示例中,我们借助方法fig.update_coloraxes(showscale=False)将 showscale 参数传递为 False,在 Plotly Express 中隐藏颜色条

Python3
# importing packages
import plotly.express as px
  
# using the gapminder dataset
data = px.data.gapminder()
data_canada = data[data.country == 'Canada']
  
# plotting the bar chart
fig = px.scatter(data_canada, x='year', y='pop',
             hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
             labels={'pop':'population of Canada'}, height=400, title="Geeksforgeeks")
  
# hiding color-bar 
fig.update_coloraxes(showscale=False)
  
fig.show()


Python3
#importing packages
import plotly.express as px
  
# using medals_wide dataset
wide_df = px.data.medals_wide()
  
# plotting the bar chart
fig = px.histogram(wide_df, x="nation", y=["gold", "silver", "bronze"], title="Geeksforgeeks")
  
# hiding legend 
fig.update_traces(showlegend=False)
  
fig.show()


Python3
# imports
import plotly.express as px
  
# using elections dataset
df = px.data.election()
  
# figure setup
fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", hover_name="district", 
    color="total", size="total", size_max=15, symbol ='Coderre',
    color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"}, title="Geeksforgeeks"
    )
  
# move colorbar
fig.update_layout(coloraxis_colorbar=dict(yanchor="top", y=1, x=0,
                                          ticks="outside",
                                          ticksuffix=" bills"))
# hiding legend
fig.update(layout_showlegend=False)
  
# hiding color-bar
fig.update(layout_coloraxis_showscale=False)
  
fig.show()


输出:

隐藏之前

隐藏后

示例 2:

在此示例中,我们借助方法fig.update_traces(showlegend=False)将 showlegend 参数传递为 False,在 Plotly Express 中隐藏图例

Python3

#importing packages
import plotly.express as px
  
# using medals_wide dataset
wide_df = px.data.medals_wide()
  
# plotting the bar chart
fig = px.histogram(wide_df, x="nation", y=["gold", "silver", "bronze"], title="Geeksforgeeks")
  
# hiding legend 
fig.update_traces(showlegend=False)
  
fig.show()

输出:

隐藏前

隐藏后

示例 3:

在这个例子中,我们在fig.update(layout_showlegend=False)fig.update(layout_coloraxis_showscale=False)方法的帮助下同时在 Plotly Express 中隐藏图例和颜色比例,通过将参数传递为 False每个案例。

Python3

# imports
import plotly.express as px
  
# using elections dataset
df = px.data.election()
  
# figure setup
fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", hover_name="district", 
    color="total", size="total", size_max=15, symbol ='Coderre',
    color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"}, title="Geeksforgeeks"
    )
  
# move colorbar
fig.update_layout(coloraxis_colorbar=dict(yanchor="top", y=1, x=0,
                                          ticks="outside",
                                          ticksuffix=" bills"))
# hiding legend
fig.update(layout_showlegend=False)
  
# hiding color-bar
fig.update(layout_coloraxis_showscale=False)
  
fig.show()

输出:

隐藏前

隐藏后