【问题标题】:Plotly: How to adjust axis labels for annotated heatmaps when scaleanchor = x?Plotly:当 scaleanchor = x 时,如何调整带注释的热图的轴标签?
【发布时间】:2021-05-11 21:19:57
【问题描述】:

当您设置scaleanchor=xadjusting the aspect ratio 时,例如使用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()

【问题讨论】:

    标签: python plotly heatmap


    【解决方案1】:

    这个解决方案有点神秘,但请确保包含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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2020-10-06
      • 2020-06-23
      • 2021-11-28
      • 2022-06-30
      • 2021-04-03
      • 2021-03-29
      相关资源
      最近更新 更多