一、matplotlib绘制折线图

  • matplotlib绘图的实质是折线图,将所有的点用直线连接起来,由于距离比较密,看起来像是个平滑的曲线;
  1. import matplotlib as mpl:加载matplotlib模块;
  2. from matplotlib import pyplot as plt:一般多用matplotlib的子模块pyplot,然后直接调用pyplot的相应函数即可;
  3. 最简单的绘图:
    from matplotlib import pyplot as plt
    import numpy as np # 从[0, 10]区间内,等分取出100个点(包含0和10);
    x = np.linspace(0, 10, 100)
    y = np.sin(x) # 绘制以x为横轴,y为纵轴,绘图,生成matplotlib.lines.Line2D对象
    plt.plot(x, y) # 使用plt的show函数显示图线对象
    plt.show()
  4. 一个图内绘制多条曲线:
    from matplotlib import pyplot as plt
    import numpy as np x = np.linspace(0, 10, 100)
    siny = np.sin(x)
    cosy = np.cos(x) # 绘制多条曲线后再显示所有的线,才可以在同一个图内显示多条线
    plt.plot(x, siny)
    plt.plot(x, cosy)
    plt.show()
  5. 改变线条颜色、线型、坐标轴范围(x轴、y轴的范围):具体颜色种类和线型的种类可查matplotliib文档;“:”:表示"...."点虚线、“-.”:表示“-.-.-.-.-.”横杠 + 点、“--”:两个横杠表示“------”横杠虚线、“-”:一个横杠表示默认实线;
    plt.plot(x, siny)
    
    # 将cosy曲线的颜色调整为红色,线型为虚线
    plt.plot(x, cosy, color = 'red', linestyle = '--') # 分别限定横纵坐标范围:横轴在[5, 8],纵轴在[0, 1]
    plt.xlim(5, 8)
    plt.ylim(0, 1) # 也可以同时限定两个坐标轴的范围,默认两面两个参数为横坐标范围,后面两个参数为纵坐标范围
    plt.axis([5, 8, 0, 1]) plt.show()
  6. 添加坐标轴的label、曲线的图式、图标的title:
    from matplotlib import pyplot as plt
    import numpy as np x = np.linspace(0, 10, 100)
    siny = np.sin(x)
    cosy = np.cos(x) # 添加图式:label
    plt.plot(x, siny, label = 'sin(x)')
    plt.plot(x, cosy, color = 'red', linestyle = '--', label = 'cos(x)') # 添加横、纵左边的名称
    plt.xlabel("x axis")
    plt.ylabel("y value") # 添加图表标题:title
    plt.title('Welcome to the Machine-Learn World') # 显示图式label
    plt.legend() plt.show()

二、matplotlib绘制散点图:Scatter Plot

  • 直接调用plt.scatter()函数即可,和plt.plot()用法一样
  • 对于折现图,横轴表示特征,纵轴表示取值;
  • 对于散点图,通常横、纵两个轴均表示特征,对用于绘制二维特征:将特征点打在图像上,用不同的颜色代替label;
  1. 绘制简单散点图:

    from matplotlib import pyplot as plt
    import numpy as np x = np.linspace(0, 10, 100)
    siny = np.sin(x)
    cosy = np.cos(x) # 添加图式:label
    plt.scatter(x, siny)
    plt.scatter(x, cosy, color = "red")
    plt.show()
  2. 设置散点的透明度:0~1,0表示全透明,1表示完全不透明;
    x = np.random.normal(0, 1, 10000)
    y = np.random.normal(0, 1, 10000) # 透明度设置为0.5
    plt.scatter(x, y, alpha = 0.5)
  3. 散点图有很多样式,可查看matplotlib文档了解;

