-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
83 lines (65 loc) · 1.96 KB
/
plot.py
File metadata and controls
83 lines (65 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
# 画三维图
x, y = np.mgrid[-2:2:100j, -2:2:100j]
z = x * np.exp( -x**2 - y**2 )
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap = 'rainbow')
plt.title("z=x*exp(-x^2-y^2)")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.ylim(-2.0,2.0)
plt.legend()
plt.show()
# 画一维图
x = np.linspace(0, 10, 1000)
y = np.sin(x)
z = np.cos(x**2)
plt.figure(figsize=(8,4))
plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2)
plt.plot(x,z,"b--",label="$cos(x^2)$")
plt.xlabel("Time(s)")
plt.ylabel("Volt")
plt.title("PyPlot First Example")
plt.ylim(-1.2,1.2)
plt.legend()
plt.show()
# -------------------------------------------------------
# 兼容MATLAB代码风格API
# 1. 绘制曲线图
from matplotlib import pylab
import numpy as np
x = np.linspace(0,10,20)
y = x * x + 2
pylab.plot( x, y, 'r' ) # 'r'代表red
plt.show()
# 2. 使用subplot
pylab.subplot(1,2,1) # 括号中的内容代表(行,列,索引)
pylab.plot( x, y, 'r--' ) # ''中的内容确定颜色和线型
pylab.subplot(1,2,2)
pylab.plot( y, x, 'g*-' )
plt.show()
# -------------------------------------------------------
# Matplotlib面向对象API(推荐这样画图)
# """使用matplotlib提供的面向对象API,需要导入pyplot模块,并约定为plt
from matplotlib import pyplot as plt
# 使用plt.subplots()添加画布
fig, axes = plt.subplots()
axes.plot( x, y, 'b--' )
plt.show()
# 调整画布尺寸和显示精度
fig, axes = plt.subplots( figsize=(16,9), dpi=50 ) # figsize调整尺寸,dpi调整精度
axes.plot( x, y, 'g-*' )
plt.show()
# 2.1 图名称、坐标轴名称、图例
fig, axes = plt.subplots()
axes.plot(x, x**2, 'r--')
axes.plot(x, x**3, 'b-*')
ax.set_title( "title" ) # 设置图标题
ax.set_xlabel("x lable") # 设置坐标轴名称
ax.set_ylabel("y lable")
axes.legend( ["y = x^2", "y = x^3"], loc = 2 ) # 设置图例
plt.show()