作者:桂。
时间:2017-04-27 23:24:55
链接:http://www.cnblogs.com/xingshansi/p/6777945.html
本文仅仅梳理最基本的绘图方法。
一、初始化
假设已经安装了matplotlib工具包。
利用matplotlib.figure.Figure创建一个图框:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
二、直线绘制(Line plots)
基本用法:
ax.plot(x,y,z,label=' ')
code:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.show()
三、散点绘制(Scatter plots)
基本用法:
ax.scatter(xs, ys, zs, s=20, c=None, depthshade=True, *args, *kwargs)
- xs,ys,zs:输入数据;
- s:scatter点的尺寸
- c:颜色,如c = 'r'就是红色;
- depthshase:透明化,True为透明,默认为True,False为不透明
- *args等为扩展变量,如maker = 'o',则scatter结果为’o‘的形状
code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def randrange(n, vmin, vmax):
'''
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
'''
return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n