【问题标题】:Place text in front / on top of a Dash Graph object将文本放在 Dash Graph 对象的前面/顶部
【发布时间】:2022-01-11 12:42:58
【问题描述】:

如果满足条件,我需要显示一条消息。我看到 this post 处理显示空白图表但无法弄清楚如何申请我的需求。
图表的功能:

import plotly.express as px

@app.callback(
Output("graph-figure", "figure"),
Input("dropdown_a", "value"),
Input("dropdown_x", "value"),
Input("dropdown_y", "value"))

def update_graph(a, x_axis, y_axis):
    df = pd.read_csv(r"C:\Users\user\project\Data.csv")
    # in this case a message needs to be shown instead of the graph
    if x_axis == y_axis:
         #  ...
    return px.bar(df, x=x_axis, y=y_axis)

图形对象:

html.Div(dcc.Graph(id="graph-figure"))

如果您认为需要更多信息,请告诉我,我会提供。
谢谢。

【问题讨论】:

    标签: python plotly-python


    【解决方案1】:

    我找到了一个解决方案:调用一个为图形的layout 返回新值的函数:

    函数(当然可以修改):

    def show_message(message):
        return {
            "layout": {
                "xaxis": {"visible": False},
                "yaxis": {"visible": False},
                "annotations": [
                    {
                        "text": message,
                        "xref": "paper",
                        "yshift": "90",
                        "showarrow": False,
                        "font": {"size": 20, "color": "gray"},
                    }
                ],
            }
        }
    

    调用函数:

    def update_graph(a, x_axis, y_axis):
        df = pd.read_csv(r"C:\Users\user\project\Data.csv")
        # in this case a message needs to be shown instead of the graph
        if x_axis == y_axis:
             show_message('Message to show')
        return px.bar(df, x=x_axis, y=y_axis)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多