💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ***** # 1\. Matplotlib [Matplotlib官网](https://matplotlib.org/) matplotlib是PYTHON绘图的基础库,是模仿matlab绘图工具开发的一个开源库。 PYTHON其它第三方绘图库都依赖与matplotlib。 本节课我们重点学习三种绘图方式: 1. matplotlib绘制基础图形 2. pandas plot API 3. seaborn绘制统计图形 我们可视化课程的重点是利用图形去理解数据,而不是注重图形的美观。因此本课程讲解的图形都是基于数据统计分析的简单图形,类似于雷达图这样的复杂图形不会在课程中讲解。 # 2\. Hello World ``` import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 #生成0到2pi的100个值,均等划分,最后放到X的数组里 X = np.linspace(0, 2*np.pi,100)# 均匀的划分数据 #根据正弦函数生成100个值放在Y数组里 Y = np.sin(X) Y1 = np.cos(X) #在plt的空白画布上添加标题 plt.title("Hello World!!") #在画布上描绘100个点 plt.plot(X,Y) #在画布上描绘100个点 plt.plot(X,Y1) #显示plt画布 plt.show() ``` ![](https://img.kancloud.cn/45/f4/45f49e6d8f33a456a5671c284002750c_509x341.png) ``` X = np.linspace(0, 2*np.pi,100) Y = np.sin(X) #将画布分成两部分,分别绘制两个图,第一部分 plt.subplot(211) # 等价于 subplot(2,1,1) plt.plot(X,Y) #将画布分成两部分,分别绘制两个图,第二部分 plt.subplot(212) #图形颜色是红色,Y值根据X数组值计算 plt.plot(X,np.cos(X),color = 'r') ``` ![](https://img.kancloud.cn/a1/54/a154e414abc08dac4f7da732afd4ff93_512x334.png) ***** # 3\. BAR CHART 条形图 ![](https://img.kancloud.cn/86/c2/86c275b87f63981fb29f3e1293686f68_838x305.png) ### 3.0.1. Verticle 垂直的 ``` #列表 data = [5,25,50,20] # 第一个参数列表是几个条形的x坐标,data是几个条形的y坐标 plt.bar(range(len(data)),data) ``` ![](https://img.kancloud.cn/2f/73/2f73a3a28a2743f16cd25b96fc013fff_612x408.png) ### 3.0.2. Horizontal 水平的 ``` data = [5,25,50,20] #barh()表示绘制水平的条形图。第一个参数列表是几个条形的y坐标,data是几个条形的x坐标 plt.barh(range(len(data)),data) ``` ![](https://img.kancloud.cn/e5/f0/e5f08a8ed35cc24d883e4cea9401f0b6_619x402.png) ### 3.0.3. 多个bar ``` # 有三组分类变量的条形图 data是它们的频数列表 data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) # label 标签 标注 #宽度:width = 0.25 。 label = "A" 这个分类变量的名字是A plt.bar(X + 0.00, data[0], color = 'b', width = 0.25,label = "A") # 第二组条形图紧挨着第一组,x坐标右移一个第一变量的宽度 plt.bar(X + 0.25, data[1], color = 'g', width = 0.25,label = "B") # 第三组条形图紧挨着第二组 plt.bar(X + 0.50, data[2], color = 'r', width = 0.25,label = "C") # legend 图例 图示 调用legend()才会显示分类变量标注 plt.legend() ``` ![](https://img.kancloud.cn/9d/d9/9dd97498ceb5c8af64c52f165caf7952_603x407.png) ***** ### 3.0.4. Stacked 堆 ``` data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) #三组分类变量的x坐标都相同,因为要堆叠起来 plt.bar(X, data[0], color = 'b', width = 0.25) # bottom = data[0] 第二组条形的底部是在第一组条形的高度 plt.bar(X, data[1], color = 'g', width = 0.25,bottom = data[0]) # 第三组条形的底部是(第一组条形的高度+第二组条形的高度) # 两个列表的元素不能一一对应相加,先用np.array()把列表变为数组,数组可以元素上对应相加 plt.bar(X, data[2], color = 'r', width = 0.25,bottom = np.array(data[0]) + np.array(data[1])) plt.show() ``` ![](https://img.kancloud.cn/af/9b/af9bf43b812ec7504a4b34436049a1c6_592x387.png) ***** # SCATTER POINTS 散点图 ![](https://img.kancloud.cn/a2/77/a2775655ce5fb23209a4c637eacf0929_743x439.png) 散点图用来衡量两个连续变量之间的相关性 ``` import numpy as np import matplotlib.pyplot as plt N = 50 #生成50个[0,1)之间的值 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y) ``` ![](https://img.kancloud.cn/14/6e/146eb995e70ea34251c666428adfc416_540x353.png) ***** ``` N = 50 x = np.random.rand(N) y = np.random.rand(N) #生成的数值序列表示颜色 colors = np.random.randn(N) #生成表示点面积大小的数值序列 area = np.pi * (15 * np.random.rand(N))**2 # 调整大小 # alpha是透明度 plt.scatter(x, y, c=colors, alpha=0.5, s = area) ``` ![](https://img.kancloud.cn/62/1a/621ad67976ae8de8fa019bb4db06d814_544x344.png) ***** # Histogram **解释:直方图是用来衡量连续变量的概率分布的。在构建直方图之前,我们需要先定义好bin(值的范围),也就是说我们需要先把连续值划分成不同等份,然后计算每一份里面数据的数量。** ![](https://img.kancloud.cn/50/77/50777d973f8dcd37fb4e21f3d6afd6d0_637x486.png) ***** ``` a = np.random.rand(100) #bins将数据值划为20份 plt.hist(a,bins= 20) #设置直方的高度在0到15之间 plt.ylim(0,15) ``` ![](https://img.kancloud.cn/9e/e0/9ee03eeacdd747837f18a11806feb2bd_570x384.png) ***** ``` a = np.random.randn(10000) plt.hist(a,bins=50) plt.title("Standard Normal Distribution") ``` ![](https://img.kancloud.cn/43/20/43201cb8e661de213def49f4fce85080_556x409.png) ***** # BOXPLOTS 箱型图 boxlot用于表达连续特征的百分位数分布。统计学上经常被用于检测单变量的异常值,或者用于检查离散特征和连续特征的关系 ![](https://img.kancloud.cn/fd/be/fdbea0be06a5a50c9b44e3fe3583e065_480x608.png) ``` #生成20到100的整数,并且是30行3列的数组 x = np.random.randint(20,100,size = (30,3)) #根据三列数据会绘制出三个箱型图 plt.boxplot(x) #y轴取值是0到120 plt.ylim(0,120) # x轴上标记是1,2,3。标记的标签是A,B,c,如图 plt.xticks([1,2,3],['A','B','C']) #plt.hlines是画一条横线,y值是第一个参数,从xmin画到xmanx plt.hlines(y = np.mean(x,axis = 0)[1] ,xmin =0,xmax=3) ``` ![](https://img.kancloud.cn/04/6c/046c41a528c1b71f38709ecf5208f3af_528x351.png) ***** ![](https://img.kancloud.cn/51/6a/516a33977b578f0497f4fde9888b7eae_488x262.png) ***** # COLORS/TEXTS/annotate ![](https://img.kancloud.cn/43/c4/43c4b58f3ad81819c8bfceb6592dda60_759x223.png) ``` #设置画布背景颜色为darkseagreen fig, ax = plt.subplots(facecolor='darkseagreen') data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] #返回给定值内的均匀间隔值 X = np.arange(4) plt.bar(X, data[0], color = 'darkorange', width = 0.25,label = 'A',bottom= 0) plt.bar(X, data[1], color = 'steelblue', width = 0.25,bottom = data[0],label = 'B') plt.bar(X, data[2], color = 'violet', width = 0.25,bottom = np.array(data[0]) + np.array(data[1]),label = 'C') #设置图像title ax.set_title("Figure 1") #显示条形图标注 plt.legend() ``` ![](https://img.kancloud.cn/f7/32/f7324df5e2afed91cc7412eeb75a55f5_543x389.png) ***** **zip方法** ![](https://img.kancloud.cn/fe/ea/feea484c52ab0dfa5ae885ab93731593_245x192.png) ***** 增加文字 ~~~python plt.text(x, y, s, fontdict=None, withdash=False, **kwargs) ~~~ ``` fig, ax = plt.subplots(facecolor='teal') data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) plt.bar(X+0.00, data[0], color = 'darkorange', width = 0.25,label = 'A') plt.bar(X+0.25, data[1], color = 'steelblue', width = 0.25) plt.bar(X+0.50, data[2], color = 'violet', width = 0.25,label = 'C') ax.set_title("Figure 2") plt.legend() # 添加文字描述 W = [0.00,0.25,0.50] for i in range(3): for a,b in zip(X+W[i],data[i]): plt.text(a,b,"%.0f"% b,ha="center",va= "bottom") plt.xlabel("Group") plt.ylabel("Num") plt.text(0.0,48,"TEXT") ``` ![](https://img.kancloud.cn/72/3b/723b871c3f8d9ccfb74f68824981c85c_554x407.png) ***** 在数据可视化的过程中,图片中的文字经常被用来注释图中的一些特征。使用annotate()方法可以很方便地添加此类注释。在使用annotate时,要考虑两个点的坐标:被注释的地方xy(x, y)和插入文本的地方xytext(x, y) ``` import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 X = np.linspace(0, 2*np.pi,100)# 均匀的划分数据 Y = np.sin(X) Y1 = np.cos(X) plt.plot(X,Y) plt.plot(X,Y1) plt.annotate('Points', #要注释的地方 xy=(1, np.sin(1)), # 文本的地方 xytext=(2, 0.5), fontsize=16, #注释的地方和文本产生联系的符号 arrowprops=dict(arrowstyle="->")) plt.title("这是一副测试图!") ``` ![](https://img.kancloud.cn/63/98/63981e6fc5c253c025235911c6e23c8c_547x353.png) ***** # Subplots ~~~python matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw) ~~~ 使用 **subplot** 绘制多个图形 ~~~python subplot(nrows, ncols, index, **kwargs) ~~~ ``` #在jupyter lab里调整图片大小 %pylab inline pylab.rcParams['figure.figsize'] = (10, 6) # 调整图片大小 np.random.seed(19680801) #直方图图形份数 n_bins = 10 # 数据为1000行*3列 x = np.random.randn(1000, 3) #将画布横的分成两部分,纵轴分为两部分,共分为4部分。放在数组中 fig, axes = plt.subplots(nrows=2, ncols=2,facecolor='white') #将数组一维化,并将四个部分按索引顺序储存在变量中 ax0, ax1, ax2, ax3 = axes.flatten() colors = ['red', 'tan', 'lime'] ax0.hist(x, n_bins, normed=1, histtype='bar', color=colors, label=colors) ax0.legend(prop={'size': 10}) ax0.set_title('bars with legend') ax1.hist(x, n_bins, normed=1, histtype='bar', stacked=True) ax1.set_title('stacked bar') ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False) ax2.set_title('stack step (unfilled)') # Make a multiple-histogram of data-sets with different length. x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]] ax3.hist(x_multi, n_bins, histtype='bar') ax3.set_title('different sample sizes') fig.tight_layout() # Adjust subplot parameters to give specified padding. plt.show() ``` ![](https://img.kancloud.cn/82/07/8207eb85da0a8e4f4e96cd71d0162ce4_1026x601.png) ***** **两部分图共享X轴或Y轴** ``` # ShareX or ShareY N_points = 100000 n_bins = 20 #产生一个标准正态分布 # Generate a normal distribution, center at x=0 and y=5 x = np.random.randn(N_points) y = .4 * x + np.random.randn(100000) + 5 #将画布竖着分为两部分,共享y轴 fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True) # We can set the number of bins with the `bins` kwarg axs[0].hist(x, bins=n_bins) axs[1].hist(y, bins=n_bins) ``` ![](https://img.kancloud.cn/71/b0/71b04b9bb1318314b0b401ebc5b9c966_637x397.png) ***** # PANDAS API 利用pandas API画图 ![](https://img.kancloud.cn/f4/4a/f44acc77ccc045f042edf130b23fe5ad_888x384.png) ![](https://img.kancloud.cn/00/26/00269139b58af0bc2736415ee1f562a4_869x618.png) ***** ![](https://img.kancloud.cn/1c/20/1c2057b778e39593f9d5f08fabdaa31d_976x618.png) ***** ``` #按值分组 grouped = df.groupby("birth_state") #每个分组行的数量 gs = grouped.size() #大于10的组排序 并画条形图 gs[gs >=10].sort_values().plot.bar() ``` ![](https://img.kancloud.cn/c0/b4/c0b4f214d8b116451fc51a33261f033e_782x707.png) ***** ![](https://img.kancloud.cn/99/56/995698d60f95dac3e0f9ed3bba229066_745x596.png) ***** ![](https://img.kancloud.cn/52/4d/524d6968a18b1b6dadbbe3ed48b91862_1145x624.png) ***** ![](https://img.kancloud.cn/f1/6e/f16ed7491d212df55d6646c2ebd43a8c_1038x640.png) ***** # Sseaborn: statistical data visualization ## Visualizing the distribution of a dataset ![](https://img.kancloud.cn/63/7d/637d306869a356d9312d3a2f9334b02f_909x414.png) ***** ![](https://img.kancloud.cn/76/5b/765bf44768cb4160dc9d947df6c7105c_610x317.png) ***** ![](https://img.kancloud.cn/24/58/2458802365c7e90bc9019c300257771b_626x464.png) ***** ![](https://img.kancloud.cn/39/a9/39a9208c2d59b55f178630726024915a_604x423.png) ***** ![](https://img.kancloud.cn/5b/d1/5bd1ff89c5837b07d34290824c8b1951_625x484.png) ***** ![](https://img.kancloud.cn/b9/b3/b9b346daf46828fab9fe4b4964b248db_720x490.png) ***** ### Plotting bivariate distributions 绘制双变量分布 ![](https://img.kancloud.cn/e3/13/e3137324a85cd0372954393f8a2ab520_314x201.png) ***** ![](https://img.kancloud.cn/2a/01/2a01be73c362c2bb77780661dc37ff49_706x655.png) ***** ![](https://img.kancloud.cn/75/c0/75c0b04bd7eb86b3c2ff18c898448eb1_667x623.png) ***** ### Visualizing pairwise relationships in a dataset 可视化数据集中的成对关系 ![](https://img.kancloud.cn/44/1f/441fd2fc2049a79b59246dc92d80f26c_729x714.png) ***** ![](https://img.kancloud.cn/46/0a/460a485d8aa6e60432f47921722eeec1_757x698.png) ![](https://img.kancloud.cn/0f/89/0f8976be94dd4a066ab4854e74d42a55_703x371.png) ***** ![](https://img.kancloud.cn/c9/ea/c9eabd5dfcbc171b916fc999aef37dd7_310x204.png) ***** ![](https://img.kancloud.cn/d0/77/d0776ff2191eaa6ff89b37da273c580b_665x384.png) ***** ## Visualizing linear relationships ![](https://img.kancloud.cn/6b/e8/6be89264f44019eb66cc5ad4b2a5bd4f_457x478.png) ***** ![](https://img.kancloud.cn/b1/c8/b1c830f13811be9d5d1f778c999be90d_713x755.png) ***** ![](https://img.kancloud.cn/b6/4c/b64cc67c081b42868d7fcfe84ac31a48_622x514.png) ***** ![](https://img.kancloud.cn/93/34/93348073fd38b656d0b9b44502672f43_609x505.png) ***** ![](https://img.kancloud.cn/22/ce/22cec9d90fdae30a34656a3c8beb6635_676x549.png) ***** ![](https://img.kancloud.cn/73/b4/73b48274b27b0dfeeb251ea3ef83ff9e_692x560.png) ***** ![](https://img.kancloud.cn/50/ed/50edc0d7b0c677711d67b1b1b87b4a82_648x585.png)