【问题标题】:Some bars are missing from the plotly dash plot randomly情节虚线图中随机缺少一些条形图
【发布时间】:2021-10-07 12:53:42
【问题描述】:

我正在使用 Plotly Dash 创建一个仪表板,其中包含在公共 x 轴上带有条形图和折线图的图。当我运行它时,代码运行良好,但是当我从下拉列表中选择不同的选项来更改图表时,某些选项的一些条形图会随机丢失。 这是一个运行良好的示例。perfectly fine image

这是一个示例,当我从下拉列表中选择不同的选项时,图中的条形消失了。

image with missing bar

我不确定出了什么问题。我检查了缺失条的数据是否丢失,但它在那里,当我将鼠标悬停在图中缺失的条区域上时,它也向我显示了那里的值,但没有显示条。作为参考,这是我的代码……

trace1  = go.Scatter(
    mode='lines+markers+text',
    x = df1['Date'],
    y = df1['AVG'],
    name="Avg time(mins)",
    text=df1['AVG'],
    textposition='bottom right',
    textfont_color='black',
    marker_color='blue',
    opacity=1
)
trace2 = go.Bar(
    x = df1['Date'],
    y = df1['Id_Count'],
    name="Count",
    ids=df1['Id_Count'],
    text=df1['Id_Count'],
    textposition='inside',
    insidetextfont_color='#45FC03',
    hoverinfo='x+y',
    yaxis='y2',
    marker_color ='#180702',
    marker_line_width=1.5,
    marker_line_color='black',
    opacity=0.72
)
data1 = [trace1, trace2]

layout1 = go.Layout(
title_text='Day wise stats',
title_font_color='#45FC03',
width=1300,
height=400,
yaxis=dict(
    side = 'right'
),
yaxis2=dict(
    overlaying='y',
    anchor='y3',
    )
)

fig1 = go.Figure(data=data1, layout=layout1)
fig1.update_layout(template='plotly_dark',
plot_bgcolor='#F0E199', paper_bgcolor='#282522',  margin_l=50, margin_t=80, margin_b=60)
fig1.update_xaxes(showgrid=False, zeroline=False)
fig1.update_yaxes(showgrid=False, zeroline=False)

拜托,如果能提供任何解决此问题的方法,我们将不胜感激。

【问题讨论】:

  • 没有什么可以显示您的过滤方式。我怀疑条形图丢失是因为过滤后的数据框中的关联值是 NaN。这不是随机的,而是基于数据/如何完成过滤的功能

标签: plotly-dash plotly-python


【解决方案1】:
  • 栏的内容基于数据框中的数据
  • 下面的代码显示,如果底层数据框具有 NaN 作为值,则条形消失
import pandas as pd
import numpy as np
import plotly.graph_objects as go

df1 = pd.DataFrame({"Date":pd.date_range("28-sep-2021", freq="D", periods=7), "AVG":np.round(np.random.uniform(2,5,7),2), "Id_Count":np.random.randint(2000,3000,7)})

def graph(df1):
    trace1  = go.Scatter(
        mode='lines+markers+text',
        x = df1['Date'],
        y = df1['AVG'],
        name="Avg time(mins)",
        text=df1['AVG'],
        textposition='bottom right',
        textfont_color='black',
        marker_color='blue',
        opacity=1
    )
    trace2 = go.Bar(
        x = df1['Date'],
        y = df1['Id_Count'],
        name="Count",
        ids=df1['Id_Count'],
        text=df1['Id_Count'],
        textposition='inside',
        insidetextfont_color='#45FC03',
        hoverinfo='x+y',
        yaxis='y2',
        marker_color ='#180702',
        marker_line_width=1.5,
        marker_line_color='black',
        opacity=0.72
    )
    data1 = [trace1, trace2]

    layout1 = go.Layout(
    title_text='Day wise stats',
    title_font_color='#45FC03',
    width=800,
    height=400,
    yaxis=dict(
        side = 'right'
    ),
    yaxis2=dict(
        overlaying='y',
        anchor='y3',
        )
    )

    fig1 = go.Figure(data=data1, layout=layout1)
    fig1.update_layout(template='plotly_dark',
    plot_bgcolor='#F0E199', paper_bgcolor='#282522',  margin_l=50, margin_t=80, margin_b=60)
    fig1.update_xaxes(showgrid=False, zeroline=False)
    fig1.update_yaxes(showgrid=False, zeroline=False)
    return fig1

graph(df1).show()

df1.loc[df1.sample(1).index, "Id_Count"] = np.nan
graph(df1).show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多