【问题标题】:Change colors in python dash plotly theme在 python dash plotly 主题中更改颜色
【发布时间】:2020-08-07 22:39:05
【问题描述】:

我目前正在创建我的第一个 plotly dash 应用程序,并且有一个关于图形主题的问题:

我想使用可用的plotly_dark 主题,并且只将基础颜色(背景和元素颜色)调整为自定义值。我玩弄了这里提供的想法:https://plotly.com/python/templates/#saving-and-distributing-custom-themes like this

import plotly.graph_objects as go
import plotly.io as pio

pio.templates["plotly_dark_custom"] = go.layout.Template(
    ...custom definitions here...
)
pio.templates.default = "plotly_dark_custom"

但我想知道是否有更直观的方法,例如从plotly_dark 构建一个儿童主题 然后只覆盖颜色(为颜色提供调色板并定义新的背景颜色)。

由于我对 Python 很陌生,所以我在这里的知识非常有限,所以我希望你能给我一些正确方向的指点。

谢谢,如果我应该提供有关此请求的更多详细信息,请告诉我。 斯蒂芬

【问题讨论】:

    标签: python plotly-dash


    【解决方案1】:

    如果这对其他人有帮助,我设法得到我想要的东西:

    1. plotly_dark 获取设置(您只需执行一次)
    2. 将自定义模板设置为暗色
    3. 根据 1 的输出,根据您的需要更新自定义模板。

    import plotly.graph_objects as go
    import plotly.io as pio
    
    plotly_template = pio.templates["plotly_dark"]
    print (plotly_template)
    
    pio.templates["plotly_dark_custom"] = pio.templates["plotly_dark"]
    
    pio.templates["plotly_dark_custom"].update({
    #e.g. you want to change the background to transparent
    'paper_bgcolor': 'rgba(0,0,0,0)',
    'plot_bgcolor': 'rgba(0,0,0,0)'
    })
    

    很可能不是最优雅的解决方案,但它可以解决问题。 斯蒂芬

    【讨论】:

      【解决方案2】:

      运行 Stephan 的答案时出现错误,所以我想我会发布最终对我有用的内容。

      # this helps us get the theme settings
      import plotly.io as plt_io
      
      # this is for simple plotting with plotly express
      import plotly.express as px
      
      # create our custom_dark theme from the plotly_dark template
      plt_io.templates["custom_dark"] = plt_io.templates["plotly_dark"]
      
      # set the paper_bgcolor and the plot_bgcolor to a new color
      plt_io.templates["custom_dark"]['layout']['paper_bgcolor'] = '#30404D'
      plt_io.templates["custom_dark"]['layout']['plot_bgcolor'] = '#30404D'
      
      # you may also want to change gridline colors if you are modifying background
      plt_io.templates['custom_dark']['layout']['yaxis']['gridcolor'] = '#4f687d'
      plt_io.templates['custom_dark']['layout']['xaxis']['gridcolor'] = '#4f687d'
      
      # load an example dataset to test
      df = px.data.iris()
      
      # create a nice default plotly example figure
      fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
                       size='petal_length', hover_data=['petal_width'])
      
      # set the template to our custom_dark template
      fig.layout.template = 'custom_dark'
      
      # and voila, we have a modified dark mode figure
      fig.show()
      

      【讨论】:

        【解决方案3】:

        当您使用 update_layout() 方法更改现有模板设置(仅指定的那些)时,显然会设法覆盖现有模板设置。

        fig1.update_layout(
            plot_bgcolor='rgb(17,17,17)',
            paper_bgcolor ='rgb(10,10,10)')
        

        我只是这样做了,它完成了工作,访问模板没有意义:)

        【讨论】:

        • 是的,继续拯救生命。谢谢
        【解决方案4】:

        这个解决方案非常干净 IMO:

        import plotly.io as pio
        import plotly.graph_objects as go
        
        pio.templates['custom'] = go.layout.Template(
            layout_paper_bgcolor='rgba(0,0,0,0)',
            layout_plot_bgcolor='rgba(0,0,0,0)'
            )
        pio.templates.default = 'plotly+custom'
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-11-13
          • 1970-01-01
          • 2020-08-16
          • 2020-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-04
          相关资源
          最近更新 更多