Plotly.py - HTML 图中有趣的图形大小不稳定性

Plotly.py - Interesting Figure Size Instability in HTML Plots

我真的很感激在这方面的一些帮助,因为它让我抓狂。我一直在使用 plotly.offline 和按钮菜单创建一些交互式图表。在某些情况下,设置轴属性时会导致绘图不稳定。执行代码并显示图形时,它的大小不正常。单击 legend/a 按钮 menu/some 缩放控件会导致图形循环更改位置和大小。这使得结束情节非常难以使用。

我已经通过不同的途径多次发现这个错误,因此感觉可能是我正在做一些特别的事情来引起它。话虽如此,这绝对不是我正在使用的设置可能导致的事情。这是我复制行为的代码的简化版本。 https://github.com/plotly/plotly.py/issues/2851 这里的 github 问题包含一个带有 .html 图的 .zip 文件,可以显示行为。我还在本期底部展示了一些示例图片。

请注意,此行为并不仅限于小提琴情节。我几乎可以在任何情节类型中做到这一点。

(请原谅这段代码中的许多奇怪之处。我将其制作为我正在处理的更大的东西的缩短版本,这就是为什么有些事情以奇怪的方式完成的原因。例如,数字轴被文本标签覆盖)

import numpy as np
import plotly.graph_objects as go
from plotly.offline import plot

x_data_1 = np.random.rand(50)*100
x_data_2 = np.random.rand(50)*100

fig = go.Figure()

for i, category in enumerate(['a', 'b', 'c', 'd']):
    fig.add_trace(go.Violin(y=i*np.ones(50),
                            x=x_data_1,
                            legendgroup=category, scalegroup=category, name=category+'1', showlegend=True,
                            side='negative',
                            line_color='rgb(255,0,0)',
                            line_width=2, visible=True, points='suspectedoutliers', orientation='h',
                            fillcolor='rgba(255,0,0,0.4)')
                  )
    fig.add_trace(go.Violin(y=i*np.ones(50),
                            x=x_data_2,
                            legendgroup=category, scalegroup=category, name=category+'2', showlegend=True,
                            side='positive',
                            line_color='rgb(255,0,0)',
                            line_width=2, visible=True, points='suspectedoutliers', orientation='h',
                            fillcolor='rgba(255,0,0,0.4)')
                  )
    fig.add_trace(go.Violin(y=i*np.ones(50),
                            x=x_data_1*2,
                            legendgroup=category, scalegroup=category, name=category+'1', showlegend=True,
                            side='negative',
                            line_color='rgb(255,255,0)',
                            line_width=2, visible=False, points='suspectedoutliers', orientation='h',
                            fillcolor='rgba(255,255,0,0.4)')
                  )
    fig.add_trace(go.Violin(y=i*np.ones(50),
                            x=x_data_2*2,
                            legendgroup=category, scalegroup=category, name=category, showlegend=True,
                            side='positive',
                            line_color='rgb(255,255,0)',
                            line_width=2, visible=False, points='suspectedoutliers', orientation='h',
                            fillcolor='rgba(255,255,0,0.4)')
                  )

fig.update_traces(meanline_visible=True)
fig.update_layout(violingap=0, violinmode='overlay')

fig.update_layout(
    updatemenus=[
        go.layout.Updatemenu(
            type="buttons",
            buttons=[
                dict(label='Data 1',
                     method="update",
                     args=[{"visible": 4*[True, True, False, False]}]),
                dict(label='Data 2',
                     method="update",
                     args=[{"visible": 4*[False, False, True, True]}]),
                dict(label='All',
                     method="update",
                     args=[{"visible": 4*[True, True, True, True]}])
            ],
        )],
    plot_bgcolor='rgba(0,0,0,0)')  # Set title

fig.update_yaxes(showline=True, linewidth=2, linecolor='black', mirror=True,
                 showgrid=True, gridwidth=1, gridcolor='black', range=[4, -1], tickvals=np.linspace(4, -1, 6),
                 ticktext=[''] + ['d', 'c', 'b', 'a'] + [''])
fig.update_xaxes(showline=True, linewidth=2, linecolor='black', mirror=True,
                 showgrid=True, gridwidth=0.5, gridcolor='rgba(100,100,100,0.2)', nticks=20,
                 range=[0, 100])

plot(fig, include_plotlyjs=True, filename=r'C:\Resize_Bug.html')

Layout Position 1

Layout Position 2

此问题目前存在于 plotly.js

的 autoMargin 功能中

这里有一些错误,这部分代码正在接受审查。对于将来遇到此行为的任何人,可以通过设置来解决此问题:

fig.update_xaxes(automargin=False) fig.update_yaxes(automargin=假)