Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Plotly:如何防止跟踪之间的文本重叠?_Python_Python 3.x_Plotly - Fatal编程技术网

Python Plotly:如何防止跟踪之间的文本重叠?

Python Plotly:如何防止跟踪之间的文本重叠?,python,python-3.x,plotly,Python,Python 3.x,Plotly,我正在使用条形图和散点图。我必须将值显示为文本,而不是使用悬停文本。我在阅读文档时没有找到答案。也许你能帮我 如果我能找到一种方法,在条的底部设置条的文本,那就太好了 看到我的代码和下面的情节,非常感谢 from plotly import graph_objs as go from plotly.subplots import make_subplots meses = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set

我正在使用条形图和散点图。我必须将值显示为文本,而不是使用悬停文本。我在阅读文档时没有找到答案。也许你能帮我

如果我能找到一种方法,在条的底部设置条的文本,那就太好了

看到我的代码和下面的情节,非常感谢

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()