繁体   English   中英

Plotly 在图例框中显示不完整的格式化轨迹名称

[英]Plotly displays incomplete formatted trace name in legend box

我试图在 plotly plot 中获取格式化文本作为跟踪名称,但文本显示不完整,好像图例框缩短了它。 代码是这样的:

trace1 = go.Scatter(x = [ 0.7686741,  0.90457  ,  1.140613 ,  1.395725 ,  1.7772   ,
        2.339876 ,  3.112365 ,  4.161422 ,  4.194801 ,  5.212864 ,
        6.371596 ,  7.396811 ,  7.604239 ,  7.969025 ,  8.355268 ,
        8.443485 ,  8.703365 ,  9.63798  , 10.97553  , 14.62577  ,
       14.75691  , 19.68748  ],
                    y = [20, 20, 21, 22, 23, 24, 25, 26, 25, 27, 28, 28, 29, 29, 30, 30, 30,
       31, 32, 35, 35, 40],
                    name = 'Process data',
                    mode = 'markers',
                    line_width = 2)

trace2 = go.Scatter(x = df['x'],
                    y = results.predict(),
                    mode = 'lines+markers',
                    name = '$y= -0.000359x^4 +0.0173x^3 -0.285x^2 +2.73x +18.4$',
                    hoverinfo = 'x+y',
                    line_width = 2,)

data = [trace1, trace2]

layout = go.Layout(template= 'simple_white',
                   legend = dict(font_size = 20,
                                 x = 0.02),
                    width = 1060, height = 550)

fig = go.Figure(data=data, layout=layout)
fig.show()

这将生成以下 plot:

在此处输入图像描述

如果我设置font size > 14 ,则多项式会被图例框剪切,如您所见。 我确实需要大字体。 是否有其他方法可以在不被剪切的情况下以大字体获取图例中的格式化跟踪名称?

编辑:删除 OLS 结果 object,替换 x 和 y 数据并直接定义引发问题的跟踪名称。

第二次尝试

我终于设法复制了你的问题,我不得不使用一个很长的表达式: r'$y= -0.0003585x^4 +0.01732x^3 -0.2849x^2 +2.729x +18.35 => y= -0.0003585x^4 +0.01732x^3 -0.2849x^2 +2.729x +18.35$'得到这个:

Plot 1:

在此处输入图像描述

虽然这不是一个非常优雅的方法,但我确实对如何解决,或者更确切地说是解决这个问题提出了建议。 我不是 100% 确定这里发生了什么。 但似乎在一定长度的轨迹名称之后,plotly 会以不同的方式将元素框的宽度调整为名称的长度。 而且似乎显示的格式化名称的长度与运行代码时使用的字符串的长度不同。 但我们可以回到这一点。 为了给上面的巨大公式腾出空间,我只是在名字后面添加了一堆空格:

Plot 2:

在此处输入图像描述

代码 2:

import plotly.io as pio
pio.renderers.default='browser'

import plotly.graph_objects as go
import numpy as np

x = np.arange(10)

polyname = r'$y= -0.0003585x^4 +0.01732x^3 -0.2849x^2 +2.729x +18.35 => y= -0.0003585x^4 +0.01732x^3 -0.2849x^2 +2.729x +18.35$'
normalname = 'trace1'
normalname = normalname + ' '*180

fig = go.Figure(data=go.Scatter(x=x**6, y=x**4,
                                mode='lines+markers',
                                hovertemplate = '<i>y</i>:%{y:20,.2f}'+ '<br><b>x</b>: %{x}<br>',
                                #name = 'trace1'+(str([' ']*len(polyname)))
                                name=normalname))

fig.add_trace(go.Scatter(x=x**6, y=x**4.1, mode='markers', name = tracename))

fig.update_layout(
    legend=dict(
        x=0,
        y=1,
        traceorder="normal",
        font=dict(
            family="sans-serif",
            size=22,
            color="black"
        ),
        bgcolor="rgba(0,0,255,0.2)",
        bordercolor="Black",
        borderwidth=2
    )
)

fig.show()

