简体   繁体   English

在 Plotly Python 中添加辅助轴

[英]Adding a secondary axis in Plotly Python

I'm working with a Dash graph object and I'm fairly new to it.我正在使用 Dash 图形对象,而且我对它还很陌生。 I'm attempting to pass in a graph that has 2 scatter charts and a bar chart on the same figure but I'd like the bar chart (green) to be on it's own secondary y axis so it looks better than it does here:我试图传入一个图表,该图表在同一个图形上有 2 个散点图和一个条形图,但我希望条形图(绿色)位于它自己的第二个 y 轴上,因此它看起来比这里更好:

在此处输入图片说明

Now from what I understand about Dash, I have to pass a go.Figure() object so I have a function which defines the data and the layout .现在根据我对 Dash 的理解,我必须传递一个go.Figure()对象,所以我有一个定义datalayout的函数。 I saw in the plotly documentation that you can use plotly express add secondary axis but I'm not sure how to do that within my frame work here.我在 plotly 文档中看到你可以使用 plotly express add secondary axis 但我不确定如何在我的框架工作中做到这一点。 Any help would be greatly appreciated!任何帮助将不胜感激!

Here's my code:这是我的代码:

def update_running_graph(n_intervals):
    df = pd.read_csv(filename)

    trace1 = go.Scatter(x=df['Timestamp'],
                        y=df['CLE'],
                        name='Crude',
                        mode='lines+markers')
    trace2 = go.Scatter(x=df['Timestamp'],
                        y=df['y_pred'],
                        name='Model',
                        mode='lines+markers')
    trace3 = go.Bar(x=df['Timestamp'],
                    y=df['ModelDiff'],
                    name='Diff',
                    )
    data = [trace1, trace2,trace3]
    layout = go.Layout(title='CLE vs Model')
    return go.Figure(data=data, layout=layout)

To add a secondary y-axis in dash you could do the following:要在破折号中添加辅助 y 轴,您可以执行以下操作:

def update_running_graph(n_intervals):
    df = pd.read_csv(filename)

    trace1 = go.Scatter(x=df['Timestamp'],
                        y=df['CLE'],
                        name='Crude',
                        mode='lines+markers',
                        yaxis='y1')
    trace2 = go.Scatter(x=df['Timestamp'],
                        y=df['y_pred'],
                        name='Model',
                        mode='lines+markers',
                        yaxis='y1')
    trace3 = go.Bar(x=df['Timestamp'],
                    y=df['ModelDiff'],
                    name='Diff',
                    yaxis='y2'
                    )
    data = [trace1, trace2,trace3]
    layout = go.Layout(title='CLE vs Model',
                       yaxis=dict(title='Crude and Model'),
                       yaxis2=dict(title='Moddel Difference',
                                   overlaying='y',
                                   side='right'))
    return go.Figure(data=data, layout=layout)

you can add more y-axis they always need to have the form of yi with i the i-th axis.你可以添加更多的 y 轴,它们总是需要具有yi的形式,i 是第 i 轴。 Then in the layout you can specify the layout of the i-th axis with yaxisi=dict(...) .然后在布局中,您可以使用yaxisi=dict(...)指定第 i 个轴的布局。

This documentation page should be of use.这个文档页面应该有用。 Just modify to fit your code, since trace1 and trace2 appear to be on the same scale, just set trace3 to the secondary axis scale and you should be set.只需修改以适合您的代码,因为 trace1 和 trace2 似乎在同一比例上,只需将 trace3 设置为辅助轴比例即可。 Below is an example with just only 2 but adding a third should not be too difficult.下面是一个只有 2 个的示例,但添加第三个应该不会太困难。

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces
fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
    secondary_y=False,
)

fig.add_trace(
    go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data"),
    secondary_y=True,
)

# Add figure title
fig.update_layout(
    title_text="Double Y Axis Example"
)

# Set x-axis title
fig.update_xaxes(title_text="xaxis title")

# Set y-axes titles
fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)

fig.show()

Cheers!干杯!

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

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