原文地址: !()[http://www.bugingcode.com/blog/Matplotlib_7_Effectively_Using.html]

这是一篇关于如何高效的使用Matplotlib 的文章,文章的地址在 原文,但是这里不准备一行一行的对文章的内容进行翻译,而是把主要的步骤和思想都记录下来,在进行项目绘制的时候能够有很好的帮助。

获取数据和数据格式

要进行数据的绘制时,数据一般存放在文档里,比如cvs或者是excel中,读取时使用 pandas 进行操作,这里 下有专门的介绍,这里不在详细的介绍了。

数据从 https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true 中得到,这里看看数据的读取和格式:

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import pandas as pd
from matplotlib.ticker import FuncFormatter df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true")
print df.head()

这里打印出sample-salesv3.xlsx 中前面4行的数据,数据的格式如下所示:

商品id,商品名称,商品sku,销售数量,销售单价,产品销售额,时间。

数据排行和展示

我们想知道哪些商品的销售最高,需要对数据进行排序,并取到前10位:

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import pandas as pd
from matplotlib.ticker import FuncFormatter df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true")
#对ext price 进行排序,并取得前10位
top_10 = (df.groupby('name')['ext price', 'quantity'].agg({'ext price': 'sum', 'quantity': 'count'})
.sort_values(by='ext price', ascending=False))[:10].reset_index()
#重新命名 ext price 换成 Sales,quantity 换成 Purchases
top_10.rename(columns={'name': 'Name', 'ext price': 'Sales', 'quantity': 'Purchases'}, inplace=True) print top_10

结果如下:

                           Name      Sales  Purchases
0 Kulas Inc 137351.96 94
1 White-Trantow 135841.99 86
2 Trantow-Barrows 123381.38 94
3 Jerde-Hilpert 112591.43 89
4 Fritsch, Russel and Anderson 112214.71 81
5 Barton LLC 109438.50 82
6 Will LLC 104437.60 74
7 Koepp Ltd 103660.54 82
8 Frami, Hills and Schmidt 103569.59 72
9 Keeling LLC 100934.30 74

取出了前面10名的销售额,第一位为 Kulas Inc,第二位White-Trantow ,等。

在图表上对这些数据进行绘制:

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import pandas as pd
from matplotlib.ticker import FuncFormatter df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true")
#对ext price 进行排序,并取得前10位
top_10 = (df.groupby('name')['ext price', 'quantity'].agg({'ext price': 'sum', 'quantity': 'count'})
.sort_values(by='ext price', ascending=False))[:10].reset_index()
#重新命名 ext price 换成 Sales,quantity 换成 Purchases
top_10.rename(columns={'name': 'Name', 'ext price': 'Sales', 'quantity': 'Purchases'}, inplace=True) plt.style.use('ggplot') top_10.plot(kind='barh', y="Sales", x="Name") plt.show()

得到各个商品的销售额:

图表自定义

自定义在图表的绘制中起到了美化和增强可读性的作用,对图表的说明和样式的改变,能够使你的图表看上去专业很多。

对图表的说明和坐标范围限制:

把上面 top_10.plot(kind='barh', y="Sales", x="Name") 代码更换为以下的代码,代码的功能也很清楚,限制x轴坐标,设置标题,x轴说明:

fig, ax = plt.subplots(figsize=(5, 6))
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)
ax.set_xlim([-10000, 140000])
ax.set(title='2014 Revenue', xlabel='Total Revenue')
ax.legend().set_visible(False)

得到的结果图如下:

要想修改这个图像,你可能需要执行很多操作。图中最碍眼的可能是总收益额的格式。Matplotlib 可以使用 FuncFormatter 解决这一问题。该函数用途多样,允许用户定义的函数应用到值,并返回格式美观的字符串。

以下是货币格式化函数,用于处理数十万美元区间的数值:

def currency(x, pos):
'The two args are the value and tick position'
if x >= 1000000:
return '${:1.1f}M'.format(x*1e-6)
return '${:1.0f}K'.format(x*1e-3)

对x轴数据格式进行说明:

