matplotlib - juedaiyuer/researchNote GitHub Wiki
matplotlib
安装python-dev
安装这个包,以后安装各种python扩展包,可以省很多事情
sudo apt-get install python-dev
使用apt-get安装
sudo apt-get install python-numpy
sudo apt-get install python-scipy
sudo apt-get install python-matplotlib
使用pip安装
需要安装编译所需库
sudo apt-get build-dep python-numpy
sudo apt-get build-dep python-scipy
Gallery页面中有上百幅缩略图,打开之后都有源程序,需要绘制某种类型的图,copy一下
简单的例子
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 1000)
y = np.sin(x)
z = np.cos(x**2)
plt.figure(figsize=(8,4))
plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2)
plt.plot(x,z,"b--",label="$cos(x^2)$")
plt.xlabel("Time(s)")
plt.ylabel("Volt")
plt.title("PyPlot First Example")
plt.ylim(-1.2,1.2)
plt.legend()
plt.show()
matplotlib中的快速绘图的函数库载入
import matplotlib.pyplot as plt
figsize,确保当图片保存到磁盘时具有一定的大小和纵横比;单位是英寸
查看格式化字符串的详细配置
ylim:设置Y轴的范围
##配置属性##
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(0, 5, 0.1)
>>> line, = plt.plot(x, x*x)
Figure的Subplot
matplotlib的图像都位于Figure对象中
fig=plt.figure()
ax1=fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
plt.show()

发出一条绘图命令,matplotlib就会在最后一个用过的subplot上进行绘制,如果没有则会创建一个
from numpy.random import randn
plt.plot(randn(50).cumsum(),'k--')

k--是一个线型选项,绘制黑色虚线图
_=ax1.hist(randn(100),bins=20,color='k',alpha=0.3)
ax2.scatter(np.arange(30),np.arange(30)+3*randn(30))

##调整subplot周围的间距##
Figure的subplots_adjust方法可以轻而易举地修改间距,它也是顶级函数
wspace和hspace用于控制宽度和高度的百分比,可以用做subplot之间的间距
##颜色,标记和线型##
plot函数接受一组X和Y坐标,还可以接受一个表示颜色和线型的字符串缩写
ax.plot(x,y,'g--')
ax.plot(x,y,linestyle='--',color='g')
常见的颜色都有一个缩写词,也可以通过RGB值指定
##刻度,标签和图例##
对于大多数的图表装饰项,其主要实现方式
- 使用过程型的pyplot接口(MATLAB用户非常熟悉)
- 面向对象的原生matplotlib API
##设置标题,轴标签,刻度以及刻度标签##
创建一个简单的图像并绘制一段随机漫步
fig=plt.figure();ax=fig.add_subplot(1,1,1)
ax.plot(np.random.randn(1000).cumsum())
plt.show()

修改X轴的刻度,最简单的方法是使用set_xticks和set_xticklabels
ticks=ax.set_xticks([0,250,500,750,1000])
labels=ax.set_xticklabels(['one','two','three','four','five'],rotation=30,fontsize='small')
设置一个标题
ax.set_title('matplotlib test')
X轴设置一个名称
ax.set_xlabel('Stages')

##添加图例##
fig=plt.figure();ax=fig.add_subplot(1,1,1)
ax.plot(np.random.randn(1000).cumsum(),'k',label='one')
ax.plot(np.random.randn(1000).cumsum(),'k--',label='two')
ax.plot(np.random.randn(1000).cumsum(),'k.',label='three')
ax.legend(loc='best')

##注解以及在subplot上绘图##
注解可以通过text,arrow,annotate等函数进行添加
matplotlib有一些表示常见图形的对象,这些对象被称之为块(patch),matplotlib.pyplot中找到(如Rectangle和Circle),但完整的集合位于matplotlib.patches
##将图标保存到文件##
plt.savefig('figure.svg')
dpi控制每英寸点数分辨率,bbox_inches可以裁剪当前图表周围的空白部分
plt.savefig('figure.png',dpi=400,bbox_inches='tight')
savefig并非写入磁盘,也可以写入任何文件型的对象,eg:StringIO,在Web上提供动态图片
form io import StringIO
buffer=StringIO()
plt.savefig(buffer)
plot_data=buffer.getvalue()
##matplotlib配置##
###Python编程方式###
rc方法,第一个参数为自定义对象,如figure,axes,xtick,ytick,grid,legend...
plt.rc('figure',figsize=(10,10))
最简单的方式是将这些选项写成一个字典
font_options={'family':'monospace',
'weight':'bold'
'size':'small'
}
plt.rc('font',**font_options)
全部的自定义选项,matplotlib的配置文件matplotlibrc(位于matplotlib/mpl-data目录中)
查看配置
import matplotlib
matplotlib.rcParams