【问题标题】:Plotly: How to change the background color of each subplot?Plotly:如何更改每个子图的背景颜色?
【发布时间】:2020-08-21 04:21:21
【问题描述】:

我正在尝试创建不同的子图,并且我希望每个子图具有不同的背景颜色。我正在使用情节,这使事情变得更加困难。任何想法?我认为 matplot 中的等价物是 face_color 或类似的东西。

fig = make_subplots(rows=1, cols=2)

fig.add_trace(
    go.Scatter(
        x=list(range(sample_points)),
        y=data_gx.iloc[scene],
        name='X-axis',
        line=dict(color='green', width=2)
    ),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(
        x=list(range(sample_points)),
        y=data_ax.iloc[scene],
        name='X-axis',
        line=dict(color='green', width=2)
    ),
    row=1, col=2
)

【问题讨论】:

  • 如果您想在本地运行代码并检查输出,请使用这两个图表更改 x 和 y,以便查看一些输出。 """ x=list(range(10)), y=list(range(10)), """

标签: python plotly background-color


【解决方案1】:

试试:

fig.update_layout(plot_bgcolor = "white")

fig.show()

【讨论】:

    【解决方案2】:

    我认为您可以使用 paper_bgcolor 和 plot_bgcolor 布局选项轻松更改背景。

    这是很久以前发布的一个相关问题:Python -- Setting Background color to transparent in Plotly plots

    【讨论】:

      【解决方案3】:

      不幸的是,您似乎仍然不能直接为不同的子图设置different background colors:

      在布局中为图形设置背景颜色: plot_bgcolor='rgb(245,245,240)'。目前您无法更改 特定子图的背景。

      但是您可以通过在fig.update_layout(shapes=dict()) 中通过xref=x 和xref=x2 引用两个x 轴,在两个子图上随意放置形状。如果你确保调整其他一些参数,比如 zeroline 和 layer='below',你会得到一个非常好的结果:

      完整代码:

      # imports
      from plotly.subplots import make_subplots
      import plotly.graph_objects as go
      import pandas as pd
      import numpy as np
      
      # data
      df = pd.DataFrame({'Index': {0: 1.0,
                                    1: 2.0,
                                    2: 3.0,
                                    3: 4.0,
                                    4: 5.0,
                                    5: 6.0,
                                    6: 7.0,
                                    7: 8.0,
                                    8: 9.0,
                                    9: 10.0},
                                   'A': {0: 15.0,
                                    1: 6.0,
                                    2: 5.0,
                                    3: 4.0,
                                    4: 3.0,
                                    5: 2.0,
                                    6: 1.0,
                                    7: 0.5,
                                    8: 0.3,
                                    9: 0.1},
                                   'B': {0: 1.0,
                                    1: 4.0,
                                    2: 2.0,
                                    3: 5.0,
                                    4: 4.0,
                                    5: 6.0,
                                    6: 7.0,
                                    7: 2.0,
                                    8: 8.0,
                                    9: 1.0},
                                   'C': {0: 12.0,
                                    1: 6.0,
                                    2: 5.0,
                                    3: 4.0,
                                    4: 3.0,
                                    5: 2.0,
                                    6: 1.0,
                                    7: 0.5,
                                    8: 0.2,
                                    9: 0.1}})
      # set up plotly figure
      fig = make_subplots(1,2)
      
      # add first bar trace at row = 1, col = 1
      fig.add_trace(go.Bar(x=df['Index'], y=df['A'],
                           name='A',
                           marker_color = 'green',
                           opacity=0.4,
                           marker_line_color='rgba(0,0,0,0)',
                           marker_line_width=2),
                    row = 1, col = 1)
      
      # add first scatter trace at row = 1, col = 1
      fig.add_trace(go.Scatter(x=df['Index'], y=df['B'], line=dict(color='red'), name='B'),
                    row = 1, col = 1)
      
      # add first bar trace at row = 1, col = 2
      fig.add_trace(go.Bar(x=df['Index'], y=df['C'],
                           name='C',
                           marker_color = 'green',
                           opacity=0.4,
                           marker_line_color='rgba(0,0,0,0)',
                          marker_line_width=2),
                    row = 1, col = 2)
      
      fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')
      
      # Define gridlines and zerolines
      # => an invisible zerolines looks better
      # in this scenario
      fig.update_layout(xaxis=dict(showgrid=False, zeroline=False),
                        yaxis=dict(showgrid=True, zeroline=False),
                        xaxis2=dict(showgrid=False, zeroline=False),
                        yaxis2=dict(showgrid=True, zeroline=False),
      
      )
      
      fig.update_layout(
          shapes=[
              # 1st highlight during Feb 4 - Feb 6
       dict(
                  type="rect",
                  xref="x",
                  yref="paper",
                  x0=df['Index'].iloc[0]-1,
                  y0=-0.001,
                  x1=df.index[-1]+2,
                  y1=1,
                  fillcolor="steelblue",
                  opacity=0.5,
                  layer="below",
                  line_width=0,
              ),
              # 2nd highlight during Feb 20 - Feb 23
              dict(
                  type="rect",
                  xref="x2",
                  yref="paper",
                  x0=df['Index'].iloc[0]-1,
                  y0=-0.001,
                  x1=df.index[-1]+2,
                  y1=1,
                  fillcolor="firebrick",
                  opacity=0.5,
                  layer="below",
                  line_width=0,
              )
          ]
      )
      
      fig.show()
      

      【讨论】:

      • 这大概是我想要做的,但我不想拥有相同的颜色背景,而是希望每个情节都有不同的背景。对于第一个浅蓝色,第二个红色或其他东西。谢谢
      【解决方案4】:
      fig.add_trace(go.Scatter(x=[-1,2],y= [2,2],fill='tozeroy'),row=1, col=1)
      

      这对我有用。这将绘制一条从 (-1,2) 到 (2,2) 的线,并将为该点以下的所有内容着色。只需添加子图的行和列,它就可以帮助您为每个子图的背景着色。它对我有用,我希望它也对你有用。

      【讨论】:

      • 这也有效。但是现在您必须确保用于提供背景颜色的轨迹不会覆盖任何 other 轨迹。使用shapes,您可以选择将展示位置指定为layer='below',以防止这种情况发生。
      猜你喜欢
      • 2020-10-12
      • 2018-06-11
      • 2020-05-08
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 2010-09-14
      相关资源
      最近更新 更多