formatter = FuncFormatter(currency)
ax.xaxis.set_major_formatter(formatter)

同样的总的代码是把plot的代码替换为如下:

fig, ax = plt.subplots()
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)
ax.set_xlim([-10000, 140000])
ax.set(title='2014 Revenue', xlabel='Total Revenue', ylabel='Customer')
formatter = FuncFormatter(currency)
ax.xaxis.set_major_formatter(formatter)
ax.legend().set_visible(False)

对x轴进行修饰以后的图表:

多图表对比

各个销售的对比和各个商品在整体中是处于哪个地位是较为关心的话题。把平均值也绘制在图表中,可以很方便的进行对比。

# Create the figure and the axes
fig, ax = plt.subplots() # Plot the data and get the averaged
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)
avg = top_10['Sales'].mean() # Set limits and labels
ax.set_xlim([-10000, 140000])
ax.set(title='2014 Revenue', xlabel='Total Revenue', ylabel='Customer') # Add a line for the average
ax.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1) # Format the currency
formatter = FuncFormatter(currency)
ax.xaxis.set_major_formatter(formatter) # Hide the legend
ax.legend().set_visible(False)

图表如下:

目前,我们所做的所有改变都是针对单个图表。我们还能够在图像上添加多个表,使用不同的选项保存整个图像。

在这个例子中,我使用 nrows 和 ncols 指定大小,这对新用户来说比较清晰易懂。我还使用 sharey=True 以使 y 轴共享相同的标签。

该示例很灵活,因为不同的轴可以解压成 ax0 和 ax1。现在我们有了这些轴,就可以像上述示例中那样绘图,然后把一个图放在 ax0 上,另一个图放在 ax1。

# Get the figure and the axes
fig, (ax0, ax1) = plt.subplots(nrows=1,ncols=2, sharey=True, figsize=(7, 4))
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax0)
ax0.set_xlim([-10000, 140000])
ax0.set(title='Revenue', xlabel='Total Revenue', ylabel='Customers') # Plot the average as a vertical line
avg = top_10['Sales'].mean()
ax0.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1) # Repeat for the unit plot
top_10.plot(kind='barh', y="Purchases", x="Name", ax=ax1)
avg = top_10['Purchases'].mean()
ax1.set(title='Units', xlabel='Total Units', ylabel='')
ax1.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1) # Title the figure
fig.suptitle('2014 Sales Analysis', fontsize=14, fontweight='bold'); # Hide the legends
ax1.legend().set_visible(False)
ax0.legend().set_visible(False)

保存图表

Matplotlib 支持多种不同文件保存格式。你可以使用 fig.canvas.get_supported_filetypes() 查看系统支持的文件格式:

fig.canvas.get_supported_filetypes()

	{'eps': 'Encapsulated Postscript',
'jpeg': 'Joint Photographic Experts Group',
'jpg': 'Joint Photographic Experts Group',
'pdf': 'Portable Document Format',
'pgf': 'PGF code for LaTeX',
'png': 'Portable Network Graphics',
'ps': 'Postscript',
'raw': 'Raw RGBA bitmap',
'rgba': 'Raw RGBA bitmap',
'svg': 'Scalable Vector Graphics',
'svgz': 'Scalable Vector Graphics',
'tif': 'Tagged Image File Format',
'tiff': 'Tagged Image File Format'}

我们有 fig 对象,因此我们可以将图像保存成多种格式:

fig.savefig('sales.png', transparent=False, dpi=80, bbox_inches="tight")

转载请标明来之:http://www.bugingcode.com/

更多教程:阿猫学编程

