使用numpy与matplotlib.pyplot画图

使用numpy与matplotlib.pyplot画图

1 折线图

# -*- enccoding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'SimHei' #用来正常显示中文
plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
fig = plt.figure()
ph = fig.add_subplot(1,1,1)
x = range(100)
y = np.cos(x) * np.sin(x)**2 * 200
ph.plot(x,y,'y-')
ph.set_xticks([0,25,50,75,100])
ph.set_title("黄灿华的声音频率")
ph.set_xlabel('Time')
ph.set_ylabel('Hz')
plt.show()

 

2 等高线

 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()
复制代码
posted @ 2019-04-17 22:07  爱你一生12581  阅读(568)  评论(0编辑  收藏  举报