如您所见,我在normalname = normalname + ' '*180中使用了180 ,以便在图例中为格式化名称腾出足够的空间。 更优雅的方法是normalname = normalname + ' '*len(polyname) ,即114 ,但随后又回到:

在此处输入图像描述

这就是为什么我认为格式化名称的长度以及用作格式化名称源的字符串中的字符数r'$y= -0.0003585x^4 +0.01732x^3 -0.2849x^2 +2.729x +18.35 => y= -0.0003585x^4 +0.01732x^3 -0.2849x^2 +2.729x +18.35$'

无论如何,我希望这是你可以使用的东西。

初步答案:

我假设您的意思是完整的多项式表达式被图例框截断。 你确定它会吗? 你看,我无法复制你的问题。 即使使用跟踪名称,如'very long polynomial expression that doesnt fit in the box'

在此处输入图像描述

只有当名称比 plot 本身长时,这才会成为问题。 由字符串长度或字体大小引起的:

在此处输入图像描述

因此,在您的情况下,这应该不是问题。 因此,我怀疑是标题本身的格式不正确。 你能试着打印出来检查一下吗? 如果您可以共享一个数据样本来重建您的 plot,它会更容易帮助您。

此外,没有指定图例框大小的选项:

Valid properties for object of type plotly.graph_objs.layout.Legend:

        bgcolor
            Sets the legend background color. Defaults to
            `layout.paper_bgcolor`.
        bordercolor
            Sets the color of the border enclosing the legend.
        borderwidth
            Sets the width (in px) of the border enclosing the
            legend.
        font
            Sets the font used to text the legend items.
        itemclick
            Determines the behavior on legend item click. "toggle"
            toggles the visibility of the item clicked on the
            graph. "toggleothers" makes the clicked item the sole
            visible item on the graph. False disable legend item
            click interactions.
        itemdoubleclick
            Determines the behavior on legend item double-click.
            "toggle" toggles the visibility of the item clicked on
            the graph. "toggleothers" makes the clicked item the
            sole visible item on the graph. False disable legend
            item double-click interactions.
        itemsizing
            Determines if the legend items symbols scale with their
            corresponding "trace" attributes or remain "constant"
            independent of the symbol size on the graph.
        orientation
            Sets the orientation of the legend.
        title
            :class:`plotly.graph_objects.layout.legend.Title`
            instance or dict with compatible properties
        tracegroupgap
            Sets the amount of vertical space (in px) between
            legend groups.
        traceorder
            Determines the order at which the legend items are
            displayed. If "normal", the items are displayed top-to-
            bottom in the same order as the input data. If
            "reversed", the items are displayed in the opposite
            order as "normal". If "grouped", the items are
            displayed in groups (when a trace `legendgroup` is
            provided). if "grouped+reversed", the items are
            displayed in the opposite order as "grouped".
        uirevision
            Controls persistence of legend-driven changes in trace
            and pie label visibility. Defaults to
            `layout.uirevision`.
        valign
            Sets the vertical alignment of the symbols with respect
            to their associated text.
        x
            Sets the x position (in normalized coordinates) of the
            legend. Defaults to 1.02 for vertical legends and
            defaults to 0 for horizontal legends.
        xanchor
            Sets the legend's horizontal position anchor. This
            anchor binds the `x` position to the "left", "center"
            or "right" of the legend. Value "auto" anchors legends
            to the right for `x` values greater than or equal to
            2/3, anchors legends to the left for `x` values less
            than or equal to 1/3 and anchors legends with respect
            to their center otherwise.
        y
            Sets the y position (in normalized coordinates) of the
            legend. Defaults to 1 for vertical legends, defaults to
            "-0.1" for horizontal legends on graphs w/o range
            sliders and defaults to 1.1 for horizontal legends on
            graph with one or multiple range sliders.
        yanchor
            Sets the legend's vertical position anchor This anchor
            binds the `y` position to the "top", "middle" or
            "bottom" of the legend. Value "auto" anchors legends at
            their bottom for `y` values less than or equal to 1/3,
            anchors legends to at their top for `y` values greater
            than or equal to 2/3 and anchors legends with respect
            to their middle otherwise.

如果我在这里遗漏了一些细节,请告诉我!

暂无
暂无

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

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