Python Plotly:当scaleanchor=x时,如何调整带注释热图的轴标签?

Python Plotly:当scaleanchor=x时,如何调整带注释热图的轴标签?,python,plotly,heatmap,Python,Plotly,Heatmap,当您设置scaleanchor=x时(例如,要使用ff.annotated_heatmaps制作完美的方形热图),您将得到与x轴本身有较大偏移的x轴标签,如下所示: 你如何解决这个问题? 代码: 此解决方案有点神秘,但请确保包含constraint='domain': fig.update_layout(xaxis=dict(scaleanchor='y',constrain='domain')) 情节 完整代码 fig.update_layout(xaxis=dict(scaleanch

当您设置
scaleanchor=x
时(例如,要使用
ff.annotated_heatmaps
制作完美的方形热图),您将得到与x轴本身有较大偏移的x轴标签,如下所示:

你如何解决这个问题?

代码:
此解决方案有点神秘,但请确保包含
constraint='domain'

fig.update_layout(xaxis=dict(scaleanchor='y',constrain='domain'))
情节

完整代码
fig.update_layout(xaxis=dict(scaleanchor='y',constrain='domain'))
import numpy as np
import plotly.graph_objs as go
import plotly.figure_factory as ff

# data
z = np.random.randint(0,6, size=(10, 10))
z_text = np.full(z.shape, '', dtype=str)
d = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
class_mat = np.vectorize(d.get)(z)

# plotly figure factory annotated heatmap
fig = ff.create_annotated_heatmap(z, annotation_text=z_text,
                                  text=class_mat, hoverinfo='text', colorscale='Viridis',
                                  x = list('ABCDEFGHIJ'),
                                  y = list('ABCDEFGHIJ')
                                 )
fig.layout.title = 'Semantic Segmentation'
fig.data[0]['hoverinfo'] = 'all'

# adjustment 1: scaleanchor => squared figure
fig['layout']['yaxis']['scaleanchor']='x'

# adjustment 2: remove redunant background background
fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')

# adjustment 3: x-axis label offsets
fig.update_layout(xaxis=dict(scaleanchor='y',constrain='domain'))

fig.show()