Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

python - Dash app multi line chart on two axes using plotly

I have written the below code and the objective is:

  • Plot a line chart with 6 lines, 3 on the primary axis (LHS) and 3 on the secondary axis (RHS)

The code I used is below and works in Plotly but when I open the dash app, there is no secondary axis

In the future I will be looking to have call backs to reduce the length of the series by date, so any tips on sliders or call backs to reduce array size would be greatly appreciated

# Import dependencies
import dash
import dash_core_components as dcc
import dash_html_components as html
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, 4, 5], y=[1, 4, 9, 16, 25], name="yaxis data"),
    secondary_y=False,
)

# Add traces
fig.add_trace(
    go.Scatter(x=[1, 2, 3, 4, 5], y=[2, 5, 10, 17, 26], name="yaxis data"),
    secondary_y=False,
)

# Add traces
fig.add_trace(
    go.Scatter(x=[1, 2, 3, 4, 5], y=[3, 6, 11, 18, 27], name="yaxis data"),
    secondary_y=False,
)

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

fig.add_trace(
    go.Scatter(x=[1, 2, 3, 4, 5], y=[2, 7, 28, 65, 126], name="yaxis2 data"),
    secondary_y=True,
)

fig.add_trace(
    go.Scatter(x=[1, 2, 3, 4, 5], y=[2, 7, 28, 65, 126], 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.write_html('first_figure.html', auto_open=True)


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.H1(children='Test - Dashboard'),

    html.Div(children='''
        Sample Data
    '''),

    dcc.Graph(
        id='Sample Data',
        figure=fig
    )
]
)

if __name__ == '__main__':
    app.run_server(debug=True)

question from:https://stackoverflow.com/questions/65927371/dash-app-multi-line-chart-on-two-axes-using-plotly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...