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的更多相关文章

  1. 【vps】Centos 7安装python3.8.5

    [vps]Centos 7安装python3.8.5 前言 由于服务器的搬迁,从香港搬到了大陆,原来的香港服务器即将到期,所以趁着大陆服务器在备案的时候,将新服务器的配置先配置一下.这篇文章就是分享C ...

  2. 【python】matplotlib进阶

    参考文章:https://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/ 几个重要对象:图像.子图.坐标轴.记号 figure:图像, subplo ...

  3. 【人工智能】【Python】Matplotlib基础

    Maplotlib 本文档由萌狼蓝天写于2022年7月24日 目录 Maplotlib (一)Matplotlib三层结构 (二)画布创建.图像绘制.图像显示 (三)图像画布设置.图像保存 (四)自定 ...

  4. 【python】matplotlib在windows下安装

    昨晚装了好久的这玩意,终于在凌晨成功搞定,然后跑起了一个人人网抓取好友关系的脚本~开心. 以下是我参考的最给力的文档,全部安装一遍,就可以啦~ 但是!在安装前一定要先确认自己的python版本!本人自 ...

  5. 【转】ubuntu16.04设置python3为默认及一些库的安装

    原文:https://www.cnblogs.com/jokie/p/6933546.html Ubuntu默认Python为2.7,所以安装Python包时安装的为py2的包. 利用alternat ...

  6. 【解决方案】M2Crypto不支持python3

    问题现象:python3的环境下,导入M2Crypto模块报错 "ModuleNotFoundError: No module named 'M2Crypto",通过pip ins ...

  7. 【Ubuntu】ubuntu系统下python3和python2环境自由切换

    shell里执行: sudo update-alternatives --install /usr/bin/python python /usr/local/lib/python2.7 100sudo ...

  8. 【转】matplotlib制图——图例legend

    转自:https://www.cnblogs.com/alimin1987/p/8047833.html import matplotlib.pyplot as pltimport numpy as ...

  9. 【Python】matplotlib 双y轴绘制及合并图例

    1.双y轴绘制 关键函数:twinx() 问题在于此时图例会有两个. # -*- coding: utf-8 -*- import numpy as np import matplotlib.pypl ...

随机推荐

  1. linux 知识点

    关于登录Linux时,/etc/profile.~/.bash_profile等几个文件的执行过程. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时,首先启动 /etc/profile ...

  2. winform 子窗体刷新父窗体的数据

    建一个接口 比如 iMainForm接口内声明个方法 比如 Refresh()主窗体 实现这个接口 主窗体打开子窗体时这样写 子窗体.Owner = this;子窗体.ShowDialog(); -- ...

  3. dede模块管理一片空白或没有列表内容的解决办法

    为什么dede后台模块管理,打开之后一片空白,又或者没有列表,插件与其他模块的使用也是正常的. 这主要是因为我们在安装模块,然后又卸载模块,卸载的时候选择了删除安装文件,就会出这个问题. 这里面分为两 ...

  4. 如何判断事务是否完成,SqlTransaction

    SqlConnection sconn = null; SqlCommand scmd = null; SqlTransaction strans = null; try { string sqlIn ...

  5. sql表设计

    数据库实际上是系统逻辑在磁盘上的固化,是信息河流的蓄水池. 数据库的表应有如下类型 1)类表.配置表.作为业务逻辑基本的名字,状态的定义,作为构建逻辑世界的最基础框架,解释框架的框架. 特点,数据不会 ...

  6. bzoj4153 [Ipsc2015]Familiar Couples

    Description 有n对夫妇,一开始夫妇之间互不认识,若两男或两女成为朋友,称他们为"熟人","熟人"关系具有传递性,即若a熟b且b熟c则a熟c.若两组夫 ...

  7. 发邮件 文字+ 附件的方法(QQ or 网易 邮箱)

    #coding:utf-8import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIME ...

  8. java web程序 String的valueOf方法总集

    在代码中用到类型转换的时候,是一个字符,然后当用户在网页中输入的是字符串, 字符转换成字符串的方法是: String.valueOf(char c);就好了 这样在写验证码的时候,网页端的就是字符串形 ...

  9. 退出循环break,在while、for、do...while、循环中使用break语句退出当前循环,直接执行后面的代码。

    在while.for.do...while循环中使用break语句退出当前循环,直接执行后面的代码. 格式如下: for(初始条件;判断条件;循环后条件值更新) { if(特殊情况) {break;} ...

  10. [C#]泛型约束在窗体定义上的使用

    查相关资料查的一团乱,自己瞎写了几次以后误打误撞成功了: namespace Test1 { public partial class Form1<T> : Form { // ... F ...