python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python绘图函数

python35种绘图函数详细总结

作者:微小冷

Python有许多用于绘图的函数和库,比如Matplotlib,Plotly,Bokeh,Seaborn等,这只是一些常用的绘图函数和库,Python还有其他绘图工具,如Pandas、ggplot等,选择适合你需求的库,可以根据你的数据类型、图形需求和个人偏好来决定,本文给大家总结了python35种绘图函数

基础图

下面这8种图像一般只有两组坐标,直观容易理解。

函数坐标参数图形类别
plotx,y曲线图
stackplotx,y散点图
stemx,y茎叶图
scatterx,y散点图
polarx,y极坐标图
stepx,y步阶图
barx,y条形图
barhx,y横向条形图

其中,除了极坐标需要添加一个极坐标映射之外,其他函数均在直角坐标系中绘制,效果如下

在这里插入图片描述

绘图代码如下

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(25)/3
y = np.sin(x)
fDct = {"plot" : plt.plot,  "stackplot": plt.stackplot,
        "stem" : plt.stem,  "scatter"  : plt.scatter,         
        "polar": plt.polar, "step"     : plt.step, 
        "bar"  : plt.bar,   "barh"     : plt.barh, }
fig = plt.figure(figsize=(14,6))
for i,key in enumerate(fDct, 1):
    p = "polar" if key=="polar" else None
    ax = fig.add_subplot(2,4,i, projection=p)
    fDct[key](x, y)
    plt.title(key)
plt.tight_layout()
plt.show()

误差线

实际绘图时,误差线这种需求十分常见,尤其是在做拟合的时候,除了要画出趋势线之外,还可能要画出其抖动的范围,下面三种函数主要实现这个功能。

函数坐标图形类别
errorbarx,y,xerr,yerr误差线
fill_betweenx,y1,y2纵向区间图
fill_betweenxy, x1, x2横向区间图

图像效果为

在这里插入图片描述

绘图代码如下,errorbar函数的误差方向,与输入的参数有关。

x = np.arange(25)/3
y = np.sin(x)
y1, y2 = 0.9*y, 1.1*y
x1, x2 = 0.9*x, 1.1*x
xerr = np.abs([x1, x2])/10
yerr = np.abs([y1, y2])/10
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(221)
ax.errorbar(x, y, yerr=yerr)
plt.title("errorbar with yerr")
ax = fig.add_subplot(222)
ax.errorbar(x, y, xerr=xerr)
plt.title("errorbar with xerr")
ax = fig.add_subplot(223)
ax.fill_between(x, y1, y2)
plt.title("fill_between")
ax = fig.add_subplot(224)
ax.fill_betweenx(y, x1, x2)
plt.title("fill_betweenx")
plt.tight_layout()
plt.show()

三维图

绘图函数坐标绘图类型坐标说明
plotx,y,z三维曲线图
scatterx,y,z三维散点图
plot_surfacex,y,z三维曲面图x,y必须是网格
plot_wireframex,y,z三维网格图x,y必须是网格
plot_trisurfx,y,z三角曲面图x,y,z是一维数组

plot和scatter虽然是二维绘图函数,但如果新增一个三维坐标,就可以摇身一变,成为三维曲线图或者三维散点图

在这里插入图片描述

绘图代码如下

x = np.arange(100)/10
y,z = np.sin(x), np.cos(x)
fig = plt.figure(figsize=(8,4))
ax = fig.add_subplot(121, projection='3d')
ax.plot(x,y,z)
plt.title("plot")
ax = fig.add_subplot(122, projection='3d')
ax.scatter(x,y,z)
plt.title("scatter")
plt.tight_layout()
plt.show()

真正专业的三维图是plot_surface, plot_wireframe和plot_trisurf

在这里插入图片描述

如果仔细看就会发现plot_trisurf的纹理和前两者不同,相比之下,前两者必须要求输入规整的数据。绘图代码如下

X, Y = np.indices([30, 30])/3 - 5
Z = np.sin(np.sqrt(X**2 + Y**2))
fig = plt.figure(figsize=(12,5))
ax = fig.add_subplot(131, projection='3d')
ax.plot_surface(X, Y, Z)
plt.title("plot_surface")
ax = fig.add_subplot(132, projection='3d')
ax.plot_wireframe(X, Y, Z)
plt.title("plot_wireframe")
ax = fig.add_subplot(133, projection='3d')
ax.plot_trisurf(X.reshape(-1), Y.reshape(-1), Z.reshape(-1))
plt.title("plot_trisurf")
plt.tight_layout()
plt.show()

等高线图

绘图函数坐标说明
contour[x,y,]z等高线
contourf[x,y,]z填充等高线
pcolormesh[x,y,]z伪彩图
imshowz图像

其中,imshow就是正常的图片展示函数,这几个函数可以只指定z轴然后绘图

X, Y = np.indices([100,100])/30 - 1.5
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
fDct = {"contour": plt.contour, "contourf":plt.contourf, 
    "pcolormesh" : plt.pcolormesh, "imshow":plt.imshow}
