Ask Your Question
4

How can I add space between the data and the border of a subplot with a secondary y axis and a fixed range using Plotly?

asked 2022-12-23 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2021-05-09 12:00:00 +0000

devzero gravatar image

To add space between the data and the border of a subplot with a secondary y axis and a fixed range using Plotly, you can use the ‘margin’ parameter to set the margins for the subplot. Setting the margin to a larger value will add more space between the data and the border. Here is an example:

import plotly.graph_objs as go

fig = go.Figure()

# add first trace
fig.add_trace(go.Scatter(
        x=[1, 2, 3],
        y=[4, 5, 6],
        name="trace 1",
        line=dict(color="blue")
))

# add second trace with secondary y axis
fig.add_trace(go.Scatter(
        x=[1, 2, 3],
        y=[10, 20, 30],
        name="trace 2",
        line=dict(color="red"),
        yaxis="y2"
))

# set fixed y-axis range for secondary y axis
fig.update_layout(
        yaxis2=dict(
                range=[0, 40],
                showgrid=False,
                zeroline=False,
                showticklabels=False
        )
)

# set margin for subplot
fig.update_layout(
        margin=dict(l=50, r=50, b=50, t=50, pad=4)
)
fig.show()

In the example above, the ‘margin’ parameter is set to 50 for all sides. This will add a 50-pixel margin between the data and the border of the subplot. You can adjust this value to create more or less space depending on your specific needs.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-23 11:00:00 +0000

Seen: 11 times

Last updated: May 09 '21