18910140161

HTML-使用Plotly(Python)保持注释相对于圆环图的相对位置不变-堆栈溢出

顺晟科技

2022-10-18 14:09:16

98

如何在屏幕分辨率更改时保持注释的位置相对于圆环图的位置不变?在我的情况下,标题在某些情况下集中在甜甜圈洞内,而在其他情况下集中在图表上方。我在Plotly为Subplot中的饼图给出的例子中看到了这个问题:https://plotly.com/python/pie-charts/#pie-charts-in-subplots

我知道这几乎是肯定的,因为我使用常量作为我的X和y坐标,但我不知道如何使它们相对。

大致正确定位:Positions looking pretty good

调整窗口大小后定位

不正确:

Positions looking less good

import plotly.graph_objects as go
from plotly.subplots import make_subplots

lst_abc = [153, 256]
lst_def = [1569, 724]
lst_ghi = [75618, 28971]
lst_long = [2, 5]

# Create subplots, using 'domain' type for pie charts
specs = [[{'type':'domain'}, {'type':'domain'}],
         [{'type':'domain'}, {'type':'domain'}]]

fig = make_subplots(rows=2, cols=2, specs = specs)

# Define pie charts                    
fig.add_trace(go.Pie(labels=[1,2], values=lst_abc, name='ABC'), 1, 1)
fig.add_trace(go.Pie(labels=[1,2], values=lst_def, name='DEF'), 1, 2)
fig.add_trace(go.Pie(labels=[1,2], values=lst_ghi, name='GHI'), 2, 1)
fig.add_trace(go.Pie(labels=[1,2], values=lst_long, name='Longer Title Here'),
              2, 2)

fig.update_traces(hole=.4, hoverinfo='percent', textinfo='value')

fig.update_layout(
    showlegend=False,
    # Add annotations in the center of the donut pies.
    annotations=[dict(text='ABC', x=0.213, y=0.805, font_size=15, showarrow=False),
                 dict(text='DEF', x=0.787, y=0.805, font_size=15, showarrow=False),
                 dict(text='GHI', x=0.213, y=0.200, font_size=15, showarrow=False),
                 dict(text='Longer Title Here', x=0.82, y=0.46, font_size=15,
                      showarrow=False)])
fig.show()

with open('Current Stats.html', 'a') as f:
        f.write(fig.to_html(full_html=False, include_plotlyjs='cdn'))

顺晟科技:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航