fig = plt.figure(figsize=(9,6))
for i,key in enumerate(fDct, 1):
    ax = fig.add_subplot(2,2,i)
    fDct[key](Z)
    plt.title(key)
plt.tight_layout()
plt.show()

绘图结果如下

在这里插入图片描述

可以看到,imshow和另外三种函数的区别是,其横坐标和纵坐标之间的比例始终是1:1,并不随着图像的拉伸而放大或者缩小。

除了imshow之外,另外三种函数还支持输入x,y,z三个坐标轴的数据来绘图,效果如下

在这里插入图片描述

绘图代码如下

X, Y = np.indices([100,100])/30 - 1.5
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
fDct = {"contour": plt.contour, "contourf":plt.contourf, 
    "pcolormesh" : plt.pcolormesh}
fig = plt.figure(figsize=(9,3))
for i,key in enumerate(fDct, 1):
    ax = fig.add_subplot(1,3,i)
    fDct[key](X,Y,Z)
    plt.title(key)
plt.tight_layout()
plt.show()

场图

绘图函数坐标说明
quiverx,y,u,v向量场图
streamplotx,y,u,v流场图
barbsx,y,u,v风场图

quiver以单点为单位,绘制出某点处向量的方向;streamplot则在此之上,将每个点衔接到一起,显得更加有流动性;barbs则是以风向标志取代了向量,这个图过于专业,我应该没啥机会用到。

Y, X = np.indices([6,6])/0.75 - 4
U = X + Y
V = Y - X
dct = {"quiver":plt.quiver, "streamplot":plt.streamplot, 
       "barbs" :plt.barbs}
fig = plt.figure(figsize=(12,4))
for i,key in enumerate(dct, 1):
    ax = fig.add_subplot(1,3,i)
    dct[key](X,Y,U,V)
    plt.title(key)
plt.tight_layout()
plt.show()

在这里插入图片描述

统计图

绘图函数坐标说明
histx数据直方图
boxplotx箱线图
violinplotx小提琴图
enventplotx平行线疏密图
hist2dx,y二维直方图
hexbinx,y钻石图
piex饼图

其中hist, boxplot, violinplot, enventplot是统计一维数据的,可直接输入随机数,绘图函数会自行统计其区间

在这里插入图片描述

绘图代码如下

x = np.random.standard_normal(size=1000)
dct = {"hist"  : plt.hist, "violinplot" : plt.violinplot,
      "boxplot": plt.boxplot}
fig = plt.figure(figsize=(10,6))
for i,key in enumerate(dct, 1):
    ax = fig.add_subplot(2,2,i)
    dct[key](x)
    plt.title(key)
ax = fig.add_subplot(224)
ax.eventplot(x)
plt.title("eventplot")
plt.tight_layout()
plt.show()

hist2d和hexbin用于统计二维数据,最终以图像的形式展示出来,二者在观感上的主要区别是,hist2d的“像素”是方形的,而hexbin则是六边形的。

在这里插入图片描述

绘图代码如下

x = np.random.randn(5000)
y = 1.2 * x + np.random.randn(5000) / 3
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(121)
ax.hist2d(x, y, bins=[np.arange(-3,3,0.1)] * 2)
plt.title("hist2d")
ax = fig.add_subplot(122)
ax.hexbin(x, y, gridsize=20)
plt.title("hexbin")
plt.tight_layout()
plt.show()

最后还有一个饼图,饼图要求输入坐标必须都大于0,绘图代码如下

plt.pie([1,2,3,4,5])
plt.tight_layout()
plt.show()

在这里插入图片描述

非结构坐标图

下面这四个绘图函数有一个特点,即其绘图坐标并不是格式化的,而支持随机坐标进行绘图,这一点和plot_trisurf比较相似

绘图函数坐标说明
tricontourx,y,z非结构等高线
tricontourfx,y,z非结构化填充等高线
tricolorx,y,z非结构化伪彩图
triplotx,y三角连线图

在这里插入图片描述

绘图代码如下

x = np.random.uniform(-4, 4, 256)
y = np.random.uniform(-2, 2, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
levels = np.linspace(z.min(), z.max(), 7)
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(221)
ax.plot(x, y, 'o', markersize=1, color='lightgrey', alpha=0.5)
ax.tricontour(x, y, z, levels=levels)
plt.title("tricontour")
ax = fig.add_subplot(222)
ax.plot(x, y, 'o', markersize=1, color='lightgrey', alpha=0.5)
ax.tricontourf(x, y, z, levels=levels)
plt.title("tricontourf")
ax = fig.add_subplot(223)
ax.plot(x, y, 'o', markersize=1, color='lightgrey', alpha=0.5)
ax.tripcolor(x, y, z)
plt.title("tripcolor")
ax = fig.add_subplot(224)
ax.triplot(x,y)
plt.title("triplot")
plt.tight_layout()
plt.show()

以上就是python35种绘图函数详细总结的详细内容,更多关于python绘图函数的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文