如何在Plotly中添加直方图到时间序列图或折线图中?

问题描述 投票:0回答:1

我在显示时间序列组件方面没有问题,但是,我似乎无法将直方图部分添加到显示中。我把问题附在下面。时间序列与我的滑块如何显示我想要的,但是直方图是无处可看。任何帮助将是感激的。

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()

python jupyter-notebook plotly jupyter
1个回答
0
投票

我相信你会希望这些在单独的图或子图。 使用后者,你可以做以下工作。

带来的库和一些样本数据

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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.