【339】matplotlib based on python3
Ref: python3 的 matplotlib绘图库的使用
Ref: python matplotlib绘图设置坐标轴刻度、文本
Ref: python中matplotlib的颜色及线条控制
Ref: 图文并茂的Python散点图教程
举例:机器学习实战教程(一):K-近邻算法(史诗级干货长文)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | from matplotlib.font_manager import FontProperties import matplotlib.lines as mlines import matplotlib.pyplot as plt import numpy as np def file2matrix(fileName): fr = open (fileName) arrayOfLines = fr.readlines() numberOfLines = len (arrayOfLines) returnMat = np.zeros((numberOfLines, 3 )) classLabelVector = [] index = 0 for line in arrayOfLines: line = line.strip() listFromLine = line.split( '\t' ) # 不包括3,所以就是前三个 returnMat[index, :] = listFromLine[ 0 : 3 ] if listFromLine[ - 1 ] = = 'didntLike' : classLabelVector.append( 1 ) elif listFromLine[ - 1 ] = = 'smallDoses' : classLabelVector.append( 2 ) elif listFromLine[ - 1 ] = = 'largeDoses' : classLabelVector.append( 3 ) index + = 1 return returnMat, classLabelVector def showData(datingDatMat, datingLabels): fig, axs = plt.subplots(nrows = 2 , ncols = 2 , sharex = False , sharey = False , figsize = ( 13 , 8 )) numberOfLabels = len (datingLabels) LabelsColors = [] for i in datingLabels: if i = = 1 : LabelsColors.append( 'green' ) if i = = 2 : LabelsColors.append( 'blue' ) if i = = 3 : LabelsColors.append( 'red' ) # fig 1 # 颜色是一个 list,对应每一组数据有一个颜色对应 axs[ 0 ][ 0 ].scatter(x = datingDatMat[:, 0 ], y = datingDatMat[:, 1 ], color = LabelsColors, s = 15 , alpha = 0.5 ) axs0_title_text = axs[ 0 ][ 0 ].set_title( 'Fight Hours & Video Game Percentage' ) axs0_xlabel_text = axs[ 0 ][ 0 ].set_xlabel( 'Flight Hours' ) axs0_ylabel_text = axs[ 0 ][ 0 ].set_ylabel( 'Video Game Percentage' ) plt.setp(axs0_title_text, size = 14 , color = 'red' ) plt.setp(axs0_xlabel_text, size = 10 , color = 'brown' ) plt.setp(axs0_ylabel_text, size = 10 , color = 'brown' ) # fig 2 axs[ 0 ][ 1 ].scatter(x = datingDatMat[:, 0 ], y = datingDatMat[:, 2 ], color = LabelsColors, s = 15 , alpha = 0.5 ) axs1_title_text = axs[ 0 ][ 1 ].set_title( 'Fight Hours & Ice Cream Weight' ) axs1_xlabel_text = axs[ 0 ][ 1 ].set_xlabel( 'Flight Hours' ) axs1_ylabel_text = axs[ 0 ][ 1 ].set_ylabel( 'Ice Cream Weight' ) plt.setp(axs1_title_text, size = 14 , color = 'red' ) plt.setp(axs1_xlabel_text, size = 10 , color = 'brown' ) plt.setp(axs1_ylabel_text, size = 10 , color = 'brown' ) # fig 3 axs[ 1 ][ 0 ].scatter(x = datingDatMat[:, 1 ], y = datingDatMat[:, 2 ], color = LabelsColors, s = 15 , alpha = 0.5 ) axs2_title_text = axs[ 1 ][ 0 ].set_title( 'Video Game Percentage & Ice Cream Weight' ) axs2_xlabel_text = axs[ 1 ][ 0 ].set_xlabel( 'Video Game Percentage' ) axs2_ylabel_text = axs[ 1 ][ 0 ].set_ylabel( 'Ice Cream Weight' ) plt.setp(axs2_title_text, size = 14 , color = 'red' ) plt.setp(axs2_xlabel_text, size = 10 , color = 'brown' ) plt.setp(axs2_ylabel_text, size = 10 , color = 'brown' ) # legend didntlike = mlines.Line2D([], [], color = 'green' , marker = '.' , markersize = 6 , label = 'didntLike' ) smallDoses = mlines.Line2D([], [], color = 'blue' , marker = '.' , markersize = 6 , label = 'smallDoses' ) largeDoses = mlines.Line2D([], [], color = 'red' , marker = '.' , markersize = 6 , label = 'largeDoses' ) # Add legend axs[ 0 ][ 0 ].legend(handles = [didntlike, smallDoses, largeDoses]) axs[ 0 ][ 1 ].legend(handles = [didntlike, smallDoses, largeDoses]) axs[ 1 ][ 0 ].legend(handles = [didntlike, smallDoses, largeDoses]) plt.tight_layout() plt.show() if __name__ = = '__main__' : fileName = 'datingTestSet.txt' datingDataMat, datingLabels = file2matrix(fileName) showData(datingDataMat, datingLabels) |
分类:
Python Study
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2014-11-13 【153】电脑耗材购买&报销
2011-11-13 【C015】Python数据类型 - 序列
2011-11-13 【C014】Python数据类型 - 数值类型
2011-11-13 【C013】ArcPy - 入门学习