課程概要:
1、matplotlib庫的使用介紹
2瓤介、mpl_toolkits 庫的使用介紹
3谷醉、mpl_toolkits 庫的使用介紹(二)
1、matplotlib 庫的使用介紹
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,1000)
y = np.sin(x)
z = np.cos(x)
# 配置
plt.figure(figsize=(8,4)) # 框的大小
plt.plot(x,y,label='$sin(x)$')
plt.plot(x,z,'b--',label='$cos(x)$') # 藍(lán)色虛線
plt.xlabel("Time(s)")
plt.ylabel("")
plt.title("matplotlib")
plt.ylim(-1.2, 1.2)
plt.legend() # 顯示為一個樣式
plt.show()
fig = plt.gcf() # 獲得當(dāng)前圖標(biāo)的對象
ax = plt.gca() # 獲得子圖的對象
print fig
print ax
>>>
Figure(640x480)
Axes(0.125,0.1;0.775x0.8)
多軸繪圖
# 多軸繪圖
# subplot(numRows, numCols, plotNum)
import matplotlib.pyplot as plt
for idx, color in enumerate("rgbyck"):
plt.subplot(320+idx+1, axisbg=color)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
w = np.linspace(0.1, 1000, 1000)
p = np.abs(1/(1+0.1j*w))
plt.subplot(221)
plt.plot(w, p, linewidth=2) # 算術(shù)坐標(biāo)系
plt.ylim(0, 1.5)
plt.subplot(222)
plt.semilogx(w, p, linewidth=2) # x 對數(shù)坐標(biāo)系
plt.ylim(0, 1.5)
plt.subplot(223)
plt.semilogy(w, p, linewidth=2) # y 對數(shù)坐標(biāo)系
plt.ylim(0, 1.5)
plt.subplot(224)
plt.loglog(w, p, linewidth=2) # 對數(shù)坐標(biāo)系
plt.ylim(0, 1.5)
plt.show()
# 1.txt的文本
>>> data
array([[ 0., 10., 20., 30., 40.],
[ 10., 23., 33., 43., 53.],
[ 20., 83., 23., 55., 33.],
[ 30., 93., 44., 22., 55.],
[ 40., 72., 33., 44., 66.]])
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt(r"D:\1.txt")
width = (data[1, 0] - data[0, 0])*0.4 # 柱形的寬帶為4
# data[:, 0]:所有行數(shù)據(jù)
# data[:, 1]:所有列數(shù)據(jù)
plt.figure()
plt.bar(data[:, 0] - width, data[:, 1], width, label='person')
plt.xlim(-width, 40) # x 的取值區(qū)間
plt.xlabel("Age")
plt.ylabel("Num")
plt.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.random.rand(100)
y = np.random.rand(100)
# marker:繪制點(diǎn)的形狀和大小宗苍,5代表五邊形,1是大小
# s的默認(rèn)值是20
plt.scatter(x, y, s=x*1000, c=y, marker=(5,1), lw=2, facecolor='none')
# marker:繪制點(diǎn)的形狀和大小 lw:linewidth facecolor:顏色
plt.xlim(0, 1) # c:cmap不是color薄榛,cmap繪制出來是彩圖(不同顏色)
plt.ylim(0, 1)
plt.show()
2讳窟、mpl_toolkits 庫使用介紹(一)
3D圖
# 3D圖
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d') # projection='3d' 已經(jīng)隱性調(diào)用了Axes3D
# ax = Axes3D(fig) # 可以加上也可以不加
th = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z ** 2 + 1
x = r * np.sin(th)
y = r * np.cos(th)
ax.plot(x, y, z, label='hello') # 立體的
ax.legend()
plt.show()
ax.plot(x, y, label='hello') # 平面的
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def randrange(n, vmin, vmax):
return (vmax-vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
輪廓圖
# 輪廓圖
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05) # 生成一系列的測試數(shù)據(jù)
cset = ax.contour(X, Y, Z,cmap=cm.coolwarm) # contour:生成輪廓圖
ax.clabel(cset, fontsize=9, inline=1)
plt.show()
# 修改
cset = ax.contour(X, Y, Z,extend3d=True, cmap=cm.coolwarm) # 顏色的填充
3、mpl_toolkits 庫使用介紹(二)
3D柱形圖
# 3D柱形圖
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], (30, 20, 10, 0)):
x = np.arange(20)
y = np.random.rand(20)
cs = [c] * len(x)
cs[0] = 'c'
ax.bar(x, y, zs=z, zdir='y', color = cs)
ax.set_xlabel('x')
plt.show()
繪制曲面
# 繪制曲面
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
繪制文字
# 繪制文字
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
xs = (1, 4, 4, 9, 4, 1)
ys = (2, 5, 8, 10, 1, 2)
zs = (10,3, 8, 9, 1, 8)
for zdir, x, y, z in zip(zdirs, xs, ys, zs):
label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
ax.text(x, y, z, label, zdir)
ax.text(9, 0, 0, 'red', color='red')
ax.text2D(0.05, 0.95, "20 text", transform=ax.transAxes)
ax.set_xlim3d(0,10)
ax.set_ylim3d(0,10)
ax.set_zlim3d(0,10)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()