I am trying to learn mayavi2 for python and can not get my savefig to show my plots. I found this example code:
from numpy import pi, sin, cos, mgrid dphi, dtheta = pi / 250.0, pi / 250.0[phi, theta] = mgrid[0: pi + dphi * 1.5: dphi, 0: 2 * pi + dtheta * 1.5: dtheta] m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4; r = sin(m0 * phi) ** m1 + cos(m2 * phi) ** m3 + sin(m4 * theta) ** m5 + cos(m6 * theta) ** m7 x = r * sin(phi) * cos(theta) y = r * cos(phi) z = r * sin(phi) * sin(theta) # View it. from mayavi import mlab a = mlab.mesh(x, y, z) mlab.show()
Now I want to save the figure, so I add:
mlab.savefig(filename = 'test.png')
i trying learn mayavi2 python , can not savefig show plots. found example code:,this same issue results using matplotlib, the best option save before show. , python - mayavi mlab.savefig() gives an empty image - , mongodb - Error while setting up compound index -
i trying learn mayavi2 python , can not savefig show plots. found example code:
from numpy import pi, sin, cos, mgrid dphi, dtheta = pi / 250.0, pi / 250.0[phi, theta] = mgrid[0: pi + dphi * 1.5: dphi, 0: 2 * pi + dtheta * 1.5: dtheta] m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4; r = sin(m0 * phi) ** m1 + cos(m2 * phi) ** m3 + sin(m4 * theta) ** m5 + cos(m6 * theta) ** m7 x = r * sin(phi) * cos(theta) y = r * cos(phi) z = r * sin(phi) * sin(theta) # view it.mayavi import mlab a = mlab.mesh(x, y, z) mlab.show()
now want save figure, add:
mlab.savefig(filename = 'test.png')
The mayavi.mlab module provides simple plotting functions to apply to numpy arrays, similar to matplotlib or matlab’s plotting interface. Try using them in IPython, by starting IPython with the switch --gui=wx.,If you have built a visualization, using the mlab plotting functions, or the mlab.pipeline function, we can update the data by assigning new values to the mlab_source attributes,A surface is defined by points connected to form triangles or polygones. In mayavi.mlab.surf() and mayavi.mlab.mesh(), the connectivity is implicity given by the layout of the arrays. See also mayavi.mlab.triangular_mesh().,For the interaction with the user (for instance changing the view with the mouse), Mayavi needs some time to process these events. The for loop above prevents this. The Mayavi documentation details a workaround
x, y, z, value = np.random.random((4, 40)) mlab.points3d(x, y, z, value)
mlab.clf() # Clear the figure t = np.linspace(0, 20, 200) mlab.plot3d(np.sin(t), np.cos(t), 0.1 * t, t)
mlab.clf()
x, y = np.mgrid[-10: 10: 100 j, -10: 10: 100 j]
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(r) / r
mlab.surf(z, warp_scale = 'auto')
mlab.clf()
phi, theta = np.mgrid[0: np.pi: 11 j, 0: 2 * np.pi: 11 j]
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
mlab.mesh(x, y, z)
mlab.mesh(x, y, z, representation = 'wireframe', color = (0, 0, 0))
mlab.clf() x, y, z = np.mgrid[-5: 5: 64 j, -5: 5: 64 j, -5: 5: 64 j] values = x * x * 0.5 + y * y + z * z * 2.0 mlab.contour3d(values)
mesh(x, y, z, ...)
After this, we define data coordinates and use plt.scatter() method to draw a scatter plot.,After that we use plt.figure() method and plt.scatter() method to plot a scatter plot.,In the above example, we import matplotlib.pyplot library and define the data coordinates.,After this we use plt.show() method to visualize the plot on the screen.
Let’s see an example of the above-described problem:
# Import Library import matplotlib.pyplot as plt # Define Data student = [3, 2, 0, 5, 9, 1] weight = [30, 35, 40, 45, 50, 55] # Plot Graph plt.scatter(weight, student) # Define axes label plt.xlabel("Weight of the students") plt.ylabel("Number of students") # show plot plt.show() # Save image plt.savefig('figure not shown', dpi = 150)
Let’s have a look at a code:
# Import Library import matplotlib.pyplot as plt # Define Data student = [3, 2, 0, 5, 9, 1] weight = [30, 35, 40, 45, 50, 55] # Plot Graph plt.scatter(weight, student) # Define axes label plt.xlabel("Weight of the students") plt.ylabel("Number of students") # Save image plt.savefig('figure shown', dpi = 150) # show plot plt.show()
Let’s see the code:
# Import Library import matplotlib.pyplot as plt # Define Data student = [3, 2, 0, 5, 9, 1] weight = [30, 35, 40, 45, 50, 55] # Plot Graph fig = plt.figure() plt.scatter(weight, student) # Define axes label plt.xlabel("Weight of the students") plt.ylabel("Number of students") # show plot plt.show() # Save image fig.savefig('figure', dpi = 150)
Some answer related to saving: mayavi anycodings_mne-python mlab.savefig() gives an empty image is anycodings_mne-python not working well in VNC so far.,The new problem is that I cannot save anycodings_mne-python the mlab plot using either API or the anycodings_mne-python interactive scene. I only got a black anycodings_mne-python figure so far. ,I had the same problem days ago, after anycodings_mne-python exchanging emails with HPC staff, our anycodings_mne-python solution is quite simple:,I also recommend PyVista, which seems to anycodings_mne-python more pythonic in many aspects. I was anycodings_mne-python able to save all the plot in PyVista and anycodings_mne-python the plots are great.
The error I get when opening a mlab window anycodings_vnc is:
ERROR: In.. / Rendering / OpenGL2 / vtkOpenGLRenderWindow.cxx, line 754
vtkXOpenGLRenderWindow(0x556e13b32670): Unable to find a valid OpenGL 3.2 or later implementation.Please update your video card driver to the latest version.If you are using Mesa please make sure you have version 11.2 or later and make sure your driver in Mesa supports OpenGL 3.2 such as llvmpipe or openswr.If you are on windows and using Microsoft remote desktop note that it only supports OpenGL 3.2 with nvidia quadro cards.You can use other remoting software such as nomachine to avoid this issue.