PlotlyJS: how to change the aspect ratio of a 3D plot

I have tried to change the aspect ratio of a typical 3D plot using PlotlyJS. I searched but found no helpful answer to my problem.

The MWE below produces a tall and thin cube with the surface in. I want to change the aspect ratio of the x,y,z-axis. I need the x,y-axis to be wider like the PGFPlotsX plot displayed below. I tried many ways but had no success. By the way, in PlotlyJS, how can I get rid of the colorbar if I want to? Thanks.

MWE

using PlotlyJS
K1 = LinRange(0.0, 10.0, 100);
L1 = LinRange(0.0, 10.0, 100);
	
Quant = 2 .*(K1.^(0.4) .* (L1.^(0.6))')
	
trace3_5 =  surface(x = K1, y = L1, z = Quant)
		
layout3_5 = Layout(#font_size = 13, 
		hovermode="x",	
	   	title_text="A standard production function", 
		title_x =0.5,
	   	titlefont_size="18",
		layout_scene = "cube",
		cube=(x=3, y=3, z=1),		
		#width=1000, height=400,
		colorbar = false) 
		
p3_5 = Plot([trace3_5], layout3_5)

PGFPlotsX type of plot I need:

Have you tried this post’s solution?

1 Like

@rafael.guerra’s suggestion is the right one, but you have to remove a few layout settings in your code, because they are ineffective

layout_scene = "cube",
cube=(x=3, y=3, z=1),	

To view the surface as in your attached image, it must be rotated:

using PlotlyJS
K1 = LinRange(0.0, 10.0, 100);
L1 = LinRange(0.0, 10.0, 100);

Quant = 2 .*(K1.^(0.4) .* (L1.^(0.6))')

trace3_5 =  surface(x = -L1, 
                    y = K1,  
                    z = Quant,  
                    showscale=false) #The surface is rotated with pi/2 
                                     #about z-axis

layout3_5 = Layout( 
            width=1000, height=800,
            title_text="A standard production function",
            title_x =0.5,
            titlefont_size="18",
            scene_aspectratio=attr(x=1, y=1, z=0.5))
p3_5 = Plot([trace3_5], layout3_5)
2 Likes

@rafael.guerra , @empet, thank you very much.

The mess I had inside the Layout settings displayed my unfortunate tries to follow the information in the Plotly documentation. It is perfect for my needs.

Thanks to @rafael.guerra for pointing out a link where there was a solution, and thanks to @empet for providing the solution.

Hi @empet,

Sorry for bothering you again with this 3D plot thing. I forgot to ask how to change the (x,y,z) axis titles. In the 2D case yaxis_title = "Capital (K)", xaxis_title = "Labor (L)" work, but in the 3D they don’t.

layout3_5 = Layout( 
                width=800, height=500,
                title_text="A standard production function",
	            yaxis_title = "Capital (K)",
		        xaxis_title = "Labor (L)",
		        zaxis_title = "Output (Q)",		
                title_x =0.5,
                titlefont_size="18",
                scene_aspectratio=attr(x=1, y=1, z=0.5))

I looked into the Plotly documentation and found the settings below. However, I tried but had no success adapting it to PlotlyJS. Thanks.

fig.update_layout(scene = dict(
                    xaxis_title='X AXIS TITLE',
                    yaxis_title='Y AXIS TITLE',
                    zaxis_title='Z AXIS TITLE'),
                    width=700,
                    margin=dict(r=20, b=10, l=10, t=10))
2 Likes

@empet, I have done it. Thanks.

Edited: In case anyone needs this information, here is how I have done it:

layout3 = Layout( 
            width=800, height=500,
			scene = attr(
                    xaxis_title="K",
                    yaxis_title="L",
                    zaxis_title="Q"),	
            title_x =0.5,
            titlefont_size="18",
            scene_aspectratio=attr(x=1, y=1, z=0.5))
3 Likes