在图例框中以不完整的格式显示跟踪名称

问题描述 投票:1回答:1

我正在尝试获取带格式的文本,作为情节图中的迹线名称,但是文本输出不完整,就像图例框将其缩写一样。代码是这样的:

print(results.params)
const    18.352876
x         2.729230
x^2      -0.284947
x^3       0.017321
x^4      -0.000359
dtype: float64

fitname = '$y='+"".join([' {:+.3}{}'.format(i,j) for i,j in zip(results.params[::-1].values,results.params[::-1].index.values)]).strip('const')+'$'

trace1 = go.Scatter(x = df['x'], #dataframe x data
                    y = df['y'], #dataframe y data
                    name = 'Process data',
                    mode = 'markers',
                    line_width = 2)

trace2 = go.Scatter(x = df['x'],
                    y = results.predict(),
                    mode = 'lines+markers',
                    name = fitname,
                    hoverinfo = 'x+y',
                    line_width = 2)

data = [trace1, trace2]

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

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

其中results是statsmodels OLS拟合结果对象。生成以下图

enter image description here

如果我设置字体大小> 14,则如图例所示,多项式被图例框剪切。我确实需要大字体。是否有其他方法可以在图例中获得具有较大字体大小的格式化跟踪名称而不会被剪切?

python plotly statsmodels
1个回答
0
投票

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

enter image description here

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

enter image description here

因此,在您的情况下这应该不是问题。因此,我怀疑标题本身的格式不正确。您可以尝试将其打印出来并进行检查吗?而且,如果您可以共享数据样本来重建绘图,将更容易为您提供帮助。

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

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.

[请让我知道我是否在这里缺少Som详细信息!

© www.soinside.com 2019 - 2024. All rights reserved.