最后一次更新日期: 2019/4/20
Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。
按需导入以下模块:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
1. 注解annotate
fig=plt.figure(figsize=(10,3.5))
ax=fig.add_subplot(121,title='annotate')
ax.scatter([-0.25],[-0.25],s=100)
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
bbox_style = dict(boxstyle="square", fc='white', ec='black',lw=1)
arrow_style = dict(arrowstyle='->',color='black',lw=2)
ax.annotate('This is a dot.',(-0.24,-0.24),(-0.14,0.16),
arrowprops=arrow_style,bbox=bbox_style,fontsize=15)
ax=fig.add_subplot(122,title='arrow+text')
ax.scatter([-0.25],[-0.25],s=100)
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
ax.arrow(0.09,0.06,-0.25,-0.23,fc='k',ec='k',
width=0.01,head_width=0.07,head_length=0.07)
ax.text(-0.14,0.16,'This is a dot.',fontsize=15,bbox=bbox_style)
annotate方法用于向图像上添加注解:第一个参数
s设置注释的文本;第二个参数
xy设置要注释的点位置,tuple类型表示的坐标;第三个参数
xytext设置注释文本的位置,tuple类型表示的坐标;参数
xycoords和textcoords设置注释点位置和文本位置所采用的坐标系,默认'data'和数据的坐标系一致;参数
arrowprops设置箭头,dict类型,其中arrowstyle属性设置样式,color属性设置颜色,lw属性设置箭头宽度;以上三个参数具体信息建议参考官方文档-annotate;
参数
bbox设置文本框样式,dict类型,其中boxstyle属性设置样式,fc属性设置填充颜色,ec属性设置边缘颜色,lw属性设置边缘线宽度;bbox中的详细设置建议参考官方文档-Rectangle;参数
fontsize设置字体大小。
注解也可通过arrow+text实现。
arrow方法用于添加箭头:
第1,2个参数x,y设置箭头的起始位置;
第3,4个参数dx,dy设置箭头往xy方向延伸的长度;
fc参数设置填充颜色;ec参数设置边缘颜色;
width参数设置箭头线宽度;
head_width参数设置箭头头部的宽度;
head_length参数设置箭头头部的长度。
text方法用于添加文本:
第1,2个参数x,y设置文本的位置;
第3个参数s设置要显示的文本;
参数fontsize设置字体大小;
参数bbox设置文本框样式,与annotate相同。
arrow绘制的箭头在有所倾斜时无法保证头部的底部与线垂直,对此有要求只能使用annotate。
2. 区域填充fill
x=np.arange(0,720,1)
y1=np.sin(x*np.pi/180)
y2=np.cos(x*np.pi/180)
fig=plt.figure(figsize=(10,3.5))
ax=fig.add_subplot(121,title='fill')
ax.plot(x,y1)
ax.plot(x,y2)
ax.fill(x,y1,color='g',alpha=0.3)
ax.fill(x,y2,color='b',alpha=0.3)
ax=fig.add_subplot(122,title='fill between')
ax.plot(x,y1)
ax.plot(x,y2)
ax.fill_between(x,y1,y2,color='g',alpha=0.3)
fill方法用于填充多边形:第1,2个参数
x,y设置边的xy坐标,该绘图方法不适合填充不封闭的曲线,会如上图出现无法预估的绘制效果。
fill_between方法用于填充两条曲线中间的区域:
第1,2,3个参数x,y1,y2设置x坐标和两条曲线的y坐标;
第4个参数where设置绘制的横坐标范围,布尔数组类型,相当于对前三个参数执行索引筛选。
3. 图片image
from PIL import Image
image1=Image.open('D:\\training_data\\used\\cifar-10-batches-py\\test\\1_猫.png')
image2=Image.open('D:\\training_data\\used\\cifar-10-batches-py\\test\\2_船.png')
fig=plt.figure(figsize=(8,4))
ax=fig.add_subplot(121,title='image1')
ax.imshow(image1)
ax=fig.add_subplot(122,title='image2')
ax.imshow(image2)
imshow用于显示图片,默认是会显示坐标轴和刻度的,可通过Axes.axis('off')关闭。
4. 基本图形patch
import matplotlib.patches as patches
from matplotlib.collections import PatchCollection
fig=plt.figure(figsize=(9,3))
ax=fig.add_subplot(121,title='Rectangle')
rects=[]
x=[1.5,3.5,5.5,]
y=[3,4.5,3]
for i in range(3):
rect=patches.Rectangle((x[i],y[i]),3,3)
rects.append(rect)
pc=PatchCollection(rects,linewidth=1,edgecolor='r',facecolor='none')
#ax.add_patch(rect)
ax.add_collection(pc)
ax.set_xlim([0,10])
ax.set_ylim([0,10])
ax=fig.add_subplot(122,title='Ellipse')
ells=[]
for i in range(5):
ell=patches.Ellipse((5,5),6,3,angle=i*36)
ells.append(ell)
pc=PatchCollection(ells,facecolor='g',alpha=0.5)
#ax.add_patch(ell)
ax.add_collection(pc)
ax.set_xlim([0,10])
ax.set_ylim([0,10])
绘制基本图形和相应集合需要导入
patches和PatchCollection。
patches提供了各种图形的构造:
Rectangle是矩形类,第1个参数xy设置左下角顶点的坐标,第2,3个参数width,height设置宽度和高度,第4个参数angle设置旋转角度;
Ellipse是椭圆类,第1个参数xy设置椭圆中心的坐标,第2,3个参数width,height设置横轴和竖轴的长度(直径),第4个参数angle设置旋转角度。
PatchCollection用于构造patches集合并设置通用的拓展参数:
linewidth参数设置边缘线宽;
edgecolor参数设置边缘颜色;
facecolor参数设置填充颜色,facecolor='none'可以设置不填充(在创建图形类时,fill=False也能设置不填充);
alpha参数设置透明度。
add_patch用于向Axes中添加单个图形;
add_collection用于向Axes中添加图形集合;
Axes.patches可以查看Axes下的所有Patch绘图对象;
Axes.collections可以查看Axes下的所有绘图集合。
5. 数据表格table
data=np.array([[1,2,3],[4,5,6],[7,8,9]])
row_labels=['row1','row2','row3']
row_colors=plt.cm.BuPu(np.linspace(0, 0.5, len(row_labels)))
col_labels=['col1','col2','col3']
col_index=np.arange(3)
fig=plt.figure()
ax=fig.add_subplot(111,title='table')
for i in range(len(row_labels)-1,-1,-1):
ax.bar(col_index,data[i],color=row_colors[i],linewidth=0.5,edgecolor='k')
ax.table(cellText=data,
rowLabels=row_labels,
rowColours=row_colors,
colLabels=col_labels,
loc='bottom')
ax.set_xticks([])
table方法用于添加表格:
cellText参数设置单元格数据,二维序列类型,默认None;
cellColors参数设置单元格颜色,二维序列类型,默认None;
cellText和cellColors两个参数至少有一个要赋值;
cellLoc参数设置单元格位置,默认'right';
colWidths参数设置列宽,一维序列类型,可选;
rowLabels参数设置行标签,一维序列类型,可选;
rowColors参数设置行标签颜色,一维序列类型,可选;
rowLoc参数设置行标签位置,默认'left';
colLabels参数设置列标签,一维序列类型,可选;
colColors参数设置列标签颜色,一维序列类型,可选;
colLoc参数设置列标签位置,默认'center';
loc参数设置表格位置,默认bottom;
bbox参数设置方框样式,可选。
更详细的设置可以自行创建Table对象,通过Axes.add_table方法添加;
Axes.tables可以查看Axes下的所有Table绘图对象。











网友评论