下面整理了几乎所有matplotlib中的绘图函数,及其在不同坐标轴下的表现。
函数 | 类别 | 2D | polar | 3D | 备注 |
---|---|---|---|---|---|
imshow | 图像 | X | ❌ | ❌ | |
pcolormesh | 伪彩图 | [X,Y,]Z | X,Y,Z | ❌ | |
plot | 曲线图 | x[,y] | x[,y] | x,y[,z] | |
scatter | 散点图 | x,y/X,Y | x,y | x,y,[,z] | 可为任意维度 |
stem | 茎叶图 | x,y | x,y | x,y[,z] | |
step | 阶梯图 | x,y | x,y | x,y[,z] | |
bar | 条形图 | x,y | x,y | x,y[,z] | |
barh | 横向条形图 | x,y | x,y | ❌ | |
stackplot | 填充线图 | x,y | x,y | ❌ | |
plot_surface | 曲面图 | ❌ | ❌ | x,y,z | x,y必须是网格 |
plot_wireframe | 网格图 | ❌ | ❌ | x,y,z | x,y必须是网格 |
plot_trisurf | 三角面图 | ❌ | ❌ | x,y,z | x,y,z是一维数组 |
errorbar | 误差线 | x,y xerr,yerr |
x,y xerr,yerr |
x,y,z xerr,yerr,zerr |
|
fill_between | 纵向区间图 | x,y1,y2 | x,y1,y2 | ❌ | |
fill_betweenx | 横向区间图 | y, x1, x2 | x,y1,y2 | ❌ | |
contour | 等高线 | [x,y,]z | [x,y,]z | x,y,z | |
contourf | 填充等高线 | [x,y,]z | [x,y,]z | x,y,z | |
quiver | 向量场图 | x,y,u,v | x,y,u,v | x,y,z,u,v,w | |
streamplot | 流场图 | x,y,u,v | x,y,u,v | ❌ | |
barbs | 风场图 | x,y,u,v | x,y,u,v | ❌ | |
hist | 直方图 | x | x | x | |
boxplot | 箱线图 | x | x | x | |
violinplot | 小提琴图 | x | x | ❌ | |
enventplot | 信封图 | x | x | ❌ | |
hist2d | 二维直方图 | x,y | x,y | ❌ | |
hexbin | 钻石图 | x,y | x,y | ❌ | |
pie | 饼图 | x | x | ❌ | |
tricontour | 自由等高线 | x,y,z | x,y,z | x,y,z | |
tricontourf | 自由填充等高线 | x,y,z | x,y,z | x,y,z | |
tricolor | 自由伪彩图 | x,y,z | ❌ | ||
triplot | 三角骨架图 | x,y | ❌ |
之所以闲得无聊总结matplotlib中的绘图函数,是为了在Python绘图系统中正确第调用它们。
有了这些绘图类别后,首先绘图字典需要更新,这个字典不能做成全局变量,因为其中的ax,其实是作为函数参数传递进来的。
funcDct = {
"点线图" : ax.plot, "曲线图" : ax.plot,
"散点图" : ax.scatter,
"图像" : ax.imshow, "伪彩图" : ax.pcolormesh,
"条形图" : ax.bar, "横向条形图": ax.barh,
"茎叶图" : ax.stem, "阶梯图": ax.step,
"填充图" : ax.stackplot,
"误差线" : ax.errorbar,
"区间图" : ax.fill_between, "横向区间图": ax.fill_betweenx,
"曲面图" : ax.plot_surface,
"网格图" : ax.plot_wireframe,
"三角面图": ax.plot_trisurf,
"等高线" : ax.contour, "填充等高线" : ax.contourf,
"向量场图": ax.quiver, "流场图":ax.streamplot,
"风场图" : ax.barbs,
"直方图" : ax.hist, "二维直方图":ax.hist2d,
"钻石图" : ax.hexbin, "信封图" : ax.enventplot,
"箱线图" : ax.boxplot, "小提琴图":violinplot,
"饼图" : ax.pie,
"自由等高线" : ax.tricontour,
"自由填充等高线" : ax.tricontourf,
"自由伪彩图" : ax.tricolor,
"三角骨架图" : ax.triplot
}
然后据此更新self.TYPES,
self.TYPES = [
"点线图", "曲线图", "散点图", "图像", "伪彩图" ,
"条形图", "横向条形图", "茎叶图", "阶梯图", "填充图",
"误差线", "区间图", "横向区间图", "曲面图",
"网格图", "三角面图", "等高线", "填充等高线",
"向量场图", "流场图", "风场图", "直方图", "二维直方图",
"钻石图", "信封图", "箱线图", "小提琴图", "饼图",
"自由等高线", "自由填充等高线" ,"自由伪彩图" ,"三角骨架图"]
这些绘图函数建议使用的坐标系如下表
绘图函数 | 图像类别 | 建议的坐标 |
---|---|---|
imshow | 图像 | None |
plot_surface | 曲面图 | 3d |
plot_wireframe | 网格图 | 3d |
plot_trisurf | 三角面图 | 3d |
tricontour | 自由等高线 | None, polar, 3d |
plot | 曲线图 | None, polar, 3d |
scatter | 散点图 | None, polar, 3d |
stem | 茎叶图 | None, polar, 3d |
step | 阶梯图 | None, polar, 3d |
bar | 条形图 | None, polar, 3d |
errorbar | 误差线 | None, polar, 3d |
contour | 等高线 | None, polar, 3d |
quiver | 向量场图 | None, polar, 3d |
pcolormesh | 伪彩图 | None, polar |
barh | 横向条形图 | None, polar |
stackplot | 填充线图 | None, polar |
fill_between | 区间图 | None, polar, |
fill_betweenx | 横向区间图 | None, polar, |
streamplot | 流场图 | None, polar |
barbs | 风场图 | None, polar |
violinplot | 小提琴图 | None, polar |
enventplot | 信封图 | None, polar |
hist2d | 二维直方图 | None, polar |
hexbin | 钻石图 | None, polar |
pie | 饼图 | None, polar |
tricolor | 自由伪彩图 | None, polar |
triplot | 三角骨架图 | None, polar |
contourf | 填充等高线 | None, polar |
tricontourf | 自由填充等高线 | None, polar |
hist | 直方图 | None, polar |
boxplot | 箱线图 | None, polar |
tricontour | 自由等高线 | None, polar, 3d |
plot | 曲线图 | None, polar, 3d |
scatter | 散点图 | None, polar, 3d |
stem | 茎叶图 | None, polar, 3d |
step | 阶梯图 | None, polar, 3d |
bar | 条形图 | None, polar, 3d |
errorbar | 误差线 | None, polar, 3d |
contour | 等高线 | None, polar, 3d |
quiver | 向量场图 | None, polar, 3d |
故更改cbTypeChanged函数如下
def cbTypeChanged(self, evt):
t = self.drawVars['type'].get()
p = self.drawVars['proj'].get()
NO3D = ("曲线图", "散点图", "茎叶图", "条形图", "误差线",
"等高线", "向量场图", "自由等高线")
if t in ("图像"):
self.wDct['proj']['value'] = ("None")
elif t in ("曲面图", "网格图", "三角面图"):
self.wDct['proj']['value'] = ("3d")
elif t in NO3D:
self.wDct['proj']['value'] = ("None", "3d", "polar")
else:
self.wDct['proj']['value'] = ("None", "polar")
projs = self.wDct['proj']['value']
if p not in projs:
self.drawVars['proj'].set(projs[0])
self.cbProjChanged(None)
接下来要为这些绘图函数分类,发现最特殊的是imshow,只支持平面直角坐标;然后是三个三维绘图函数,只支持三维直角坐标,剩下的基本都同时支持平面直角和极坐标。
def cbTypeChanged(self, evt):
t = self.drawVars['type'].get()
p = self.drawVars['proj'].get()
NO3D = ("曲线图", "散点图", "茎叶图", "条形图", "误差线",
"等高线", "向量场图", "自由等高线")
if t in ("图像"):
self.wDct['proj']['value'] = ("None")
elif t in ("曲面图", "网格图", "三角面图"):
self.wDct['proj']['value'] = ("3d")
elif t in NO3D:
self.wDct['proj']['value'] = ("None", "3d", "polar")
else:
self.wDct['proj']['value'] = ("None", "polar")
projs = self.wDct['proj']['value']
if p not in projs:
self.drawVars['proj'].set(projs[0])
self.cbProjChanged(None)
最后,是坐标的变化,直角坐标系下的变化为
def cbProjNone(self, t):
if t in ("点线图", "曲线图"):
self.wDct['dim']['value'] = ('x', 'xy')
elif t in ("图像", "直方图", "饼图", "箱线图", "小提琴图", "信封图"):
self.wDct['dim']['value'] = ('x')
elif t in ("误差线", "向量场图", "流场图", "风场图"):
self.wDct['dim']['value'] = ('xyuv')
elif t in ("伪彩图", "等高线", "填充等高线"):
self.wDct['dim']['value'] = ('x', 'xyz')
elif t in ("区间图", "横向区间图", "自由等高线", "自由伪彩图", "自由填充等高线"):
self.wDct['dim']['value'] = ('xyz')
else:
self.wDct['dim']['value'] = ('xy')
经过对比发现,极坐标情况下除了没有imshow之外,其他绘图函数的坐标轴的个数与直角坐标基本相同,所以就直接调用cbProjNone了。
def cbProjPolar(self, t):
self.cbProjNone(t)
相比之下,三维坐标的情况可能更加简单一些
def cbProj3d(self, t):
if t in ("向量场图", "误差线"):
self.wDct['dim']['value'] = ('xyzuvw')
elif t in ("曲线图", "散点图", "茎叶图", "阶梯图", "条形图"):
self.wDct['dim']['value'] = ('xy', 'xyz')
else:
self.wDct['dim']['value'] = ('xyz')
至此,就成功载入了几乎所有matplotlib中的函数。
Python绘图系统: