【问题标题】:Plotly: How to change background color of a plotly express legend?Plotly:如何更改情节表达图例的背景颜色?
【发布时间】:2020-10-12 23:52:58
【问题描述】:

注释掉的行是我最好的猜测。

    def _make_json(self, given_panda: pd.DataFrame, sort_by: str) -> str:
        figure = px.scatter_mapbox(given_panda,
                                   hover_name='City',
                                   hover_data=['State', 'Average_Low', 'Average_High', 'Latitude', 'Longitude', 'Record_Low', 'Record_High', 'Wind', 'Elevation', 'Humidity', 'Total_Precip'],
                                   color=sort_by,
                                   color_continuous_scale=px.colors.sequential.Plasma,
                                   zoom=3.4,
                                   opacity=1,
                                   lat='Latitude',
                                   lon='Longitude',
                                   center={'lat': 37.0902, 'lon': -95.7129},
                                   mapbox_style='carto-darkmatter')
        figure.update_layout(margin = {'r':0,'t':0,'l':0,'b':0})
        #figure.update(layout = dict(legend = dict(bgcolor = 'red')))
        return figure.to_json()

这里有一些可能会有所帮助的东西 https://plotly.com/python/reference/#layout-showlegend

【问题讨论】:

    标签: python plotly legend background-color plotly-express


    【解决方案1】:

    你已经很接近了。只需使用:

    fig.update_layout(legend = dict(bgcolor = 'yellow'))
    

    你会得到:

    完整代码:

    import plotly.express as px
    
    df = px.data.gapminder().query("continent=='Oceania'")
    fig = px.line(df, x="year", y="lifeExp", color='country')
    
    fig.update_layout(showlegend=True)
    fig.update_layout(legend = dict(bgcolor = 'yellow'))
    
    fig.show()
    

    【讨论】:

      【解决方案2】:

      根据你的填写值

      import plotly.express as px
      tips = px.data.tips()
      fig = px.bar(tips, x='sex', y='total_bill',
                   color='smoker', barmode='group')
      
      fig.update_layout({
      'plot_bgcolor': 'rgba(0, 0, 0, 0)',
      'paper_bgcolor': 'rgba(0, 0, 0, 0)',
      })
      
      fig.show()
      

      【讨论】:

        猜你喜欢
        • 2023-02-23
        • 2020-08-21
        • 2022-10-13
        • 2015-12-25
        • 2022-11-04
        • 2019-06-29
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        相关资源
        最近更新 更多