matplotlib绘图教程
程序员文章站
2022-03-26 18:41:05
...
matplotlib是强大的python数据可视化工具;与MATLAB类似,可以轻松移至python;易于绘制2D,3D数据;甚至可以制作动画。
安装:Linux系统上安装:$ sudo apt-get install python3-matplotlib
在windows系统上用conda和pip均可以。
例1:绘制见简单的函数图像
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 50)##
y = np.sin(x)
plt.plot(x, y)
plt.show()###显示
例2:将两个函数绘制同一幅图中
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)#x是一个数组
print(x)
y1 = 3*x **2
y2 = x**3
plt.figure()#新建一张图片绘制第一幅图像
plt.plot(x, y1)
plt.figure(figsize=(10, 10),)#再新建一张图片,将两个函数绘制到同一张图中
plt.plot(x, y2)
plt.plot(x, y1, color='blue', linewidth=1.0, linestyle='--')
plt.show()
例3:添加横轴纵轴标签
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.figure()
plt.plot(x, y2,label='cosx')
plt.plot(x, y1,label='sinx')
# set x limits
plt.xlim((-10, 10))
plt.ylim((-2, 2))
plt.xlabel('x')
plt.ylabel('y')
plt.title('sin-cos-picture')
plt.legend()
plt.show()
#例4:分块绘制:subplot
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3=x**2
y4=x**3
plt.figure()
plt.subplot(221),plt.plot(x, y2,label='cosx'),plt.title('cos')
plt.subplot(222),plt.plot(x, y1,label='sinx'),plt.title('sin')
plt.subplot(223),plt.plot(x, y3,label='x^2'),plt.title('x**2')
plt.subplot(224),plt.plot(x, y4,label='x^3'),plt.title('x**3')
# set x limits
#plt.xlim((-10, 10))
#plt.ylim((-2, 2))
plt.xlabel('x')
plt.ylabel('y')
plt.show()
例5:将图像绘制成坐标轴的形式(去掉边界线)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 50)
y1 = 2*x + 1
y2 = x**2
plt.figure()
plt.plot(x, y2,label='x**2')
# plot the second curve in this figure with certain parameters
plt.plot(x, y1,label='x**3')
# set x limits
plt.xlim((-1, 2))
plt.ylim((-2, 3))
# set new ticks
#new_ticks = np.linspace(-1, 2, 5)
#plt.xticks(new_ticks)
# set tick labels
# gca = 'get current axis'
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
# ACCEPTS: [ 'left' | 'right' | 'both' | 'default' | 'none' ]
ax.spines['left'].set_position(('data',0))
plt.show()
例6:散点图
import matplotlib.pyplot as plt
import numpy as np
import random
n = 1024 # data size
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X) # for color later on
#T=random(1,10)
plt.scatter(X, Y,c=T,alpha=0.5)
plt.xlim(-1.5, 1.5)
plt.xticks(()) # ignore xticks
plt.ylim(-1.5, 1.5)
plt.yticks(()) # ignore yticks
plt.show()
例7:柱状图
import matplotlib.pyplot as plt
import numpy as np
n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x, y in zip(X, Y1):
# ha: horizontal alignment
# va: vertical alignment
plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
for x, y in zip(X, Y2):
# ha: horizontal alignment
# va: vertical alignment
plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')
plt.xlim(-.5, n)
plt.xticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())
plt.show()
例8:梯度
import matplotlib.pyplot as plt
import numpy as np
def f(x,y):
# the height function
return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)
n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
X,Y = np.meshgrid(x, y)
# use plt.contourf to filling contours
# X, Y and value for (X,Y) point
plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot)
# use plt.contour to add contour lines
C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
# adding label
plt.clabel(C, inline=True, fontsize=10)
plt.xticks(())
plt.yticks(())
plt.show()
例9:图片
import matplotlib.pyplot as plt
import numpy as np
# image data
a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
0.365348418405, 0.439599930621, 0.525083754405,
0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')
plt.colorbar(shrink=0.92)
plt.xticks(())
plt.yticks(())
plt.show()
例10:三维图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
# X, Y value
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X ** 2 + Y ** 2)
# height value
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
# I think this is different from plt12_contours
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))
ax.set_zlim(-2, 2)
plt.show()
`参考链接
`
上一篇: 用ajax实现预览链接可以看到链接的内容
下一篇: Matplotlib基础教程