Matplotlib绘图库入门(七):高效使用的更多相关文章

  1. Python图表绘制:matplotlib绘图库入门

    matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并 ...

  2. Python图表绘制:matplotlib绘图库入门(转)

    matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并 ...

  3. 使用 Python 的 matplotlib 绘图库进行绘图

    matplotlib 是 Python 最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 1  使用 Ma ...

  4. Python Matplotlib绘图库 安装

    一般我们在做科学计算的时候,首先会想到的是matlab,但是呢,一想到matlab安装包那么大,我就有点不想说什么了. Matplotlib 是python最著名的绘图库,它提供了一整套和matlab ...

  5. Python第三方库matplotlib(2D绘图库)入门与进阶

    Matplotlib 一 简介: 二 相关文档: 三 入门与进阶案例 1- 简单图形绘制 2- figure的简单使用 3- 设置坐标轴 4- 设置legend图例 5- 添加注解和绘制点以及在图形上 ...

  6. Python 绘图库Matplotlib入门教程

    0 简单介绍 Matplotlib是一个Python语言的2D绘图库,它支持各种平台,并且功能强大,能够轻易绘制出各种专业的图像. 1 安装 pip install matplotlib 2 入门代码 ...

  7. 23、matplotlib数据可视化、绘图库模块

    matplotlib官方文档:https://matplotlib.org/contents.html?v=20190307135750 matplotlib是一个绘图库,它可以创建常用的统计图,包括 ...

  8. matplotlib python高级绘图库 一周总结

    matplotlib python高级绘图库 一周总结 官网 http://matplotlib.org/ 是一个python科学作图库,可以快速的生成很多非常专业的图表. 只要你掌握要领,画图将变得 ...

  9. Matplotlib Toolkits:python高级绘图库seaborn

    http://blog.csdn.net/pipisorry/article/details/49515745 Seaborn介绍 seaborn (Not distributed with matp ...

随机推荐

  1. 判断1/N是否为无限小数

    给定一个正整数N,请判断1/N是否为无限小数,若是输出YES,若不是请输出NO. 思路: 只要被除数n可以转换成2的次幂或者2与5的组合即为有限小数,否则为无线小数 代码如下: #include &l ...

  2. Eclipse Jee 2019-12 使用的一些小窍门(积累性更新)

    在安装使用 Eclipse IDE for Enterprise Java Developers  Version: 2019-12 M1 (4.14.0) 之前请确保已在系统环境变量配置好JDK1. ...

  3. day51-线程-条件

    #条件:Condition.notify通知.notify_all.wait #条件 = 递归锁 + wait功能 #条件可以让主线程控制多少个子线程可以执行. import threading de ...

  4. Java任务调度框架之分布式调度框架XXL-Job介绍

    ​ Java任务调度框架之分布式调度框架XXL-Job介绍及快速入门 调度器使用场景: Java开发中经常会使用到定时任务:比如每月1号凌晨生成上个月的账单.比如每天凌晨1点对上一天的数据进行对账操作 ...

  5. java centos7 gcc编码 解决socket通信 汉字乱码

    1.把 Java eclipes 设置编码成utf-8 windows->preference->workspace 2.centos7 gcc 默认为utf-8

  6. eclipse优化自动补全(解决空格、分号等上屏问题)

    第一步:打开"Window"——>"Show View"——>"Other"——>找到"Plug-in Deve ...

  7. java类为什么要实现Serializable接口

    什么是Serializable接口? 一个对象序列化的接口.一个类只有实现了Serializable接口,它的对象才能被序列化. 什么是序列化? 将对象的状态信息转换为可以存储或传输的形式的过程. 在 ...

  8. 定时任务--Timer()实现

    Java的Timer以及TimerTask类可以帮助我们实现定时器功能,利用servlet监听程序可以实现WEB服务启动之后执行某些工作.两者结合就可以再web应用中实现定时器功能. 1.计划类代码S ...

  9. 迅为iTOP-4418开发板编译Ubuntu

    Ubuntu 系统比较特殊,源码就是它的镜像.Ubuntu 系统通过解压的方式进行烧写,我们也可以通过配置解压出来的 Ubuntu 系统源码文件夹,来配置 Ubuntu 系统.然后通过打包压缩的方式来 ...

  10. Seikimatsu Occult Tonneru(网络流,状态数(建不建边)不多时,可考虑直接进行枚举

    http://acm.hdu.edu.cn/showproblem.php?pid=4309 总结:边可存东西时,可新建一个点x连接u.v,x再连向汇点: #include<iostream&g ...