[Solved] How to use matplotlib to draw 3D surface graphs in Python (freely rotatable 3D graphs)

1. Demand

When doing the electromagnetic design of the motor, some parameters need to be optimized, so the data is exported from the electromagnetic simulation software Maxwell, and some data are shown in the following figure.
insert image description here
However, it is impossible to visually see the influence of the parameters, so it is adjusted to a matrix form, as shown in the following figure.
insert image description here
In this way, although the influence of input parameters (current and diameter) on output parameters (torque) can be seen intuitively, it is not beautiful enough, so it is hoped that a three-dimensional map can be produced.

So our requirement is to draw the above table as a three-dimensional map and display it. MATLAB or Python can be used to draw three-dimensional diagrams. Since this computer cannot carry MATLAB, I choose a relatively lightweight and free Python to draw.

2. Effects

insert image description here

3. Source code

# -*- coding: utf-8 -*-
"""
Created on Wed Nov  4 11:50:31 2021

@author: 29235
"""

import matplotlib.pyplot as plt
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

X = np.array([40, 40.5, 41, 41.5, 42, 42.5, 43, 43.5, 44, 44.5, 45, 45.5, 46])
Y = np.array([73, 73.4, 73.8, 74.2, 74.6, 75])
X, Y = np.meshgrid(X, Y)

print("X维度信息",X.shape)
print("Y维度信息",Y.shape)

Z = np.array([[6.589055889,6.64902114,6.708506801,6.76873195,6.826929116,6.883863945,6.941234139,7.000980041,7.054255481,7.109803791,7.164581639,7.218312404,7.270706248
], 
             [6.791371118,6.852543525,6.913251843,6.973519432,7.033344309,7.092707208,7.151614187,7.210021177,7.267848299,7.324956649,7.381181255,7.436250947,7.489788889
], 
             [6.831692483,6.89286339,6.953606738,7.013941807,7.073861088,7.133397721,7.192511521,7.251186927,7.309458728,7.367340603,7.424484379,7.481372044,7.537841682
], 
             [7.086868085,7.149849934,7.212869543,7.275419973,7.337549343,7.399451263,7.461001477,7.522183123,7.582986224,7.643290063,7.703127756,7.762594775,7.821019196
], 
             [7.294926321,7.363130314,7.430014234,7.485656198,7.539103262,7.603323023,7.666993121,7.73028388,7.792974692,7.855100171,7.916647003,7.977528019,8.037752676
], 
             [7.694961389,7.766138728,7.83685522,7.907105954,7.976906299,8.046261641,8.114924107,8.183130388,8.250939779,8.318071108,8.384670594,8.450760983,8.516177871
], 
])

print("Z轴数据维度",Z.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
ax.set_xlabel('I(A)', color='b')
ax.set_ylabel('D(mm)', color='g')
ax.set_zlabel('T(Nm)', color='r')

plt.draw()
plt.show()

I wrote this code in spyder, because when running the code with spyder, the generated image will be drawn using spyder's Plots by default, so the resulting image is a static image and cannot be rotated.
insert image description here
It is said on the Internet that the image viewer that comes with matplotlib can rotate and view the three-dimensional image, but I have not found a way to draw without Plots in spyder.

So I thought of a way.

4. Special requirements (free rotation)

Open a command line and change directory to the directory where the drawing code is located.
insert image description here
Activate the environment. (Because I use a self-built environment to process data, I need to activate the environment. If you use the default environment to draw, you don't need this step)
insert image description here
Enter the command python I_D_T_Data_Process.py
insert image description here
to run the .py file directly, which will call The graphics display that comes with matplotlib. At this point you can freely rotate the 3D image.
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/mahoon411/article/details/121139438