Plotly:当 scaleanchor = x 时,如何调整带注释的热图的轴标签?
Plotly: How to adjust axis labels for annotated heatmaps when scaleanchor = x?
当您在 时设置 scaleanchor=x
,例如使用 ff.annotated_heatmaps
制作一个完美的正方形热图,您最终会得到 x 轴标签与x 轴本身是这样的:
你怎么解决这个问题?
代码:
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()
这个解决方案有点神秘,但一定要包括 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()
当您在 scaleanchor=x
,例如使用 ff.annotated_heatmaps
制作一个完美的正方形热图,您最终会得到 x 轴标签与x 轴本身是这样的:
你怎么解决这个问题?
代码:
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()
这个解决方案有点神秘,但一定要包括 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()