1.二维平面函数绘图
1.基础版(y=x^2)
#抛物线
import numpy as np
import matplotlib.pyplot as plt #导入matplotlib库
x = np.arange(-100,100,0.1) #x步长为0.1
y = x**2
plt.xlim(-12,12) #设置x、y坐标轴的范围
plt.ylim(0, 100)
plt.plot(x,y)
plt.show()
2.添加画布和坐标轴箭头
import numpy as np
import matplotlib.pyplot as plt #导入matplotlib库
import mpl_toolkits.axisartist as axisartist
fig = plt.figure(figsize=(8, 8)) #创建画布
ax = axisartist.Subplot(fig, 111) #使用axisartist.Subplot方法创建一个绘图区对象ax
fig.add_axes(ax) #将绘图区对象添加到画布中
ax.axis[:].set_visible(False) #通过set_visible方法设置绘图区所有坐标轴隐藏
ax.axis["x"] = ax.new_floating_axis(0,0) #ax.new_floating_axis代表添加新的坐标轴
ax.axis["x"].set_axisline_style("->", size = 1.0) #给x坐标轴加上箭头
ax.axis["y"] = ax.new_floating_axis(1,0) #添加y坐标轴,且加上箭头
ax.axis["y"].set_axisline_style("-|>", size = 1.0)
ax.axis["x"].set_axis_direction("top") #设置x、y轴上刻度显示方向
ax.axis["y"].set_axis_direction("right")
x = np.arange(-100,100,0.1) #x步长为0.1
y = x**2
plt.xlim(-12,12) #设置x、y坐标轴的范围
plt.ylim(0, 100)
plt.plot(x,y)
plt.show()
2.三维函数绘图
参考链接:https://blog.csdn.net/Mr_Cat123/article/details/100054757
3d绘图汇总:http://c.biancheng.net/matplotlib/3d-plot.html
figure语法说明:
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
- num:图像编号或名称,数字为编号 ,字符串为名称
- figsize:指定figure的宽和高,单位为英寸; dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为801英寸等于2.5cm,A4纸是 21*30cm的纸张 facecolor:背景颜色 edgecolor:边框颜色
- frameon:是否显示边框
plt、fig、axes、axis的含义:https://blog.csdn.net/qq_38048756/article/details/117741677
1.基础版(z = x^2 + y^2)
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10,0.2)
y = np.arange(-10,10,0.2)
z = np.power(x,2)+np.power(y,2) #z = x**2 + y**2
fig = plt.figure() #生成一个figure画布
ax = plt.axes(projection='3d') #创建3d绘图区域
ax.plot(x,y,z)
plt.show()
3.建立几何模型
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
# print("请输入a1:")
a1 = 1
# print("请输入b1:")
b1 = 2
u = np.linspace(0, np.pi, 256, endpoint=True)
v = np.linspace(0, 2 * np.pi, 256, endpoint=True)
u, v = np.meshgrid(u, v)
def x(u, v):
return a1 * np.sin(u) * np.cos(v)
def y(u, v):
return a1 * np.sin(u) * np.sin(v)
def z(u):
return b1 * np.cos(u)
# x = a1*np.sin(u)*np.cos(v)
# y = a1*np.sin(u)*np.sin(v)
# z = b1*np.cos(u)
x = x(u, v)
y = y(u, v)
z = z(u)
fig = plt.figure() # 生成一个figure画布
ax = plt.axes(projection='3d') # 创建3d绘图区域
plt.gca().set_box_aspect((a1, a1, b1)) # 当x、y、z轴范围之比为3:5:2时。
ax.plot_wireframe(x, y, z)
ax.set_title('tuoqiu')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
https://vimsky.com/examples/usage/python-scipy.interpolate.griddata.html