• [转]用Matplotlib绘制 折线图 散点图 柱状图 圆饼图


    Matplotlib是一个Python工具箱,用于科学计算的数据可视化。借助它,Python可以绘制如Matlab和Octave多种多样的数据图形。

    安装

    Matplotlib并不是Python的默认组件,需要额外安装。

    官方下载地址 http://matplotlib.org/downloads.html

    必须下载与自己的Python版本,操作系统类型对应的安装包。如Windows 64位+Python3.3,应该下载matplotlib-1.3.1.win-amd64-py3.3.exe

    第一个程序

    让我们运行第一个Matplotlib程序:

    import matplotlib.pyplot as plt
    plt.plot([1, 2, 3])
    plt.ylabel('some numbers')
    plt.show()

    如果Matplotlib已经正确安装,您将会看到如下结果:

    Matplotlib-first-example.png

    这个弹出窗口就是Matplotlib的绘图输出窗口,底部从左到右有七个按钮:

      1. 重置视图,显示最初的视图
      2. 上一个视图
      3. 下一个视图
      4. 移动工具,用于拖动绘图的显示区域
      5. 缩放工具,可以拖拽一个矩形缩放到窗口大小
      6. 配置工具,调整绘图选项
      7. 保存图片

    接下来我们一起分析这个短小的程序代码。

    第1行:

    import matplotlib.pyplot as plt

    调用matplotlib的pyplot绘图工具,之后我们就可以使用plt代替matplotlib.pyplot使用了。

    第2行:

    plt.plot([1, 2, 3])

    绘制一个折线图。plot()方法用于绘制折线图,pyplot还有绘制柱状图、饼图的方法。[1,2,3]是Y坐标,而缺省的X坐标为[0,1,2]。两个一维数组X和Y构成了折线关键点的坐标(0,1),(1,2),(2,3)。您可以尝试修改参数如:

    plt.plot([0, 1, 2, 3], [0.1, 0.2, 0.15, 0.3]) # 第一个数组是X坐标,第二个数组是Y坐标

    第3行:

    plt.ylabel('some numbers')

    为Y坐标轴添加标签,通常用以标注此变量的名称和单位。当然X轴也是可以添加标签的:

    plt.xlabel('Time (s)')
    plt.ylabel('Speed (m/s)')

    第4行:

    plt.show()

    显示绘图窗口。在绘图完成之后,我们要调用这个方法才能打开绘图输出窗口。

    另一种图形输出方式是保存图片,缺点是不能使用缩放、移动等交互浏览功能:

    plt.save('output.png')

    基本绘图

    折线图和散点图-plot()

    plot()函数可以绘制折线图和折线图,取决于您使用的参数。

    数据和坐标

    一个最简单的例子:

    import matplotlib.pyplot as plt
     
    x = [0, 1, 2, 3, 4, 5]
    y = [0.1, 0.2, 0.2, 0.3, 0.2, 0.1]
     
    plt.plot(x, y)
     
    plt.show()

    绘制折线图/散点图需要一系列关键点。x是关键点的x轴坐标,y是关键点的y轴坐标。

    xy
    0 0.1
    1 0.2
    2 0.2
    3 0.3
    4 0.2
    5 0.1

    未附加额外参数时,将绘制最基本的折线图:

    Matplotlib-pyplot-plot-1.png

    x轴坐标是可以缺省的:

    plot(y)

    y是一个一维数组,是折线图关键点的y轴坐标。而x轴坐标没有给出,会默认以[0,1,2,...]的常数列作为x轴坐标。

    样式

    散点图和折线图只是线条样式的差异,我们可以通过简单的参数设置线条的样式和颜色。样式和颜色参数都是一个或多个字符构成的字符串。

      1. 既可以单独设定颜色或样式,如'g'代表绿色,'-'代表实线
      2. 也可以同时设定样式和颜色,比如'--g'代表绿色虚线,其中'--'代表虚线,'g'代表绿色
      3. 样式字符串中的参数字符是无顺序的,'g--'和'--g'的意义是相同的
      4. 也可以同时设定折线和散点,如'-or'代表红色实线+圆圈

    例子:

    import matplotlib.pyplot as plt
     
    x = [0, 1, 2, 3, 4, 5]
    y = [0.1, 0.2, 0.2, 0.3, 0.2, 0.1]
     
    plt.plot(x, y, '-or')
     
    plt.show()
    Matplotlib-pyplot-plot-3.png

    折线样式:

    参数样式
    '-' 实线
    '--' 虚线
    '-.' 线-点
    ':' 点虚线

    散点样式:

    参数样式
    '.' 实心点
    'o' 圆圈
    ',' 一个像素点
    'x' 叉号
    '+' 十字
    '*' 星号
    '^' 'v' '<' '>' 三角形(上下左右)
    '1' '2' '3' '4' 三叉号(上下左右)

    颜色:

    参数颜色
    'b'
    'g' 绿
    'r'
    'c'
    'm' 品红
    'y'
    'k'
    'w'

    更多样式参见 http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot

    您也可以不必使用样式字符串,而直接使用参数名,这种方式更加灵活:

    plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12)

    多条线

    我们可以在一个坐标中绘制多条折线或散点,plot(第一条线的参数,第二条线的参数...):

    import matplotlib.pyplot as plt
     
    x = [0, 1, 2, 3, 4, 5]
    y = [0.1, 0.2, 0.2, 0.3, 0.2, 0.1]
    y2 = [0.2, 0.2, 0.3, 0.2, 0.3, 0]
     
    plt.plot(x, y, 'b', x, y2, 'g')
     
    plt.show()
    Matplotlib-pyplot-plot-2.png

    柱状图-bar()

    bar()函数可以绘制各种样式的柱状图,barh()则可绘制水平方向的柱状图。两个方法除了绘图方向外,其他属性和用法的是相同的。

    数据和坐标

    bar()至少需要两个数组left和height:left是每个柱向左到坐标原点的距离;height是每个柱的高度。

    import matplotlib.pyplot as plt
     
    left = [0, 1, 2, 3, 4, 5]
    height = [0.1, 0.2, 0.2, 0.3, 0.2, 0.1]
     
    plt.bar(left, height)
     
    plt.show()
    Matplotlib-pyplot-bar-1.png

    样式

    bar()函数的参数可以控制柱状图的多种样式,最常用的有:

      • width,第3个参数,柱的宽度。可以是一个常数或数组。数组将对每条柱设定不同的值。
      • bottom,第4个参数,柱底部的y轴坐标。可以是一个常数或数组。数组将对每条柱设定不同的值。
      • color,关键字参数,柱的填充颜色。可以是一个常数或数组。数组将对每条柱设定不同的值。
      • edgecolor,关键字参数,柱的边框颜色。
      • linewidth,关键字参数,边框宽度。
      • xerr,yerr,关键字参数,x和y轴误差线。
      • ecolor,关键字参数,误差线颜色。
    提示 以上参数都可以是一个常数或数组。常数为所有柱设定相同的值,数组将对每条柱设定不同的值。
      • align,关键字参数,设定柱的对齐方式。'edge'将x坐标设为左边界,'center'将x坐标设为中轴线。

    下面的柱状图要表达一件商品12个月的销量:

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    sales = [112, 105, 111, 109, 103, 110, 113, 112, 108, 106, 111, 114]
     
    plt.bar(month, sales, 0.5, color='y', edgecolor='g', linewidth=3, align='center')
     
    plt.show()

    我们设置柱宽度0.5,填充颜色'y'(黄色),边框颜色'g'(绿色),线框宽度3,对齐方式为'center'

    Matplotlib-pyplot-bar-2.png

    为了让销量变化看起来更明显,我们现在只绘出100以上的部分:

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    sales = [12, 5, 11, 9, 3, 10, 13, 12, 8, 6, 11, 14]
     
    plt.bar(month, sales, 0.5, 100, color='y', edgecolor='g', linewidth=3, align='center')
     
    plt.show()

    我们添加了新的参数,设置柱状图的底部为100,而sales数据则减去100。

    Matplotlib-pyplot-bar-3.png

    现在我们想将四个季度用颜色区分出来:

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    sales = [12, 5, 11, 9, 3, 10, 13, 12, 8, 6, 11, 14]
    colors = ['g', 'g', 'g', 'm', 'm', 'm', 'y', 'y', 'y', 'c', 'c', 'c']
     
    plt.bar(month, sales, 0.5, 100, color=colors, linewidth=0, align='center')
     
    plt.show()

    我们用一个数组来存储每个柱的颜色,其长度必须与month和sales相同。

    Matplotlib-pyplot-bar-4.png

    多段柱图和水平柱图

    绘制多段柱图的原理是:先后绘制多张柱图,依次重叠在上方,如果后面绘制的柱图比前者的柱图短,就可以显示出前者长出的部分。

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    bar1 = plt.bar(month, profit, 0.5, color='y', linewidth=0, align='center')
    bar2 = plt.bar(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    plt.legend( (bar1[0], bar2[0]), ('Profits', 'Costs') )
     
    plt.show()

    我们还是用了legend()方法标识两段柱图的图例。legend()的更多用法见#绘图样式一节。

    Matplotlib-pyplot-bar-5.png

    最后,让我们来绘制一个水平柱图,只需将bar()替换成barh():

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    bar1 = plt.barh(month, profit, 0.5, color='y', linewidth=0, align='center')
    bar2 = plt.barh(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    plt.legend( (bar1[0], bar2[0]), ('Profits', 'Costs') )
     
    plt.show()
    Matplotlib-pyplot-barh-1.png

    圆饼图-pie()

    pie()是绘制饼图的函数。

    数据

    pie()最基本的输入数据是一个数组。它可以根据数组元素的比例绘制扇形,因此不必要事先计算好百分比。

    import matplotlib.pyplot as plt
     
    rate = [1, 7, 3, 9]
     
    plt.pie(rate)
     
    plt.show()
    Matplotlib-pyplot-pie-1.png

    样式

    有几个最常用的参数可以控制绘图样式:

      • colors - 数组,扇形颜色
      • explode - 数组,扇形偏离圆心的距离
      • labels - 数组,扇形的标签

    首先,让我们为饼图设定颜色,并让第三个扇形抽离出来:

    import matplotlib.pyplot as plt
     
    rate = [1, 7, 3, 9]
    explode = [0, 0, 0.1, 0]
    colors = ['c', 'm', 'y', 'g']
     
    plt.pie(rate, explode=explode, colors=colors)
     
    plt.show()
    Matplotlib-pyplot-pie-2.png

    然后,要为每个扇形添加标签:

    import matplotlib.pyplot as plt
     
    rate = [1, 7, 3, 9]
    explode = [0, 0, 0.1, 0]
    colors = ['c', 'm', 'y', 'g']
    labels = ['Apple', 'Pear', 'Peach', 'Orange']
     
    plt.pie(rate, explode=explode, colors=colors, labels=labels)
     
    plt.show()
    Matplotlib-pyplot-pie-3.png

    百分比

    如何显示百分比数值呢?我们需要使用autopct参数:

      • None,不显示百分比
      • 格式字符串,如'%d percent',显示“40 percent”形式的百分比
      • 方法,调用方法输出百分比
    import matplotlib.pyplot as plt
     
    rate = [1, 7, 3, 9]
    explode = [0, 0, 0.1, 0]
    colors = ['c', 'm', 'y', 'g']
    labels = ['Apple', 'Pear', 'Peach', 'Orange']
     
    plt.pie(rate, explode=explode, colors=colors, labels=labels, autopct='%d%%')
     
    plt.show()

    autopct='%d%%'表示我们将百分比以整数(%d)形式输出,后缀是百分号'%'。在格式化字符串中,百分号要用'%%'转义字符表示。

    Matplotlib-pyplot-pie-4.png

    绘图样式

    这一节介绍如何为图表添加标题,刻度,标签,图例,注释等元素,让图表更加清晰易懂。

    标题-title()

    title()方法用于绘制图表标题,一般位于顶部。最简单的方法是使用title('title'),绘制默认格式的标题文本。

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    plt.barh(month, profit, 0.5, color='y', linewidth=0, align='center')
    plt.barh(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    plt.title('Sales Statistics')
     
    plt.show()

    若只想设置水平对齐,可以直接使用loc参数:

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    plt.barh(month, profit, 0.5, color='y', linewidth=0, align='center')
    plt.barh(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    plt.title('Sales Statistics', loc='right')
     
    plt.show()
    Matplotlib-pyplot-title-1.png

    您也可以额外使用fontdict参数定义格式,fontdict是一个字典数据(dict),有以下字段

      • 'fontsize': 字体大小
      • 'verticalalignment': 垂直对齐
      • 'horizontalalignment': 水平对齐
    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    plt.barh(month, profit, 0.5, color='y', linewidth=0, align='center')
    plt.barh(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    font = {'fontsize': 22, 'verticalalignment': 'bottom', 'horizontalalignment': 'center'}
    plt.title('Sales Statistics', fontdict=font)
     
    plt.show()
    Matplotlib-pyplot-title-2.png

    刻度-xticks()&yticks()

    xticks()和yticks()会修改默认的x轴y轴刻度。

    继续上一个例子,现在我们将要把y轴的刻度从1,2,3...换成月份Jan,Feb,Mar...

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    plt.barh(month, profit, 0.5, color='y', linewidth=0, align='center')
    plt.barh(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    plt.title('Sales Statistics')
    ticks = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    plt.yticks(month, ticks)
     
    plt.show()

    yticks()有两个参数,第一个数组是每个刻度的位置,第二个数组是各个刻度。两个数组等长,设定某刻度的值应该定位于何处。

    Matplotlib-pyplot-yticks.png

    标签-xlabel()&ylabel()

    接下来,添加x轴和y轴的标签:

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    plt.barh(month, profit, 0.5, color='y', linewidth=0, align='center')
    plt.barh(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    plt.title('Sales Statistics')
    ticks = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    plt.yticks(month, ticks)
    plt.xlabel('Money (10000RMB)')
    plt.ylabel('Month')
     
    plt.show()
    Matplotlib-pyplot-label.png

    图例-legend()

    因为有多段柱图,黄色和绿色,我们需要图例区分它们的含义:

    import matplotlib.pyplot as plt
     
    month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    cost = [8, 7, 9, 9, 3, 10, 10, 12, 8, 6, 11, 10]
    profit = [12, 11, 11, 13, 5, 11, 13, 15, 10, 9, 12, 13]
     
    bar1 = plt.barh(month, profit, 0.5, color='y', linewidth=0, align='center')
    bar2 = plt.barh(month, cost, 0.5, color='g', linewidth=0, align='center')
     
    plt.title('Sales Statistics')
    ticks = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    plt.yticks(month, ticks)
    plt.xlabel('Money (10000RMB)')
    plt.ylabel('Month')
    plt.legend( (bar1[0], bar2[0]), ('Profits', 'Costs') )
     
    plt.show()

    legend()有两个数组参数:第一个是样式对象,比如这里选了bar1的第1个柱,bar2的第1个柱(如果是折线图则是某一点的样式);第二个是描述字符,对应描述每一个样式。

    Matplotlib-pyplot-legend.png

    子图-subplot()

    使用subplot()替代plot,可以在同一画布里绘制多个图表,它们的数量,大小都是可调的。

    绘制图片

    三维绘图

    参考资源

      1. 官方网站
      2. Python图表绘制:matplotlib绘图库入门 博客
      3. 使用 python Matplotlib 库绘图 博客
  • 相关阅读:
    [CTF隐写]png中CRC检验错误的分析
    Bugku
    Bugku
    【CTF 攻略】CTF比赛中关于zip的总结
    sqlserver中利用Tran_sql把逗号分隔的字符串拆成临时表
    H5摇一摇遇到的问题
    C# MVC 微信支付之微信模板消息推送
    各种大型网站技术架构
    ORM框架详解
    显示实现接口
  • 原文地址:https://www.cnblogs.com/I-Tegulia/p/4615919.html
Copyright © 2020-2023  润新知