机器学习:Jupyter Notebook中Matplotlib的使用的更多相关文章

  1. 第三十六篇 入门机器学习——Jupyter Notebook中的魔法命令

        No.1.魔法命令的基本形式是:%命令   No.2.运行脚本文件的命令:%run %run 脚本文件的地址 %run C:\Users\Jie\Desktop\hello.py # 脚本一旦 ...

  2. 解决在jupyter notebook中遇到的ImportError: matplotlib is required for plotting问题

    昨天学习pandas和matplotlib的过程中, 在jupyter notebook遇到ImportError: matplotlib is required for plotting错误, 以下 ...

  3. 机器学习:Jupyter Notebook中numpy的使用

    一.Jupyter Notebook的魔法命令 # 模块/方法 + ?或者help(模块/方法):查看模块/方法的解释文档: 1)%run # 机械学习中主要应用两个魔法命令:%run.%timeit ...

  4. 非线性函数的最小二乘拟合及在Jupyter notebook中输入公式 [原创]

    突然有个想法,能否通过学习一阶RC电路的阶跃响应得到RC电路的结构特征——时间常数τ(即R*C).回答无疑是肯定的,但问题是怎样通过最小二乘法.正规方程,以更多的采样点数来降低信号采集噪声对τ估计值的 ...

  5. jupyter notebook中No module named 'tensorflow'

    当我们在jupyter notebook中运行时可能会遇见没有某个包的情况,如下: ---------------------------------------------------------- ...

  6. 在jupyter notebook中同时安装python2和python3

    之前讨论过在anaconda下安装多个python版本,本期来讨论下,jupyter notebook中怎样同时安装python2.7 和python3.x. 由于我之前使用的jupyter note ...

  7. TensorFlow Jupyter Notebook 和matplotlib安装配置

    Jupyter Notebook 和matplotlib Jupyter Notebook安装 Python 3 : python3 -m pip install --upgrade pip pyth ...

  8. 在jupyter notebook中运行R语言

    要想在jupyter notebook中运行R语言其实非常简单,按顺序安装下面扩展包即可: install.package('repr','IRdisplay','evaluate','crayon' ...

  9. 在jupyter notebook 中同时使用安装不同版本的python内核-从而可以进行切换

    在安装anaconda的时候,默认安装的是python3.6 但是cs231n课程作业是在py2.7环境下运行的.所以需要在jupyter notebook中安装并启用python2.7版本 方法: ...

随机推荐

  1. oschina git服务, 如何生成并部署ssh key

    1.如何生成ssh公钥 你可以按如下命令来生成 sshkey: ssh-keygen -t rsa -C "xxxxx@xxxxx.com" # Generating public ...

  2. python基础-第五篇-5.1冒泡排序

    几个月过去了,小白逐渐对公司的后端服务熟悉了,不过这天小白又接到一封神秘邮件,是景女神发来的:公司急需一批对语言算法有些了解的优秀员工,鉴于你在公司的表现很不错,现在给到你一个培训机会,请速到开发部报 ...

  3. JVM类加载器

    系统中的类加载器 1.BootStrap ClassLoader a.启动ClassLoader b.加载rt.jar 2.Extension ClassLoader a.扩展ClassLoader ...

  4. docker 存储定义成direct-lvm 模式

    配置direct-lvm模式 1.  停止Docker systemctl stop docker 2.  安装依赖包 device-mapper-persistent-data,lvm2, and ...

  5. python-安装 pip

    https://pip.pypa.io/en/stable/installing/ wget https://bootstrap.pypa.io/get-pip.py python get-pip.p ...

  6. LeeCode:两数之和【1】

    LeeCode:两数之和[1] 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2 ...

  7. php添加或升级扩展模块步骤

    php添加或升级扩展模块步骤: 1).下载对应扩展的稳定版源码包2).进入到解压后的源码包执行: sudo /usr/local/php/bin/phpize //对应安装到哪个php版本 sudo ...

  8. 【leetcode刷题笔记】Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. uCGUI 按键窗口切换机制

    前段时间在做一个窗口项目,这个项目菜单项过多,在管理起来比较麻烦.想做一个高效移植又方便的一个切换机制.后来在网上多方查找这方面资料,但是感觉比较少.后来自己整理出了这个结构,希望对后来朋友有所帮助. ...

  10. 【Flask】Sqlalchemy 增删该查操作

    ### sqlalchemy 增删改查操作, 通过session来进行操作. # coding:utf-8 # Author: liangjun. from sqlalchemy import cre ...