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. ZedGraph控件的使用

    http://blog.chinaunix.net/uid-20776117-id-1847015.html 在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状 ...

  2. WCF揭秘学习笔记(5):WF定制活动

    WF(Windows Workflow Foundation,Windows工作流基础)为.NET提供了一种基于模型的.声明方式的过程执行引擎,它改变了传统的通过一行行编写代码来开发服务功能的方式. ...

  3. DEDECMS ShowMsg()样式修改 提示信息的修改以及美化

    织梦DedeCMS系统,处处都在用到提示信息,但是这个提示框,前台后台一层不变,太死板了,可能有很多人都有过去修改它的想法,只是苦于不知道去哪里 改.今天我就来说说这个吧,DedeCMS的所有提示信息 ...

  4. pycharm中配置pep8

    Pycharm本身是有pep8风格检测的,当你敲得代码中不符合规范时,会有下划波浪线提示.如何让代码修改为符合规范,去掉这些难看的波浪线呢? 1.安装autopep8  pip install aut ...

  5. 软件-分布式:Kylin (apache开源分布式分析引擎软件)

    ylbtech-软件-分布式:Kylin (apache开源分布式分析引擎软件) Apache Kylin™是一个开源的分布式分析引擎,提供Hadoop之上的SQL查询接口及多维分析(OLAP)能力以 ...

  6. 搭建OpenStack先电云平台

    实际操作示意图 在VMware里面创建两台centos7的虚拟机作为搭建云平台的两节点配置如下: 1.第一台虚拟机   作为控制节点 2CPU 3G以上内存 硬盘50G 网络适配器一个nat 一个仅主 ...

  7. Git 查询某次历史提交的修改内容

    在工作时,有时候想查看某次的提交修改了哪些的内容. 我们首先可以git log显示历史的提交列表: 之后我们用git show <commit-hashId> 便可以显示某次提交的修改内容 ...

  8. 深入理解yield(三):yield与基于Tornado的异步回调

    转自:http://beginman.cn/python/2015/04/06/yield-via-Tornado/ 作者:BeginMan 版权声明:本文版权归作者所有,欢迎转载,但未经作者同意必须 ...

  9. 第8章 信号(1)_Linux信号处理机制

    1. 信号的基本概念 1.1 基本概念 (1)信号(signal)机制是linux系统中最为古老的进程之间的通信机制,解决进程在正常运行过程中被中断的问题,导致进程的处理流程会发生变化. (2)信号本 ...

  10. Keras实现简单BP神经网络

    BP 神经网络的简单实现 from keras.models import Sequential #导入模型 from keras.layers.core import Dense #导入常用层 tr ...