如何设置 Plotly 两个包含热图的水平子图,以便它们不共享色标?
How to setup Plotly two horizontal subplots containing heatmaps so that they don't share a colorscale?
我有两个热图。它们的值在完全不同的范围内,因此不应共享色标。我尝试创建一个带有两个子图(水平排列)的绘图图,并将我的每个热图放入其中一个子图中。但是,这两个热图在右侧热图的右侧共享相同的色标。色标显示两组标签相互重叠。我想看到的是有两个色标,一个在每个热图的右边。
我的代码如下。有人在这里看到问题吗?
fig = plotly.tools.make_subplots(rows=1, cols=2)
fig.append_trace(mm, 1, 1)
fig.append_trace(sm, 1, 2)
plotly.offline.iplot(fig)
我已尝试将 share_xaxes
和 share_yaxes
明确设置为 False
。没有帮助。
您可以明确指定颜色条的 x
position,请参见下面的示例。
笔记本里的图更好看,也许应该把右图也移一下?
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
plotly.offline.init_notebook_mode()
mm = go.Heatmap(
z=[[1, 20, 30],
[20, 1, 60],
[30, 60, 1]],
colorbar = dict(x=0.45),
colorscale='Viridis'
)
sm = go.Heatmap(
z=[[1, 2, 3],
[2, 1, 6],
[3, 6, 1]]
)
fig = plotly.tools.make_subplots(rows=1, cols=2)
fig.append_trace(mm, 1, 1)
fig.append_trace(sm, 1, 2)
plotly.offline.iplot(fig)
我找到了这个解决方案:对于 3 个热图,我创建了一个由 6 个框组成的网格,以便手动将每个热图的颜色条放在一边:
fig = make_subplots(rows=1, cols=6,
subplot_titles=("Web", "Magasin", "Email"),
specs=[[{}, None, {}, None, {}, None]],
shared_yaxes=True)
然后我通过定义每个颜色条来绘制热图:
fig.add_trace(go.Heatmap(
z=df_web.values,
x=df_web.columns.tolist(),
y=df_web.index.tolist(),
colorscale='RdBu',
reversescale=True,
colorbar = dict(x=0.27, title='Nb moyen', thickness=15)), row=1, col=1
)
fig.add_trace(go.Heatmap(
z=df_mag.values,
x=df_mag.columns.tolist(),
y=df_mag.index.tolist(),
colorscale='RdBu',
reversescale=True,
colorbar = dict(x=0.62, title='Nb moyen', thickness=15)), row=1, col=3
)
fig.add_trace(go.Heatmap(
z=df_email.values,
x=df_email.columns.tolist(),
y=df_email.index.tolist(),
colorscale='RdBu',
reversescale=True,
colorbar = dict(x=0.97, title='Nb moyen', thickness=15)), row=1, col=5
)
最后我定义了职位:
fig.update_layout(title_text='Répartition par jour et heure', title_x=0.5,
height=250, margin=dict(l=0,r=0,b=50,t=25),
xaxis=dict(
domain=[0, 0.27]
),
xaxis2=dict(
domain=[0.35, 0.62]
),
xaxis3=dict(
domain=[0.7, 0.97]
))
结果:
我有两个热图。它们的值在完全不同的范围内,因此不应共享色标。我尝试创建一个带有两个子图(水平排列)的绘图图,并将我的每个热图放入其中一个子图中。但是,这两个热图在右侧热图的右侧共享相同的色标。色标显示两组标签相互重叠。我想看到的是有两个色标,一个在每个热图的右边。
我的代码如下。有人在这里看到问题吗?
fig = plotly.tools.make_subplots(rows=1, cols=2)
fig.append_trace(mm, 1, 1)
fig.append_trace(sm, 1, 2)
plotly.offline.iplot(fig)
我已尝试将 share_xaxes
和 share_yaxes
明确设置为 False
。没有帮助。
您可以明确指定颜色条的 x
position,请参见下面的示例。
笔记本里的图更好看,也许应该把右图也移一下?
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
plotly.offline.init_notebook_mode()
mm = go.Heatmap(
z=[[1, 20, 30],
[20, 1, 60],
[30, 60, 1]],
colorbar = dict(x=0.45),
colorscale='Viridis'
)
sm = go.Heatmap(
z=[[1, 2, 3],
[2, 1, 6],
[3, 6, 1]]
)
fig = plotly.tools.make_subplots(rows=1, cols=2)
fig.append_trace(mm, 1, 1)
fig.append_trace(sm, 1, 2)
plotly.offline.iplot(fig)
我找到了这个解决方案:对于 3 个热图,我创建了一个由 6 个框组成的网格,以便手动将每个热图的颜色条放在一边:
fig = make_subplots(rows=1, cols=6,
subplot_titles=("Web", "Magasin", "Email"),
specs=[[{}, None, {}, None, {}, None]],
shared_yaxes=True)
然后我通过定义每个颜色条来绘制热图:
fig.add_trace(go.Heatmap(
z=df_web.values,
x=df_web.columns.tolist(),
y=df_web.index.tolist(),
colorscale='RdBu',
reversescale=True,
colorbar = dict(x=0.27, title='Nb moyen', thickness=15)), row=1, col=1
)
fig.add_trace(go.Heatmap(
z=df_mag.values,
x=df_mag.columns.tolist(),
y=df_mag.index.tolist(),
colorscale='RdBu',
reversescale=True,
colorbar = dict(x=0.62, title='Nb moyen', thickness=15)), row=1, col=3
)
fig.add_trace(go.Heatmap(
z=df_email.values,
x=df_email.columns.tolist(),
y=df_email.index.tolist(),
colorscale='RdBu',
reversescale=True,
colorbar = dict(x=0.97, title='Nb moyen', thickness=15)), row=1, col=5
)
最后我定义了职位:
fig.update_layout(title_text='Répartition par jour et heure', title_x=0.5,
height=250, margin=dict(l=0,r=0,b=50,t=25),
xaxis=dict(
domain=[0, 0.27]
),
xaxis2=dict(
domain=[0.35, 0.62]
),
xaxis3=dict(
domain=[0.7, 0.97]
))
结果: