python - Plotly:如何更改每个子图的背景颜色?
问题描述
我正在尝试创建不同的子图,并且我希望每个子图具有不同的背景颜色。我正在使用情节,这使事情变得更加困难。任何想法?我认为 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
)
解决方案
不幸的是,您似乎仍然不能直接为不同的子图设置不同的背景颜色:
背景颜色在图形的布局中设置:plot_bgcolor='rgb(245,245,240)'。目前您无法更改特定子图的背景。
但是您可以通过引用 x 轴到xref=x
和xref=x2
infig.update_layout(shapes=dict())
。如果你确保调整一些其他参数以及零线,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()
推荐阅读
- java - 从 ComboBox 中选择值时如何处理 for 循环的大小变化
- mysql - Laravel mysql if else 条件
- c - 为什么指向结构的指针的指针在以下代码中有效,但在单个指针中无效
- python - 来自未来的 Python3 / str 和 string_types 中等效的 Basestring,六个不起作用
- jquery - JQuery - 防止提交时刷新页面
- r - 从 JPEG 创建 Spark 对象并在非翻译函数上使用 spark_apply()
- node.js - 缓存静态资产的最佳算法
- angular - 方法中的 browser.wait 是否意味着该方法需要异步..?
- google-cloud-firestore - firestore.rules 正则表达式中的计算错误
- c - 一元“*”的无效类型参数(有“双”)