How to make a python plot interactive

Advertisements

I’m trying to make a plot that I have done in 3D interactive so that I can move it in order to see it from other angles.

cut_k = 0.35
height = 3

#slice at k = 0.08

#Create data for the cylinder
theta = np.linspace(0, 2 * np.pi, 1000)
z_cylinder = np.linspace(0, height, 1000)
theta, z_cylinder = np.meshgrid(theta, z_cylinder)
x_cylinder = cut_k* np.cos(theta)
y_cylinder = cut_k* np.sin(theta)

#plot surface
fig = plt.figure(figsize=(7, 7))
axes = fig.add_subplot(111, projection='3d')
axes.plot_surface(x_cylinder, y_cylinder, z_cylinder, color='red', alpha=1)

This is my code.

>Solution :

I usually use %matplotlib widget to changed the Matplotlib plot to be interactive. However, if you then want to plot a 2d graph you might want to run %matplotlib inline which will kind of reset it. Might not be the most efficient way but it always works for me. I hope this helps.

Leave a ReplyCancel reply