Python可视化进阶:使用matplotlib绘制三维图形的详细指南(二)
一、绘制线形图
Axes3D.plot 函数用来绘制线形图。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
x = np.linspace(-10, 10, 1000)
y = np.linspace(-10, 10, 1000)
z = np.add(x, y)
ax.plot(x, y, z)
plt.show()
二、散点图
Axes3D.scatter 函数用来绘制散点图。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
count = 100
range1 = 100
xs = np.random.rand(count) * range1
ys = np.random.rand(count) * range1
zs = np.random.rand(count) * range1
ax.scatter(xs, ys, zs, s=zs, c=zs)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
三、线框图
Axes3D.plot_wireframe 函数用来绘制线框图。
# meshgrid_demo.py
import numpy as np
x = np.arange(1, 4)
y = np.arange(11, 16)
print(x)
print(y)
X, Y = np.meshgrid(x, y)
print(X)
print(Y)
[1 2 3]
[11 12 13 14 15]
[[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]]
[[11 11 11]
[12 12 12]
[13 13 13]
[14 14 14]
[15 15 15]]
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
x = np.arange(-10, 10, 0.1)
y = np.arange(-10, 10, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.add(-np.power(X, 3), np.power(Y, 4))
surf = ax.plot_wireframe(X, Y, Z)
plt.show()
四、曲面图
Axes3D.plot_surface 函数用来绘制曲面图。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
x = np.arange(-10, 10, 0.1)
y = np.arange(-10, 10, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.add(-np.power(X, 3), np.power(Y, 2))
surf = ax.plot_surface(X, Y, Z, cmap=cm.gist_rainbow)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
五、等高线
Axes3D.contour 函数用来绘制等高线。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
x = np.arange(-10, 10, 0.1)
y = np.arange(-10, 10, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.add(-np.power(X, 4), np.power(Y, 4))
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.plot_wireframe(X, Y, Z, alpha=0.1)
ax.contour(X, Y, Z, cmap=cm.Accent, linewidths=2)
plt.show()
六、柱状图
Axes3D.bar 函数用来绘制柱状图。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
np.random.seed(59)
month = np.arange(1, 12)
years = [2016, 2017, 2018, 2019]
def get_color(value_array):
color = []
for v in value_array:
if (v < 50):
color.append('y')
elif (v < 100):
color.append('g')
elif (v < 150):
color.append('b')
elif (v < 200):
color.append('c')
elif (v < 250):
color.append('m')
else:
color.append('r')
return color
for year, c in zip(years, ['b','c','r','m']):
value = np.random.rand(len(month)) * 300
ax.bar(month, value, year, zdir='y', color=get_color(value), alpha=0.7)
for i in np.arange(0, 12):
ax.bar
ax.set_xlabel('Month')
ax.set_xticks(np.arange(1, 13))
ax.set_ylabel('Year')
ax.set_yticks(np.arange(2016, 2020))
ax.set_zlabel('Precipitation')
plt.show()
七、多边形
Axes3D.add_collection3d 函数用来向图形中添加3D集合对象。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
np.random.seed(59)
month = np.arange(0, 13)
years = [2016, 2017, 2018, 2019]
precipitation = []
for year in years:
value = np.random.rand(len(month)) * 300
value[0], value[-1] = 0, 0
precipitation.append(list(zip(month, value)))
poly = PolyCollection(precipitation, facecolors=['b','c','r','m'])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=years, zdir='y')
ax.set_xlabel('Month')
ax.set_xlim3d(0, 12)
ax.set_ylabel('Year')
ax.set_ylim3d(2015, 2020)
ax.set_zlabel('Precipitation')
ax.set_zlim3d(0, 300)
plt.show()
八、制作动图
很多时候,我们可能需要制作一张动画图来展示图形的全貌,下面我们就来看一下如何做到。
为了制作动图,我们需要先有制作动图的图片素材。
下面我们就以前面等高线那个函数生成的复杂图形为例,来看看如何生成一个关于这个图形不同角度的动图。
相关代码如下:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(projection='3d')
x = np.arange(-10, 10, 0.1)
y = np.arange(-10, 10, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.add(-np.power(X, 4), np.power(Y, 4))
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.plot_surface(X, Y, Z, cmap=cm.hsv)
for angle in range(95, 180, 3):
ax.set_zlabel("Angle: " + str(angle))
ax.view_init(30, angle)
filename = "./pic/" + str(angle) + ".png"
plt.savefig(filename)
print("Save " + filename + " finish")
Save ./pic/95.png finish
Save ./pic/98.png finish
Save ./pic/101.png finish
Save ./pic/104.png finish
Save ./pic/107.png finish
Save ./pic/110.png finish
Save ./pic/113.png finish
Save ./pic/116.png finish
Save ./pic/119.png finish
Save ./pic/122.png finish
Save ./pic/125.png finish
Save ./pic/128.png finish
Save ./pic/131.png finish
Save ./pic/134.png finish
Save ./pic/137.png finish
Save ./pic/140.png finish
Save ./pic/143.png finish
Save ./pic/146.png finish
Save ./pic/149.png finish
Save ./pic/152.png finish
Save ./pic/155.png finish
Save ./pic/158.png finish
Save ./pic/161.png finish
Save ./pic/164.png finish
Save ./pic/167.png finish
Save ./pic/170.png finish
Save ./pic/173.png finish
Save ./pic/176.png finish
Save ./pic/179.png finish
作者:谷风的世界