Plotly surface plot with unequal length x and y axes

Viewed 615

I have a 10x12 array that I would like to plot using plotly. The length of x-axis is 10 and y-axis is 12. I expect the plot to have a range of 10 in the x-axis and 12 in the y-axis. But that is not the case. I get 10 on both axes. I have included a MWE to reproduce the issue.

import numpy as np
import plotly.graph_objects as go

data_array = np.empty((10, 12))
for i in range(10):
    for j in range(12):
        data_array[i, j] = i+j

x = np.linspace(1, 10, 10)
y = np.linspace(1, 12, 12)

fig = go.Figure(data=[go.Surface(z=data_array, x=x, y=y)])
fig.show()

The plot that it generates is enter image description here

I have tried setting the range via update_layout which doesn't solve the problem.

import numpy as np
import plotly.graph_objects as go

data_array = np.empty((10, 12))
for i in range(10):
    for j in range(12):
        data_array[i, j] = i+j

x = np.linspace(1, 10, 10)
y = np.linspace(1, 12, 12)

fig = go.Figure(data=[go.Surface(z=data_array, x=x, y=y)])
fig.update_layout(scene=dict(xaxis=dict(range=[0, 10]),
                             yaxis=dict(range=[0, 12])))
fig.show()

enter image description here

Although the range of y-axis in now till 12, there is nothing plotted in that part of the graph. How can I get this to work? Thanks.

Based on the answer below, I have changed the code.

import numpy as np
import plotly.graph_objects as go

data_array = np.empty((10, 12))
for idx1, i in enumerate(range(0, 50, 5)):
    for idx2, j in enumerate(range(0, 24, 2)):
        data_array[idx1, idx2] = (i+j) / (50+24)

x = np.linspace(0, 45, 10)
y = np.linspace(0, 22, 12)

fig = go.Figure(data=[go.Surface(z=data_array.T)])
fig.show()

But the axes now have no correspondence with the actual x and y values. The values in the plot go from 0 to 9 and 0 to 11 for x and y axes respectively. How do I set the calibration? Thanks.

1 Answers

The matrix data_array should be transposed (x along columns, y along rows). The limit can be explicitly set by passing the x, y arguments. The code is as follows:

import numpy as np
import plotly.graph_objects as go

data_array = np.empty((10, 12))
for idx1, i in enumerate(range(0, 50, 5)):
    for idx2, j in enumerate(range(0, 24, 2)):
        data_array[idx1, idx2] = (i+j) / (50+24)

x = np.linspace(0, 45, 10)
y = np.linspace(0, 22, 12)

fig = go.Figure(data=[go.Surface(z=data_array.T, x=x, y=y)])
fig.show()

I am currently unable to save the image with the tag on the last position. The image obtained is as follows:

enter image description here

Documentation: https://plotly.com/python/3d-surface-plots/

Related