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. php实现cookie加密解密

    1.加密解密类 class Mcrypt { /** * 解密 * * @param string $encryptedText 已加密字符串 * @param string $key 密钥 * @r ...

  2. PHP图像处理

    1.创建画布: $img=imagescreatetruecolor(200,200); 创建颜色并填充 $red=imagecolorallocate($img,255,0,0);  //创建颜色 ...

  3. UI设计心得

    旁观型ui.追求一种无所不在,同时低调退隐的,奢华的存在感.内容由用户自己去搜索,浏览,构建,召唤,或是随着信息世界的某种外界趋势自然产生,ui作为始终凌驾于用户之上的高高在上的守护神,随时起到中承, ...

  4. centos6+nginx+php+mysql+memcached+wordpress

    centos6+nginx+php+mysql+memcached+wordpress 搭建步骤(1) LNMP 平台搭建: 请参考:http://www.cnblogs.com/ligao/p/61 ...

  5. RDD之四:Value型Transformation算子

    处理数据类型为Value型的Transformation算子可以根据RDD变换算子的输入分区与输出分区关系分为以下几种类型: 1)输入分区与输出分区一对一型 2)输入分区与输出分区多对一型 3)输入分 ...

  6. java实验二——输出一个指定整数的所有质因数

    import java.util.Scanner; public class 实验二 { /** * @param args */ public static void main(String[] a ...

  7. 杂项-TOOL:NPIO

    ylbtech-杂项-TOOL:NPIO NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作.NPOI是一个开源的Ja ...

  8. 关于clearfix和clear的讨论

    本文摘自百度文库 还是提到了一个关于网页制作很古老的问题,浮动的清除. 虽然看过一些资料介绍说能不用浮动就尽量不要用,但对定位不是很熟的我来说,浮动就不能不用了:既然惹上这个麻烦,就得想个办法进行解决 ...

  9. win2003 server的域用户加入本地管理员组

    很久没有搞windows的域管理,今天碰到一个问题,一直找不到解决方法,一直在网络找资料,直到刚刚才找到方法,方法如下: C:\>net localgroup administrators li ...

  10. sklearn的GridSearchCV例子

    class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_j ...