前言
本文其实属于:Python的进阶之道【AIoT阶段一】的一部分内容,本篇把这部分内容单独截取出来,方便大家的观看,本文介绍Matplotlib数据可视化进阶,读本文之前,如果没有 Matplotlib基础建议先看博客:Matplotlib数据可视化入门,Matplotlib数据可视化高级。
🌟 学习本文之前,需要先自修:NumPy从入门到进阶,pandas从入门到进阶本文中很多的操作在 NumPy从入门到进阶 ,pandas从入门到进阶二文中有详细的介绍,包含一些软件以及扩展库,图片的安装和下载流程,本文会直接进行使用。
下载Matplotlib 见博客:matplotlib的安装教程以及简单调用,这里不再赘述
1.常用视图
1.1 折线图
import numpy as np import matplotlib.pyplot as plt y = np.random.randint(0, 10, size = 15) # 一图多线 plt.figure(figsize = (9, 6)) # 只给了y,不给x,则x有默认值:0、1、2、3、... plt.plot(y, marker = '*', color = 'r') plt.plot(y.cumsum(), marker = 'o') # 多图布局 fig,axs = plt.subplots(2, 1) # 设置宽高 fig.set_figwidth(9) fig.set_figheight(6) axs[0].plot(y, marker = '*', color = 'red') axs[1].plot(y.cumsum(), marker = 'o')
1.2 柱状图
1.2.1 堆叠柱状图
import numpy as np import matplotlib.pyplot as plt labels = ['G1', 'G2', 'G3', 'G4', 'G5','G6'] # 级别 # 生成数据 men_means = np.random.randint(20, 35, size = 6) women_means = np.random.randint(20, 35, size = 6) men_std = np.random.randint(1, 7, size = 6) women_std = np.random.randint(1, 7, size = 6) width = 0.35 # 柱状图中柱的宽度 plt.bar(labels, # 横坐标 men_means, # 柱高 width, # 线宽 yerr = men_std, # 误差条(标准差) label = 'Men') # 标签 plt.bar(labels, women_means, width, yerr = women_std, bottom = men_means, # 把女生画成男生的上面 # 没有上一行代码柱状图会发生重叠覆盖,读者可以自行尝试 label = 'Women') plt.ylabel('Scores') plt.title('Scores by group and gender') plt.legend()
1.2.2 分组带标签柱状图
import matplotlib import matplotlib.pyplot as plt import numpy as np # 创造数据 labels = ['G1', 'G2', 'G3', 'G4', 'G5','G6'] # 级别 men_means = np.random.randint(20, 35,size = 6) women_means = np.random.randint(20, 35,size = 6) x = np.arange(len(men_means)) plt.figure(figsize = (9, 6)) # 把男生的柱状图整体左移 width / 2 rects1 = plt.bar(x - width / 2, men_means, width) # 把女生的柱状图整体右移 width / 2 rects2 = plt.bar(x + width / 2, women_means, width) # 设置标签标题,图例 plt.ylabel('Scores') plt.title('Scores by group and gender') plt.xticks(x, labels) plt.legend(['Men','Women']) # 放置文本 text def set_label(rects): for rect in rects: height = rect.get_height() # 获取高度 plt.text(x = rect.get_x() + rect.get_width() / 2, # 水平坐标 y = height + 0.5, # 竖直坐标 s = height, # 文本 ha = 'center') # 水平居中 set_label(rects1) set_label(rects2) # 设置紧凑布局 plt.tight_layout()
1.3 极坐标图
1.3.1 线性极坐标
🚩对于极坐标,我们先来绘制一个普通的直角坐标系下的直线
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4 * np.pi, 200) y = np.linspace(0, 2, 200) plt.plot(x, y)
我们对上述代码加上一行代码后,就可以转为极坐标:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4 * np.pi, 200) y = np.linspace(0, 2, 200) plt.subplot(111, projection = 'polar') plt.plot(x, y)
接下来,为了让这个极坐标图更加的美观,我们对其属性进行一些设置:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4 * np.pi, 200) y = np.linspace(0, 2, 200) ax = plt.subplot(111, projection = 'polar', facecolor = 'lightgreen') plt.plot(x, y) # 设置 ax.set_rmax(3) # 最大半径设置为3 ax.set_rticks([0.5, 1, 1.5, 2]) # 设置刻度 ax.grid(True) # 设置网格线
1.3.2 条形极坐标
import numpy as np import matplotlib.pyplot as plt # 分成8份 (0~360) N = 8 # 横坐标 x = np.linspace(0.0, 2 * np.pi, N, endpoint = False) # 纵坐标 y = np.random.randint(3, 15, size = N) # 宽度(8个柱子沾满圆) width = np.pi / 4 # 8个柱子随机生成颜色 colors = np.random.rand(8,3) # polar表示极坐标 ax = plt.subplot(111, projection = 'polar') ax.bar(x, y, width = width, color = colors)