使用numpy与matplotlib.p

2020-01-16 16:23:00 浏览数 (1)

使用numpy与matplotlib.pyplot画图

1. 折线图

代码语言:javascript复制
 1 # -*- enccoding:utf-8 -*-
 2 import numpy as np
 3 import matplotlib.pyplot as plt
 4 plt.rcParams['font.family'] = 'SimHei'             #用来正常显示中文
 5 plt.rcParams['font.sans-serif'] = ['SimHei']     #用来正常显示中文
 6 plt.rcParams['axes.unicode_minus'] = False   #用来正常显示负号
 7 fig = plt.figure()
 8 ph = fig.add_subplot(1,1,1)
 9 x = range(100)
10 y = np.cos(x) * np.sin(x)**2 * 200
11 ph.plot(x,y,'y-')
12 ph.set_xticks([0,25,50,75,100])
13 ph.set_title("步平凡的声音频率")
14 ph.set_xlabel('Time')
15 ph.set_ylabel('Hz')
16 plt.show()

2. 等高线

代码语言:javascript复制
 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 def f(x,y):
 4     return (1-x/2 x**5 y**3)*np.exp(-x**2-y**2)*1000
 5 plt.rcParams['font.family'] = 'KaiTi'               #用来正常显示中文
 6 plt.rcParams['font.sans-serif'] = ['SimHei']    #用来正常显示中文
 7 plt.rcParams['axes.unicode_minus'] = False   #用来正常显示负号
 8 
 9 x = np.linspace(-3,3,256)
10 y = np.linspace(-3,3,256)
11 X,Y = np.meshgrid(x,y)
12 
13 plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap=plt.cm.hot)
14 C = plt.contour(X, Y, f(X,Y), 8, colors='black')
15 
16 #plt.clabel(C, inline=1, fontsize=10)
17 plt.title("步平凡火焰山")
18 plt.show()

0 人点赞