【339】matplotlib based on python3
Ref: python3 的 matplotlib绘图库的使用
Ref: python matplotlib绘图设置坐标轴刻度、文本
Ref: python中matplotlib的颜色及线条控制
Ref: 图文并茂的Python散点图教程
举例:机器学习实战教程(一):K-近邻算法(史诗级干货长文)
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)

【339】matplotlib based on python3的更多相关文章
- 【vps】Centos 7安装python3.8.5
[vps]Centos 7安装python3.8.5 前言 由于服务器的搬迁,从香港搬到了大陆,原来的香港服务器即将到期,所以趁着大陆服务器在备案的时候,将新服务器的配置先配置一下.这篇文章就是分享C ...
- 【python】matplotlib进阶
参考文章:https://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/ 几个重要对象:图像.子图.坐标轴.记号 figure:图像, subplo ...
- 【人工智能】【Python】Matplotlib基础
Maplotlib 本文档由萌狼蓝天写于2022年7月24日 目录 Maplotlib (一)Matplotlib三层结构 (二)画布创建.图像绘制.图像显示 (三)图像画布设置.图像保存 (四)自定 ...
- 【python】matplotlib在windows下安装
昨晚装了好久的这玩意,终于在凌晨成功搞定,然后跑起了一个人人网抓取好友关系的脚本~开心. 以下是我参考的最给力的文档,全部安装一遍,就可以啦~ 但是!在安装前一定要先确认自己的python版本!本人自 ...
- 【转】ubuntu16.04设置python3为默认及一些库的安装
原文:https://www.cnblogs.com/jokie/p/6933546.html Ubuntu默认Python为2.7,所以安装Python包时安装的为py2的包. 利用alternat ...
- 【解决方案】M2Crypto不支持python3
问题现象:python3的环境下,导入M2Crypto模块报错 "ModuleNotFoundError: No module named 'M2Crypto",通过pip ins ...
- 【Ubuntu】ubuntu系统下python3和python2环境自由切换
shell里执行: sudo update-alternatives --install /usr/bin/python python /usr/local/lib/python2.7 100sudo ...
- 【转】matplotlib制图——图例legend
转自:https://www.cnblogs.com/alimin1987/p/8047833.html import matplotlib.pyplot as pltimport numpy as ...
- 【Python】matplotlib 双y轴绘制及合并图例
1.双y轴绘制 关键函数:twinx() 问题在于此时图例会有两个. # -*- coding: utf-8 -*- import numpy as np import matplotlib.pypl ...
随机推荐
- php 目录操作
1.打开文件: opendir("文件名称"); 2.读取文件:readdir("文件名称"); <?php $dirname="phpMyAd ...
- python列表中的pop
pop()将列表指定位置的元素移除,同时可以将移除的元素赋值给某个变量,不填写位置参数则默认删除最后一位. pop()根据键将字典中指定的键值对删除,同时可以将删除的值赋值给变量. a = [1, 2 ...
- HttpClient连接池
HttpClient连接池,发现对于高并发的请求,效率提升很大.虽然知道是因为建立了长连接,导致请求效率提升,但是对于内部的原理还是不太清楚.后来在网上看到了HTTP协议的发展史,里面提到了一个属性C ...
- Mysql 性能优化2 系统参数配置方法 和 文件系统
--------------------------------------------目录------------------------------------------------- • 关于 ...
- Zabbix二次开发_03api列表_中文版
基于ZABBIX 3.0 https://www.zabbix.com/documentation/3.0/manual/api/reference 参考方法 本节提供了的zabbix提供的功能的概述 ...
- Apache2.4.7 + php5 + mysql thinkphp
1. LAMP 的安装sudo apt-get install apache2 2.安装PHP sudo apt-get install libapache2-mod-php5 php5 php5- ...
- Bootstrap:百科
ylbtech-Bootstrap:百科 Bootstrap (Web框架) Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.Java ...
- 接口测试maven管理
接口测试框架选择 界面化工具,针对不会编码的测试人员: 1.Jmeter性能测试工具,不具备完备的接口测试框架功能 2.Robotframerwork 3.PostMan 推荐框架: ResrAssu ...
- Windows7下搭建Eclipse+Python开发环境
机器: Windows7_x86_64 前提: 机器已成功安装Python2.7,并配置好环境变量. 步骤: 一.Eclipse的安装 Eclipse是基于java的一个应用程序,因此需要一个java ...
- Ubuntu安装配置串口通讯工具minicom&&cutecom
原帖地址:https://blog.csdn.net/gatieme/article/details/45310493 2017-04-07更新 发现新的工具gtkterm全名叫serial port ...