matplotlib可视化之饼图、三维图
作者:互联网
饼图
import matplotlib.pyplot as plt labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] # 设置分离的距离,0表示不分离 explode = (0, 0.1, 0, 0) plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) # Equal aspect ratio 保证画出的图是正圆形 plt.axis('equal') plt.show()
三维图
绘制三维散点图
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D data = np.random.randint(0, 255, size=[30, 30, 30]) x, y, z = data[0], data[1], data[2] ax = plt.subplot(111, projection='3d') # 创建一个三维的绘图工程 # 将数据点分成三部分画, 在颜色上有区分度 ax.scatter(x[:1], y[:1], z[:1], c='y') # 绘制数据点 ax.scatter(x[10:20], y[10:20], z[10:20], c='r') ax.scatter(x[20:30], y[20:30], z[20:30], c='g') ax.set_zlabel('Z') # 坐标轴 ax.set_ylabel('Y') ax.set_xlabel('X') plt.show()
绘制三维平面图
from matplotlib import pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) X = np.arange(-4, 4, 0.25) Y = np.arange(-4, 4, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) # 具体函数方法可用 help(function) 查看,如:help(ax.plot_surface) ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow') plt.show()
参考链接:掘金-Python绘制六种可视化图表详解
标签:plt,20,30,matplotlib,三维图,import,np,ax,之饼 来源: https://www.cnblogs.com/lfri/p/12251990.html