【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 ...
随机推荐
- bzoj1799(洛谷4127)同类分布(月之谜)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1799 https://www.luogu.org/problemnew/show/P4127 ...
- 三元表达式return if 简化 if 判断语句
- 设计模式初学者笔记:Abstract Factory模式
首先啰嗦下创建迷宫所用的Room类.这个类并不直接保存Room四周的构造,而是通过MapSite* _sides[4]这个私有数组成员指向Room四周的构造.那么什么时候将四周构造直接放在Room中, ...
- csrf xss sql注入
1.输入框 sql注入 测试直接在输入框输入1' ,看sql会不会拼接出错 xss攻击 csrf攻击 测试直接在输入框输入 <script>alert(123)</script> ...
- [UE4] 虚幻4学习---UE4中的字符串转换
String Conversions: FString To FName FString To Int32 Float To FString FArrayReaderPtr To FString TA ...
- PHP中的urlencode,rawurlencode和JS中的encodeURI,encodeURIComponent
PHP中的urlencode,rawurlencode和JS中的encodeURI,encodeURIComponent [PHP中的urlencode和rawurlencode] urlencode ...
- maven package 命令报:-source1.3 中不支持注释错误
在使用maven 打包或者编译时报:-source1.3 中不支持注释错误解决方案如下: <build> <plugins> <plugin> < ...
- How to use POST method in Tornado?
http://stackoverflow.com/questions/10367981/how-to-use-post-method-in-tornado
- 基于Vue的Ui框架
基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 Element Ui 基于vue pc端的UI框架 http://element.eleme.io/ MintUi 基于vue 移动 ...
- 常用Java程序片段
1.改变数组的大小 package com.js.ai.modules.jsa.test; public class Testxf { private static Object resizeArra ...