1、条形图(柱状图)
绘制柱状图的相关API:
1 plt.figure('Bar', facecolor='lightgray')
2 plt.bar(
3 x, # 水平坐标数组
4 y, # 柱状图高度数组
5 width, # 柱子的宽度
6 bottom, # 柱子的底部基准位置
7 color='', # 填充颜色
8 label='', # 标签
9 alpha=0.2 # 透明度
示例:
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 apples = np.random.randint(10, 30, size=10)
5 oranges = np.random.randint(50, 70, size=10)
6
7 plt.figure('Bar', facecolor='lightgray')
8 plt.title('Bar', fontsize=14)
9 plt.xlabel('Month', fontsize=14)
10 plt.ylabel('Data', fontsize=14)
11 plt.grid(linestyle=':', axis='y')
12 x = np.arange(apples.size)
13 plt.bar(x - 0.2, apples, 0.4, color='fuchsia', label='Apple', align='center')
14 y = np.arange(oranges.size)
15 plt.bar(y + 0.2, oranges, 0.4, color='lime', label='orange', align='center')
16 plt.xticks(x, ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'])
17 plt.legend(loc='best')
18 plt.savefig('images/bar.png')
19 plt.show()
运行结果:
2、饼图
绘制饼状图的基本API:
1 plt.pie(
2 values, # 值列表
3 spaces, # 扇形之间的间距列表
4 labels, # 标签列表
5 colors, # 颜色列表
6 '%d%%', # 标签所占比例格式
7 shadow=True, # 是否显示阴影
8 startangle=90 # 逆时针绘制饼状图时的起始角度
9 radius=1 # 半径
示例:
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 values = [25, 71, 38, 29, 16]
5 spaces = [0.1, 0.1, 0.1, 0.1, 0.1]
6 labels = ['Java', 'Javascript', 'Python', 'PHP', 'C++']
7 colors = ['dodgerblue', 'orangered', 'limegreen', 'cyan', 'gold']
8
9 plt.figure('Pie', facecolor='lightgray')
10 plt.axis('equal')
11 plt.pie(values, spaces, labels, colors, '%.2f%%', shadow=True, radius=1, startangle=90)
12 plt.legend(loc='best')
13 plt.savefig('images/pie.png')
14 plt.show()
运行结果:
3、等高线图
绘制等高线图的基本API:
1 cntr = plt.contour(
2 x, # 网格坐标矩阵的x坐标 (2维数组)
3 y, # 网格坐标矩阵的y坐标 (2维数组)
4 z, # 网格坐标矩阵的z坐标 (2维数组)
5 8, # 把等高线绘制成8部分
6 colors='black', # 等高线的颜色
7 linewidths=0.5 # 线宽
8 )
9
10 # 为等高线图添加高度标签
11 plt.clabel(cntr, inline_spacing=1, fmt='%.1f',fontsize=10)
12 plt.contourf(x, y, z, 8, cmap='jet')
示例:
import numpy as np
import matplotlib.pyplot as plt
n = 500
x, y = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 3, n))
# print(x)
# print(y)
z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
plt.figure('Contour', facecolor='lightgray')
plt.title('Contour', fontsize=16)
cont = plt.contour(x, y, z, 8, colors='black', linewidths=0.75)
plt.clabel(cont, inline_spacing=5, fmt='%.1f', fontsize=10)
plt.contourf(x, y, z, 8, cmap='Pastel1')
plt.savefig('images/contour.png')
plt.show()
运行结果:
4、热成像图
绘制热成像图的基本API:
1 # origin: 坐标轴方向
2 # upper: 缺省值,原点在左上角
3 # lower: 原点在左下角
4 plt.imshow(z, cmap='jet', origin='low')
示例:
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 n = 500
5 x, y = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 3, n))
6 # print(x)
7 # print(y)
8 z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
9
10 # 绘制热成像图
11 plt.figure('Contour', facecolor='lightgray')
12 plt.title('Contour', fontsize=16)
13 cont = plt.contour(x, y, z, 8, colors='black', linewidths=0.75)
14 plt.imshow(z, cmap='jet', origin='lower')
15 plt.colorbar()
16 plt.savefig('images/imshow.png')
17 plt.show()
运行结果:
5、极坐标图
绘制极坐标图的基本API:
plt.gca(projection='polar')
示例:
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 theta = np.linspace(0, 4 * np.pi, 1000)
5 r = 0.8 * theta
6 plt.figure("Polar", facecolor='lightgray')
7 plt.gca(projection='polar')
8 plt.title('Polar', fontsize=16)
9 plt.xlabel(r'$ heta$')
10 plt.ylabel(r'$
ho$')
11 plt.grid(linestyle=':')
12 plt.plot(theta, r)
13 x = np.linspace(0, 6 * np.pi, 1000)
14 y = 3 * np.sin(6 * x)
15 plt.plot(x, y)
16 plt.savefig('images/polar.png')
17 plt.show()
运行结果:
6、3D图形
(1)、3D散点图
3D散点图绘制基本API:
1 from mpl_toolkits.mplot3d import axes3d
2 ax3d = plt.gca(projection='3d') # class axes3d
3
4 ax3d.scatter(..) # 绘制三维点阵
5 ax3d.scatter(
6 x, # x轴坐标数组
7 y, # y轴坐标数组
8 z, # z轴坐标数组
9 marker='', # 点型
10 s=10, # 大小
11 zorder='', # 图层序号
12 color='', # 颜色
13 edgecolor='', # 边缘颜色
14 facecolor='', # 填充色
15 c=v, # 颜色值 根据cmap映射应用相应颜色
16 cmap='' #
17 )
示例:
1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import axes3d
4
5 n = 300
6 x = np.random.normal(0, 1, n)
7 y = np.random.normal(0, 1, n)
8 z = np.random.normal(0, 1, n)
9
10 plt.figure('3D Points', facecolor='lightgray')
11 ax3d = plt.gca(projection='3d')
12 ax3d.set_xlabel('X')
13 ax3d.set_ylabel('Y')
14 ax3d.set_zlabel('Z')
15 d = x**2 + y**2 + z**2
16 ax3d.scatter(x, y, z, s=60, alpha=0.7, c=d,cmap='jet')
17 plt.tight_layout()
18 plt.savefig('images/3dscatter.png')
19 plt.show()
运行结果:
(2)、3D平面图
绘制3D平面图的API:
1 ax3d.plot_surface(
2 x, # 网格坐标矩阵的x坐标 (2维数组)
3 y, # 网格坐标矩阵的y坐标 (2维数组)
4 z, # 网格坐标矩阵的z坐标 (2维数组)
5 rstride=30, # 行跨距
6 cstride=30, # 列跨距
7 cmap='jet' # 颜色映射
8 )
示例:
import numpy as np
import matplotlib.pyplot as mp
from mpl_toolkits.mplot3d import axes3d
n = 1000
x, y = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 3, n))
z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
mp.figure('3D Surface', facecolor='lightgray')
ax3d = mp.gca(projection='3d')
ax3d.plot_surface(x, y, z, cstride=20,rstride=20, cmap='Pastel1')
mp.tight_layout()
mp.savefig('images/3dsurface.png')
mp.show()
运行结果:
(3)、3D线框图
绘制3D线框图的API:
1 # rstride: 行跨距
2 # cstride: 列跨距
3 ax3d.plot_wireframe(x,y,z,rstride=30,cstride=30, linewidth=1, color='dodgerblue')
示例:
1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import axes3d
4
5 n = 1000
6 x, y = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 3, n))
7 z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
8
9 plt.figure('3D Wireframe', facecolor='lightgray')
10 ax3d = plt.gca(projection='3d')
11 ax3d.plot_wireframe(x, y, z, cstride=20,rstride=20, linewidth=0.5,color='orangered')
12 plt.tight_layout()
13 plt.savefig('images/3dwireframe.png')
14 plt.show()
运行结果:
7、直方图
绘制直方图的基本API:
1 n, bins, patches = plt.hist(
2 x, # 这个参数是指定每个bin(箱子)分布的数据,对应x轴
3 50, # 这个参数指定bin(箱子)的个数,也就是总共有几条条状图
4 density=1, # bool值,默认为false,显示的是频数统计结果
5 edgecolor='navy',
6 facecolor='lime',
7 alpha=0.75
8 ) # 返回值n为数组或数组列表每一个bar区间的数量或者百分比,bins为表示bar的范围的数组,patches为表示图形对象的列表
示例:
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 mu = 100
5 sigma = 15
6 x = mu + sigma * np.random.randn(10000)
7 plt.figure('Hist', facecolor='lightgray')
8 plt.title('Histogram of IQ', fontsize=16)
9 n, bins, patches = plt.hist(
10 x, 50, density=True, edgecolor='slategray', facecolor='lime', alpha=0.75
11 )
12 plt.xlabel('Smarts')
13 plt.ylabel('Probability')
14 plt.text(60, .025, r'$mu=100, sigma=15$')
15 plt.axis([40, 160, 0, 0.03])
16 plt.plot(bins[1:]-1, n, color='orangered')# 以每个区间的中点绘制折线
17 plt.grid(linestyle=':')
18 plt.tight_layout()
19 plt.savefig('images/hist.png')
20 plt.show()
运行结果: