Search code examples
pythonplotlyaxesplotly-pythonstretch

Consistent and Reproducable Axes Spacing


I am trying to create a gif made out of plotly graphs. And this works surprisingly well! The only problem I’m having, is that I haven’t found the correct setting for fixing the stretching of the axes.

So far, I was able to create this gif (I cannot post the image, because of insufficient reputation, sorry!).

As you can see, in the beginning of the gif, the x-axis is “stretched” out, compared to other frames. The range is constant (-400 to 400), but the same number of distance takes up different amount of space.

This is the layout code I am using for each frame:

    fig.update_layout(autosize=False,
                  width=700, height=500,
                  margin=dict(l=80, r=80, t=100, b=80),
                  showlegend=False,
                  font_family="serif",
                  font_color="black",
                  title_font_family="Computer Modern",
                  title_font_color="black",
                  scene = dict(
                          xaxis = dict(gridcolor="#b8b8b8",
                                       showbackground=False,
                                       zerolinecolor="black",
                                       autorange=False,
                                       range=[-400, 400],
                                       rangemode="nonnegative"),
                          yaxis = dict(gridcolor="#b8b8b8",
                                       showbackground=False,
                                       zerolinecolor="black",
                                       autorange=False,
                                       range=[-20, 20],
                                       rangemode="nonnegative"),
                          zaxis = dict(gridcolor="#b8b8b8",
                                       showbackground=False,
                                       zerolinecolor="black",
                                       autorange=False,
                                       range=[0, 70],
                                       rangemode="nonnegative"),
                          xaxis_title=r"x",
                          yaxis_title=r"y",
                          zaxis_title=r"z",
                          camera = dict(eye={"x": -1.25, "y": 1.25, "z": 1.25})
                  ))

I already tried setting autosize to false, and hoped that specifying the axes range would work. But sadly, it does not.

Does anyone know which setting fixes the "stretch" of an axis or how to make that constant?


Solution

  • You can use the Fixed Ratio Axes:

    # fix the ratio in the top left subplot to be a cube
    fig.update_layout(scene_aspectmode='cube')
    # manually force the z-axis to appear twice as big as the other two
    fig.update_layout(scene2_aspectmode='manual',
                      scene2_aspectratio=dict(x=1, y=1, z=2))
    # draw axes in proportion to the proportion of their ranges
    fig.update_layout(scene3_aspectmode='data')
    # automatically produce something that is well proportioned using 'data' as the default
    fig.update_layout(scene4_aspectmode='auto')