简体   繁体   English

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

[英]Plotly: How to adjust axis labels for annotated heatmaps when scaleanchor = x?

When you set scaleanchor=x when adjusting the aspect ratio , for example to make a perfectly square heatmap using ff.annotated_heatmaps , you'll end up with x-axis labels with a large offset from the x-axis itself like this:当您在调整纵横比时设置scaleanchor=x时,例如使用ff.annotated_heatmaps制作完美的正方形热图,您最终会得到与 x 轴本身有较大偏移的 x 轴标签,如下所示:

在此处输入图像描述

How can you fix that?你怎么能解决这个问题?

Code:代码:

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)')

fig.show()

This solution is a bit cryptic, but just make sure to include constrain='domain' :这个解决方案有点神秘,但只要确保包含constrain='domain'

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

Plot Plot

在此处输入图像描述

Complete code完整代码

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM