簡體   English   中英

Plotly:如何防止跟蹤之間的文本重疊?

[英]Plotly: How to prevent text overlapping between traces?

我正在使用條形圖和散點圖。 我必須將值顯示為文本,而不是使用懸停文本。 我沒有找到閱讀文檔的答案。 也許你能幫我!

如果我能找到一種在條形底部設置條形文本的方法,那就太好了。

請參閱我的代碼和下面的情節,非常感謝!

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

meses = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']  
rlzd = [335101.3, 693658.5, 335101.3, 800019, 514379, 594379, 581279, 310029, 485022, 692091, 227918]
meta = [660756.5, 658980.9, 527478.1, 800019, 603574.5, 584198.7, 590098, 571476.2, 616965.7, 676205.1, 617237.94, 613634.7]

fig = make_subplots(rows=1, cols=2, column_widths=[0.85, 0.15], print_grid=True, horizontal_spacing=0.05)
fig.add_trace(go.Bar(
                x=meses,y = rlzd,
                text=rlzd,
                textfont=dict(size=15,color='white'),
                textposition='auto',
                name = 'Realizado',
                showlegend=True),
                row=1, col=1)
fig.add_trace(go.Scatter(
                x=meses,y=meta,
                name='Meta',
                mode='markers+lines+text',
                text=meta,
                textfont=dict(size=13,color='black'),
                textposition="top right",
                marker_size=8,
                showlegend=True,
                line = go.scatter.Line(color="DarkBlue", dash='dot')),
                row=1, col=1)
fig.show()

在此處輸入圖片說明

據我所知,沒有直接的方法來編輯條形中標簽的位置。 但是您可以輕松刪除標簽並使用自定義注釋來代替:

在此處輸入圖片說明

完整代碼:

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

meses = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']  
rlzd = [335101.3, 693658.5, 335101.3, 800019, 514379, 594379, 581279, 310029, 485022, 692091, 227918]
meta = [660756.5, 658980.9, 527478.1, 800019, 603574.5, 584198.7, 590098, 571476.2, 616965.7, 676205.1, 617237.94, 613634.7]

fig = make_subplots(rows=1, cols=2, column_widths=[0.85, 0.15], print_grid=True, horizontal_spacing=0.05)
fig.add_trace(go.Bar(
                x=meses,y = rlzd,
                text=rlzd,
                textfont=dict(size=15,color='rgba(0,0,0,0)'),
                textposition='outside',
                name = 'Realizado',
                showlegend=True),
                row=1, col=1)
fig.add_trace(go.Scatter(
                x=meses,y=meta,
                name='Meta',
                mode='markers+lines+text',
                text=meta,
                textfont=dict(size=13,color='black'),
                textposition="top right",
                marker_size=8,
                showlegend=True,
                line = go.scatter.Line(color="DarkBlue", dash='dot')),
                row=1, col=1)

# you've got one more month than
# youve got elements in the list rlzd
# the following two lines takes that
# into consideration
length_diff = len(meses)-len(rlzd)
rlzd.extend(['']*length_diff)

# adds annotatoins for each month / bar
for i, m in enumerate(meses):
    fig.add_annotation(dict(font=dict(color="white",size=12),
                            #x=x_loc,
                            x=i,
                            y=0.05,
                            showarrow=False,
                            text=rlzd[i],
                            textangle=-90,
                            xref="x",
                            yref="paper"
                           ))

fig.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM