Plotly GO表在添加注释时会添加x轴和y轴元素

huangapple go评论53阅读模式
英文:

Plotly GO Table adds x-axis/y-axis elements when adding annotation

问题

我有一个函数,用于返回一个GO表。当向这个表添加注释时,它会放置x轴和y轴,用通用数字填充。不明白为什么会显示为表格。

期望的行为是标题将出现在表格区域之外,而x轴和y轴元素不会显示。下面是一个输出表格的图像,函数代码以及将plotly可视化转换为图像的代码。

Plotly GO表在添加注释时会添加x轴和y轴元素

函数代码:

def returnPlotlyGoTable(pdfData, dictChartText, strFillColorCols='lightsteelblue', strAlignCols='left', strFillColorVals='lightgray', strAlignVals='left'):
    tmpFig = go.Figure(data=[
        go.Table(header=dict(values=list(pdfData.columns),
                           fill_color=strFillColorCols,
                           align=strAlignCols),
                 cells=dict(values=pdfData.transpose().values.tolist(),
                            fill_color=strFillColorVals,
                            align=strAlignVals,
                            format=dictChartText['d3fmt']
                            )
                 )
    ])

    if 'title' in dictChartText.keys() and dictChartText['title'] != '':
        tmpFig.update_layout(title_text=dictChartText['title'])
    tmpFig.update_layout(margin=dict(t=150))
    if 'caption' in dictChartText.keys() and dictChartText['caption'] != '':
        tmpFig.add_annotation(font=dict(color='black', size=12),
                              x=0,
                              y=1.06,
                              showarrow=False,
                              text=dictChartText['caption'],
                              textangle=0,
                              xref='x',
                              yref='paper'
                              )
        tmpFig.update_layout()
    return tmpFig

图像转换代码:

imgTmp = BytesIO(key2.to_image(format='png', width=1980, height=1080))
英文:

I have a function to return a GO Table. When adding an annotation to this table, it puts an x-axis and y-axis, populated with generic numbers. Not understanding why this would display with a table.

Expected behavior is that the caption would appear outside of the table area and that x-axis and y-axis elements wouldn't be shown. Below is an image of an output table, the function code, and the code that converts the plotly visual into an image.

I tried to do a good faith search of open issues on Github, but didn't see anything. Additionally, I tried to search all official and unofficial documentation on GO tables, but didn't find anything that elucidated this. I have functions for px.bar and px.line that doesn't add any unwanted elements yet adds the caption in the exact spot as what is shown.

Plotly GO表在添加注释时会添加x轴和y轴元素

Function code:

def returnPlotlyGoTable(pdfData, dictChartText, strFillColorCols = 'lightsteelblue', strAlignCols = 'left', strFillColorVals = 'lightgray', strAlignVals = 'left'): 
    tmpFig = go.Figure(data = [go.Table(header = dict(values = list(pdfData.columns),                         
                                                      fill_color = strFillColorCols, 
                                                      align = strAlignCols), 
                                                      cells = dict(values = pdfData.transpose().values.tolist(), 
                                                                   fill_color = strFillColorVals, 
                                                                   align = strAlignVals, 
                                                                   format = dictChartText['d3fmt']
                                                                  )
                                       )
                              ]
                      )
    
    if 'title' in dictChartText.keys() and dictChartText['title'] != '': 
        tmpFig.update_layout(title_text = dictChartText['title'])
    #if 'xlabel' in dictChartText.keys() and dictChartText['xlabel'] != '': 
    #    tmpFig.update_xaxes(title_text = dictChartText['xlabel'])
    #if 'ylabel' in dictChartText.keys() and dictChartText['ylabel'] != '': 
    #    tmpFig.update_yaxes(title_text = dictChartText['ylabel'])
    #if 'caption' in dictChartText.keys() and dictChartText['caption'] != '': 
    #    tmpFig.update_layout(annotations = [go.layout.Annotation(showarrow = False, text = dictChartText['caption'], xanchor = 'left', x = 0.5, yanchor = 'bottom',  y = 0.5)]) 
        #tmpFig.update_layout(annotations = [dict(xref = 'paper', yref = 'paper', x = 0.5, y = -0.50, showarrow = False, text = dictChartText['caption'])])
    tmpFig.update_layout(margin = dict(t = 150))
    if 'caption' in dictChartText.keys() and dictChartText['caption'] != '': 
        tmpFig.add_annotation(font = dict(color = 'black', size = 12), 
                                     x = 0, 
                                     y = 1.06, 
                                     showarrow = False, 
                                     text = dictChartText['caption'], 
                                     textangle = 0, 
                                     xref = 'x', 
                                     yref = 'paper'
                               )
        tmpFig.update_layout()
        #tmpFig.update_layout(annotations = [go.layout.Annotation(showarrow = False, text = dictChartText['caption'], xanchor = 'left', x = 0.5, yanchor = 'bottom',  y = 0.5)]) 
        #tmpFig.update_layout(annotations = [dict(yref = 'paper', y = 1.06, showarrow = False, text = dictChartText['caption'])])
    #if 'd3fmt' in dictChartText.keys() and dictChartText['d3fmt'] != '': 
    #    tmpFig.update_layout()
    return tmpFig

Image convert snippet:

imgTmp = BytesIO(key2.to_image(format = 'png', width = 1980, height = 1080))

答案1

得分: 0

我通过将xref设置为'paper'而不是'x'来解决了这个问题。不确定为什么库会在表格上放置x轴/ y轴标签/刻度等,但这是由xref = 'x'引起的。更改此设置将删除异常的轴元素。

英文:

I was able to resolve this by setting xref = 'paper' instead of xref = 'x'. Not sure why the library puts an x-axis/y-axis label/ticks/etc on a table, but this was caused by the xref = 'x'. Changing this removes the aberrant axis elements.

huangapple
  • 本文由 发表于 2023年3月4日 00:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定