Learning Matplot Library

Matplot是一个超强的Python绘图库。

在此记录学习Matplot一些例子,以便于日后可以较快的熟悉学习。

在此以量化计算的一些数据为例,如下将EDA计算解离过程的数据保存为一个CSV格式的数据文件,第一列为横坐标,其它列作为纵坐标。

# d, Ees, Eex, Erep, Epol, Edisp, Eh, E
1.3,-47.49,-66.98,135.53,-17.40,-3.69,3.66,-0.03
1.4,-40.61,-48.73,96.73,-8.88,-3.22,-1.50, -4.72
1.5,-35.05,-35.24,68.72,-2.93,-2.76,-4.50, -7.26
1.6,-28.90,-21.83,41.59,2.70,-2.16,-6.43,  -8.60
1.8,-24.15,-12.94,24.12,6.26,-1.63,-6.71,  -8.34
2.0,-19.85,-6.51,11.83,8.66,-1.12,-5.87,   -6.99
2.2,-16.87,-3.23,5.75,9.86,-0.77,-4.49,    -5.26
2.5,-13.65,-1.11,1.92,9.95,-0.44,-2.88,    -3.33
2.8,-11.36,-0.38,0.64,9.25,-0.26,-1.85,    -2.11
3.1,-9.70,-0.13,0.21,8.56,-0.16,-1.06,     -1.22
3.5,-8.07,-0.03,0.05,7.63,-0.09,-0.42,     -0.51
4.0,-6.61,0.00,0.01,6.52,-0.05,-0.09,      -0.13
4.5,-5.45,0.00,0.00,5.38,-0.02,-0.07,      -0.09
5.0,-4.56,0.00,0.00,4.51,-0.01,-0.05,      -0.07
6.0,-3.34,0.00,0.00,3.30,0.00,-0.04,       -0.04
7.0,-2.56,0.00,0.00,2.53,0.00,-0.03,       -0.03
8.0,-2.02,0.00,0.00,2.00,0.00,-0.02,       -0.02
9.0,-1.64,0.00,0.00,1.62,0.00,-0.02,       -0.02
10.0,-1.35,0.00,0.00,1.34,0.00,-0.02,      -0.01

用于绘图的python代码如下,此部分的重点在如何拟合光滑的曲线。

直接法

这部分我将绘图的相关信息直接写入在python代码中。

#!/bin/env python2
# -*- encoding:utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline
from scipy import interpolate

plt.figure(figsize=(8,4), dpi=300)
#plt.figure(dpi=300)
dataFile = 'summary'
data = np.genfromtxt(dataFile, delimiter=',').transpose()
newx = np.linspace(data[0].min(), data[0].max(), 500)
#for kind in ['nearest', 'zero', 'slinear', 'quadratic', 'cubic']:
for kind in ['cubic']:
    f = interpolate.interp1d(data[0], data[7], kind=kind)
    newy = f(newx)
    plt.plot(newx, newy, label=kind)
    plt.plot(data[0], data[7], '+')

plt.title('The Interaction Energy Between $NH_4$...$OH_2$')
plt.xlabel('d/A')
plt.ylabel('E/$kcal.mol^{-1}$')
plt.xlim([1, 10.5])
plt.ylim([-9, 0.2])
plt.legend(loc='lower right')
plt.savefig('test1.png', dpi=120)

对其中的函数进行简单的简介:

  • numpy.genfromtxt 从文件中读取数据
  • numpy.linspace 在某一数据段产生一系列均匀的数据。用于后面拟合滑曲线。
  • scipy.interpolate.interp1d 使用指定算法对数据进行拟合,得到一个函数。
  • pyplot.figure 用于设定画布相关属性
  • matplotlib.pyplot.plot 绘制图形
  • matplotlib.pyplot.title 设定图形的标题
  • matplotlib.pyplot.xlabel, ylabel 分别设定横坐标、纵坐标的标签
  • matplotlib.pyplot.xlim, ylim 分别设定横坐标、纵坐标的取值范围
  • matplotlib.pyplot.legend 设定关于图例的一些属性
  • matplotlib.pyplot.savefig 输出图形至文件

使用json作配置文件

这里我将绘图的一些配置信息以配置文件的方式存放,绘制图形时从配置文件中读取数据。最初愿望是实现数据,程序,配置三者分离,便于通用的处理大量数据。配置文件为json格式,如下:

{
    "inputfile": {
                    "name":"summary", 
                    "delimiter":","
                 },
    "outputfile": {
                    "name":"test2.png",
                    "dpi":120
                  },
    "title": "The Interaction Energy Between $NH_4$...$OH_2$",
    "xlabel": "d/A",
    "ylabel": "E/$kcal.mol^{-1}$",
    "xrange": [1, 10.5],
    "yrange": [-9, 0.2],
    "legend": {
                "location": "lower right"
              }
}

绘图代码略有修改,如下:

#!/bin/env python2
# -*- encoding:utf-8 -*-

#
# Author: Liu Hui
# Date: Thu Mar 14 23:21:42 CST 2013
#
# 利用matplotlib图形库来处理数据作图。需要安装matplotlib包。
#
# 此处将作图的一些配置文件写为json的格式导入,希望可以做到数据,程序,控制分离
#

import json
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline
from scipy import interpolate

f = open('config.json','r')
config = json.load(f)
f.close()

data = np.genfromtxt(config['inputfile']['name'],
                        delimiter=config['inputfile']['delimiter']).transpose()
newx = np.linspace(data[0].min(), data[0].max(), 500)
#for kind in ['nearest', 'zero', 'slinear', 'quadratic', 'cubic']:
for kind in ['cubic']:
    f = interpolate.interp1d(data[0], data[7], kind=kind)
    newy = f(newx)
    plt.plot(newx, newy, label=kind)
    plt.plot(data[0], data[7], '+')
plt.title(config['title'])
plt.xlabel(config['xlabel'])
plt.ylabel(config['ylabel'])
plt.xlim(config['xrange'])
plt.ylim(config['yrange'])
plt.legend(loc=config['legend']['location'])
plt.savefig(config['outputfile']['name'], dpi=config['outputfile']['dpi'])