zoukankan      html  css  js  c++  java
  • Python基础 — Matplotlib

    Matplotlib -- 简介

            matplotlib是Python优秀的数据可视化第三方库;
            matplotlib库的效果可参考官网:http://matplotlib.org/gallery.html ;
            matplotlib的使用 由各种可视化类构成,内部结构复杂,受matlab库启发,matplotlab.pyplot是绘制种类可视化图形的命令子库,相当于快捷方式

            matplotlib可以绘制的图


    Matplotlib -- 绘图函数

    1、plot函数

    plt.plot(x,y,format_string,**kwargs)

         参数:
                 x:X轴数据,列表或数组,可选;
                 y:Y轴数据,列表或数组;
                 format_string:控制曲线的格式字符串,由颜色字符、风格字符和标识字符组成,可选;
                 **kwargs:第二组或更多的(x,y,format_string,**kwargs);

    2、subplot()函数

    plt.subplot(numRows, numCols, plotnum)

         参数:
                 numRows : int ,指创建的sublots的行数,默认为1 ;
                 numCols : int ,指创建的sublots的列数,默认为1;
                 plotnum:指定操作的区域;


    3、Matplotlib.pyplot常用的方法

    方法描述
    plt.savefig('filename',dpi=n)将绘制的图画保存成png格式,并设置分辨率,默认为100;
    plt.axis([0,9,1,8])x轴起始于0,终止于9 ,y轴起始于1,终止于8
    plt.subplot(Rows, Cols, plotnum)分成Rows行Clos列,共R*C个绘图区域,在第plotnum个区域绘图。排序为行优先。
    plt.xlabel()对x轴增加文本标签
    plt.ylabel()对y轴增加文本标签
    plt.title()对图形整体增加文本标签
    plt.text()在任意位置增加文本
      

    Matplotlib -- 画图种类

    1、折线图(grid)

    # 绘制折线图
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.arange(9)
    y = np.sin(x)
    z = np.cos(x)
    # marker数据点样式,linewidth线宽,linestyle线型样式,color颜色
    plt.plot(x, y, marker="*", linewidth=3, linestyle="--", color="orange")
    plt.plot(x, z)
    plt.title("matplotlib")
    plt.xlabel("height")
    plt.ylabel("width")
    # 设置图例
    plt.legend(["Y","Z"], loc="upper right")
    plt.grid(True)
    plt.show()

    2、条形图(bar)

    #绘制条形图
    x = np.arange(10)
    y = np.random.randint(0,30,10)
    plt.bar(x, y)
    plt.show()

    3、饼图(pie)

    #绘制饼图
    plt.figure(figsize=(10,10));
    x = [4, 9, 21, 55, 30, 18] 
    labels = ['Swiss', 'Austria', 'Spain', 'Italy', 'France', 'Benelux'] 
    explode = [0.2, 0.1, 0, 0, 0.1, 0] 
    plt.pie(x, labels=labels, explode=explode, autopct='%1.1f%%'); 
    plt.show()

    4、散点图(scatter)

    #绘制散点图
    x = np.random.rand(10)
    y = np.random.rand(10)
    plt.scatter(x,y)
    plt.show()

    5、直方图(hist)

    #绘制直方图
    
    mean, sigma = 0, 1
    x = mean + sigma * np.random.randn(10000)
    plt.hist(x,50)
    plt.show()

    6、坐标图(plot)

    #绘制坐标图
    import matplotlib.pyplot as plt
    import numpy as np
    
    #创建数据
    x = np.linspace(-5, 5, 100)
    y1 = np.sin(x)
    y2 = np.cos(x)
    
    #创建figure窗口
    plt.figure(num=3, figsize=(8, 5))
    #画曲线1
    plt.plot(x, y1)
    #画曲线2
    plt.plot(x, y2, color='red', linewidth=5.0, linestyle='--')
    #设置坐标轴范围
    plt.xlim((-5, 5))
    plt.ylim((-2, 2))
    #设置坐标轴名称
    plt.xlabel('xxxxxxxxxxx')
    plt.ylabel('yyyyyyyyyyy')
    #设置坐标轴刻度
    my_x_ticks = np.arange(-5, 5, 0.5)
    my_y_ticks = np.arange(-2, 2, 0.2)
    plt.xticks(my_x_ticks)
    plt.yticks(my_y_ticks)
    
    #显示出所有设置
    plt.show()

    7、子图(subplot)

    # figsize绘图对象的宽度和高度,单位为英寸,dpi绘图对象的分辨率,即每英寸多少个像素,缺省值为80
    plt.figure(figsize=(8,6),dpi=100)
    
    # subplot(numRows, numCols, plotNum)
    # 一个Figure对象可以包含多个子图Axes,subplot将整个绘图区域等分为numRows行*numCols列个子区域,按照从左到右,从上到下的顺序对每个子区域进行编号
    # subplot在plotNum指定的区域中创建一个子图Axes
    A = plt.subplot(2,2,1)
    plt.plot([0,1],[0,1], color="red")
    
    plt.subplot(2,2,2)
    plt.title("B")
    plt.plot([0,1],[0,1], color="green")
    
    plt.subplot(2,1,2)
    plt.title("C")
    plt.plot(np.arange(10), np.random.rand(10), color="orange")
    
    # 选择子图A
    plt.sca(A)
    plt.title("A")
    
    plt.show()


    Matplotlib -- 作图线的属性

    颜色(color 简写为 c):

    颜色字符
    红色'r' (red)
    蓝色'b' (blue)
    绿色'g' (green)
    黄色'y' (yellow)
    黑色'k' (black)
    白色'w' (white)
    紫色'm' (magenta)
    青色'c' (cyan)
    灰色[0,1]内任意浮点数
    RGB表示法'#2F4F4F' 或 (0.18, 0.31, 0.31)

    线型(linestyle 简写为 ls):

    线型字符
    '.'                   
    点线 ':'
    点画线'-.'
    实线'-'
    虚线 '--'

    图形标记(标记marker):

    形状字符
    圆形'o'
    加号 '+'               
    星形'*'
    乘号'x'
    方形's'
    菱形'D'



  • 相关阅读:
    .NET自带控件 ································
    FCKeditor 2.2 + Asp.Net 设置
    . Net 2.0 实现动态切换首页图片···················
    按月统计的问题,即数据中没有该月的数据,该月的数据显示为0
    Active Directory如何用C#进行增加、删除、修改、查询用户与组织单位!
    GridView 的排序 !!
    利用GridView显示主细表并一次编辑明细表所有数据的例子 !!【转自孟子e章】
    [翻译]使用ASP.NET2.0的ReportViewer查看RDLC报表 !!!
    opengl 教程(20) 点光源
    每个程序员都应该了解的内存知识(1)
  • 原文地址:https://www.cnblogs.com/lsqin/p/9342930.html
Copyright © 2011-2022 走看看