[英]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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.