簡體   English   中英

如何在 Plotly 中向時間序列或折線圖添加直方圖

[英]How to add a Histogram to a time series or line chart chart in Plotly

我沒有問題顯示時間序列組件,但是,我似乎無法將直方圖部分添加到顯示中。 我附上了下面的問題。 我的 slider 的時間序列顯示了我想要的方式,但是直方圖無處可見。 任何幫助將不勝感激。

fig = px.line(df, x='OPR_DATE', y='HERMISTON_GENERATING', title='Daily Flow for Hermiston', template = "simple_white")

# Issue Seems to be in here
fig.add_trace(go.Histogram(
    x=Herm['Total'],
    histnorm='percent',
    name='Contracted', # name used in legend and hover labels
    xbins=dict( # bins used for histogram
        start=-4.0,
        end=3.0,
        size=0.5
    ),
    marker_color='#EB89B5',
    opacity=0.75
))

fig.update_xaxes(
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=list([
            dict(count=1, label="1m", step="month", stepmode="backward"),
            dict(count=6, label="6m", step="month", stepmode="backward"),
            dict(count=1, label="YTD", step="year", stepmode="todate"),
            dict(count=1, label="1y", step="year", stepmode="backward"),
            dict(step="all")
        ])
    )
)
fig.update_layout(
    title_text='Daily Flow for Hermiston + Contracted Volumes', # title of plot
    xaxis_title_text='OPR_DATE', # xaxis label
    yaxis_title_text='Daily Flows', # yaxis label
    bargap=0.2, # gap between bars of adjacent location coordinates
    bargroupgap=0.1 # gap between bars of the same location coordinates
)
fig.show()

我相信你會想要這些在單獨的情節或子情節中。 使用后者,您可以執行以下操作:

引入庫和一些示例數據

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
import pandas as pd

# some sample data
N=250
df = pd.DataFrame({'OPR_DATE': np.arange(0, N)+np.datetime64('2018-05-01'),
                   'HERMISTON_GENERATING': np.random.randint(0,1000,N),
                   'VOLUME': np.random.uniform(-3,3,N)})

創建一個子圖並添加線和直方圖

# now setup the subplot
fig = make_subplots(
    rows=2, cols=1,
    subplot_titles=("Daily Flow for Hermiston", "Contracted Volumes"),
    row_heights=[0.5, 0.5],
    specs=[[{"type": "xy"}], [{"type": "bar"}]])

# add the line
fig.add_trace(go.Scatter(x=df['OPR_DATE'],
                         y=df['HERMISTON_GENERATING']),
              row=1, col=1) #note the row/col

# add the histogram
fig.add_trace(go.Histogram(
        x=df['VOLUME'],
        histnorm='percent',
        name='Contracted',
        xbins=dict(start=-4.0, end=3.0, size=0.5),
        marker_color='#EB89B5',
        opacity=0.75),
    row=2, col=1) #note the row/col

更新數字

# update the line plot's x-axis
fig.update_xaxes(row=1, col=1, #note the row/col
                 title_text="OPR_DATE",
                 rangeslider_visible=True,
                 rangeslider_thickness=0.05,
                 rangeselector=dict(
                 buttons=list([
                    dict(count=1, label="1m", step="month", stepmode="backward"),
                    dict(count=6, label="6m", step="month", stepmode="backward"),
                    dict(count=1, label="YTD", step="year", stepmode="todate"),
                    dict(count=1, label="1y", step="year", stepmode="backward"),
                    dict(step="all")])))

# update the other axes with titles
fig.update_yaxes(title_text="Daily Flows", row=1, col=1) #note the row/col
fig.update_xaxes(title_text="VOLUME", row=2, col=1) #note the row/col
fig.update_yaxes(title_text="PCT", row=2, col=1) #note the row/col

# update the whole thing
fig.update_layout(
    title_text='Daily Flow for Hermiston + Contracted Volumes',
    showlegend=False, # not useful here
    bargap=0.2, width=900, height=900,
)
fig.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM