子图中的Ploty热图:如何设置纵横比?

问题描述 投票:0回答:1

有一个类似的问题,但它只要求常规数字。相应的答案不适用于子图:

https://stackoverflow.com/a/55239488/2215205
https://stackoverflow.com/a/71579181/2215205

  1. px.imshow(df,aspect ='equal')被fig.add_trace()完全忽略。
  2. 在这方面操作图形布局仅适用于第一个子图,而不是全部:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

## Example data
df = pd.DataFrame([[0,0,0,0,0],
                   [0,0,1,0,0],
                   [0,1,2,1,0],
                   [0,0,1,0,0],
                   [0,0,0,0,0],
                  ])

## Subplots w/ go.Heatmap()
layout = dict(
    # yaxis=...,
    # xaxis=...,
    # width=plot_width,
    # height=plot_height,
    # autosize=False,
    yaxis_scaleanchor="x" ,  # <---- only works in 1st subplot :(
    )

fig = go.Figure(layout=layout).set_subplots(rows=1, cols=2, subplot_titles=['#1', '#2'])

fig.add_trace(
    go.Heatmap(
        z = df,
        coloraxis = "coloraxis",  # use a common coloraxis for all subplots
    ), 
    1,1)
fig.add_trace(
    go.Heatmap(
        z = df*2,
        coloraxis = "coloraxis",  # use a common coloraxis for all subplots
    ),
    1,2)

## Update y-axes
fig.update_yaxes(
    title_text="yaxis #1", 
    row=1, col=1, 
    scaleanchor='x',  # <---- only works in 1st subplot :(  ==> works in Colab !!
    ) 
fig.update_yaxes(
    title_text="yaxis #2",
    row=1, col=2,
    scaleanchor='x',  # <---- only works in 1st subplot :( ==> works in Colab !!
    )

## Update layout
# fig['layout']['yaxis']['scaleanchor']='x' # <--- updates only first subplot :(

fig.update_layout(
    title='2D Heatmaps',
    autosize=False,
    # yaxis_scaleanchor="x",  # <--- updates only first subplot :(
    yaxis = dict(scaleanchor = 'x'),   # <--- updates only first subplot :(
    # coloraxis_colorscale='Viridis',
    )

# fig.show()

非常感谢任何帮助。

请通过我的努力在这里找到一个Jupyter-Notebook: https://colab.research.google.com/drive/13NKNmgAXudXp62UlmXKDhVFgpFe1YeQN?usp=sharing

python plotly heatmap
1个回答
0
投票

请注意,默认情况下,每个子图都有自己的 x 轴/y 轴,除非在调用

shared_xaxes
shared_yaxes
时通过设置
set_subplots()
make_subplots()
另有指定。

第二个 x 轴

xaxis2
的定义已添加到布局中,并且可以使用字符串
'x2'
进行引用,第二个 y 轴也是如此:
'y2'
指的是
yaxis2
,它也在布局(等等)。

fig.update_layout(
    title='2D Heatmaps',
    autosize=False,
    yaxis_scaleanchor="x",      # <- updates 1st subplot :)
    yaxis2_scaleanchor="x2"     # <- updates 2nd subplot :)
)
© www.soinside.com 2019 - 2024. All rights reserved.