Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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_Plotly_Plotly Dash - Fatal编程技术网

Python Plotly:如何在组合图表中注释图形对象条形图?

Python Plotly:如何在组合图表中注释图形对象条形图?,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我有一个折线图和条形图的组合图。Plotly允许我对条形图或折线图进行注释。如何对两者进行注释 fig_time_series = go.Figure( go.Scatter( x=errors_df["date"], y=errors_df["count_cumsum"], mode="lines+markers+text&q

我有一个折线图和条形图的组合图。Plotly允许我对条形图或折线图进行注释。如何对两者进行注释

fig_time_series = go.Figure(
            go.Scatter(
                x=errors_df["date"],
                y=errors_df["count_cumsum"],
                mode="lines+markers+text",
                text=errors_df["count_cumsum"],
                textposition="top center",
                name="Cumulative errors"
            )
        )
        fig_time_series.update_layout(title="Error Trend")

        fig_time_series.add_trace(
            go.Bar(
                x=errors_df["date"],
                y=errors_df["count"],
                text=errors_df["count"],
                name="Monthly errors",
            )
        )
这给了我-


也请您帮助注释条形图。

通过将
go.bar中的
textposition
设置为['inside'、'outside'、'auto'、'none']中的一个,我能够获得条形图显示的文本值。使用示例代码,它看起来是这样的:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=[0, 1, 2, 3, 4, 5],
        y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
        mode="lines+markers+text",
        text = ['a','a','a','a','a','a'],
        textposition= 'top center'
    ))

fig.add_trace(
    go.Bar(
        x=[0, 1, 2, 3, 4, 5],
        y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
        text = ['b','b','b','b','b', 'b'],
        textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
    ))


fig.show()
实际图形如下所示:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=[0, 1, 2, 3, 4, 5],
        y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
        mode="lines+markers+text",
        text = ['a','a','a','a','a','a'],
        textposition= 'top center'
    ))

fig.add_trace(
    go.Bar(
        x=[0, 1, 2, 3, 4, 5],
        y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
        text = ['b','b','b','b','b', 'b'],
        textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
    ))


fig.show()

通过将
go.Bar
中的
textposition
设置为['inside'、'outside'、'auto'、'none']中的一个,我能够获得条形图显示的文本值。使用示例代码,它看起来是这样的:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=[0, 1, 2, 3, 4, 5],
        y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
        mode="lines+markers+text",
        text = ['a','a','a','a','a','a'],
        textposition= 'top center'
    ))

fig.add_trace(
    go.Bar(
        x=[0, 1, 2, 3, 4, 5],
        y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
        text = ['b','b','b','b','b', 'b'],
        textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
    ))


fig.show()
实际图形如下所示:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=[0, 1, 2, 3, 4, 5],
        y=[1.5, 1, 1.3, 0.7, 0.8, 0.9],
        mode="lines+markers+text",
        text = ['a','a','a','a','a','a'],
        textposition= 'top center'
    ))

fig.add_trace(
    go.Bar(
        x=[0, 1, 2, 3, 4, 5],
        y=[1, 0.5, 0.7, -1.2, 0.3, 0.4],
        text = ['b','b','b','b','b', 'b'],
        textposition = 'auto' # it must be one of: ['inside', 'outside', 'auto', 'none']
    ))


fig.show()

只需在
go.Bar
中包含
text
textposition

fig.add_traces(go.Bar(x=df['x'], y = df['y1'],
                      text = df['y2'],
                      textposition="outside"
                      ))

textposition
的其他选项包括:

['inside', 'auto', 'none']
默认值是
none
,正如您已经发现的那样

完整代码:
只需将
text
textposition
包含在
go.Bar
中即可:

fig.add_traces(go.Bar(x=df['x'], y = df['y1'],
                      text = df['y2'],
                      textposition="outside"
                      ))

textposition
的其他选项包括:

['inside', 'auto', 'none']
默认值是
none
,正如您已经发现的那样

完整代码:
我根据一些随机数提出了一个建议。下一次你发布一个问题,请考虑包括你的数据样本,如伟大的信息,谢谢。我对细节投了赞成票。我根据一些随机数提出了一个建议。下一次你发布一个问题,请考虑包括你的数据样本,如伟大的信息,谢谢。投票赞成细节,理解错误。谢谢,我理解这个错误。非常感谢。