简体   繁体   English

具有动态变化的条形宽度(或动态变化的图形频率)的 Plotly Dash 条形图

[英]Plotly Dash bar chart with dynamically changing bar width (or dynamically changing graphing frequencies)

I have a bar graph made using Plotly Dash that looks like below.我有一个使用 Plotly Dash 制作的条形图,如下所示。 I have daily data from 2006 to now (2021) which makes the bar width very small.我有从 2006 年到现在(2021 年)的每日数据,这使得条形宽度非常小。 I am wondering if there is any way to plot and show a lower frequency graph when viewing in a larger timeframe(Y2006-Y2021) but show a detailed, higher frequency graph in a smaller timeframe(say, 2020 Mar to 2020 June).我想知道在较大的时间范围内(Y2006-Y2021)查看时是否有任何方法可以绘制和显示较低频率的图表,但在较小的时间范围内(例如,2020 年 3 月至 2020 年 6 月)显示详细的较高频率图表。

条形图输出

The solution I can think of now is to pre-process the data in Pandas before plotting, but it won't be dynamically changing when I zoom in. How can I graph a dynamic graph with changing graphing frequencies?我现在能想到的解决方案是在绘图之前对 Pandas 中的数据进行预处理,但是当我放大时它不会动态变化。如何绘制具有变化绘图频率的动态图? Below is my code.下面是我的代码。

df_data = df_data.dropna(subset=['date'])
    df_data = df_data.groupby(['date'])[
        ['mean_s', 'positive', 'negative']].mean().reset_index().sort_values('date')

fig = go.Figure()
fig.add_trace(go.Bar(
        x=df_data['date'],
        y=100 * (df_data['positive']) / (df_data['positive'] + df_data['negative']),
        base=0,
        name='Positive',
        marker_color=colors['pos1']
        ))
fig.add_trace(go.Bar(
        x=df_data['date'],
        y=100 * (df_data['negative']) / (df_data['positive'] + df_data['negative']),
        base=-100 * (df_data['negative']) / (df_data['positive'] + df_data['negative']),
        name='Negative',
        marker_color=colors['neg1']
        ))```

Given your requirement is to zoom in / interact with the data, the clear answer is a rangeslider鉴于您的要求是放大数据/与数据交互,明确的答案是范围滑块

import plotly.graph_objects as go
import pandas as pd
import numpy as np

s = 365 * 15
df = pd.DataFrame({"date": pd.date_range("1-jan-2008", periods=s),
                   "positive": np.random.uniform(0, 20, s),
                   "negative": np.random.uniform(0, -5, s),})

go.Figure([
    go.Bar(x=df["date"], y=df["positive"], name="positive", marker_color="green"),
    go.Bar(x=df["date"], y=df["negative"], name="negative", base=0, marker_color="red"),
    ]).update_layout(
    barmode="stack",
    xaxis={
        "range": [df.iloc[-200]["date"], df["date"].max()],
        "rangeslider": {"visible": True},
    },
    margin={"l": 0, "r": 0, "t": 0, "r": 0},
)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM