本文实例讲述了Python使用matplotlib绘制正弦和余弦曲线的方法。分享给大家供大家参考,具体如下:
一 介绍
关键词:绘图库
二 代码
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
|
import numpy as np
import matplotlib.pyplot as plt
#line
x = np.linspace( - np.pi,np.pi, 256 ,endpoint = True )
#定义余弦函数正弦函数
c,s = np.cos(x),np.sin(x)
plt.figure( 1 )
#画图,以x为横坐标,以c为纵坐标
plt.plot(x,c,color = "blue" ,linestyle = "-" ,label = "COS" ,alpha = 0.5 )
plt.plot(x,s, "r*" ,label = "SIN" )
#增加标题
plt.title( "COS & SIN" )
ax = plt.gca()
ax.spines[ "right" ].set_color( "none" )
ax.spines[ "top" ].set_color( "none" )
ax.spines[ "left" ].set_position(( "data" , 0 ))
ax.spines[ "bottom" ].set_position(( "data" , 0 ))
ax.xaxis.set_ticks_position( "bottom" )
ax.yaxis.set_ticks_position( "left" )
plt.xticks([ - np.pi, - np.pi / 2 , 0 ,np.pi / 2 ,np.pi],
[r '$-\pi$' ,r '$-\pi/2$' ,r '$0$' ,r '$+\pi/2$' ,r '$+\pi$' ])
plt.yticks(np.linspace( - 1 , 1 , 5 ,endpoint = True ))
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize( 16 )
label.set_bbox( dict (facecolor = "white" ,edgecolor = "None" ,alpha = 0.2 ))
#图例显示
plt.legend(loc = "upper left" )
#显示网格
plt.grid()
#显示范围
#plt.axis([-1,1,-0.5,1])
plt.fill_between(x,np. abs (x)< 0.5 ,c,c> 0.5 ,color = "green" ,alpha = 0.25 )
t = 1
plt.plot([t,t],[ 0 ,np.cos(t)], "y" ,linewidth = 3 ,linestyle = "--" )
plt.annotate( "cos(1)" ,xy = (t,np.cos( 1 )),xycoords = "data" ,xytext = ( + 10 , + 30 ),
textcoords = "offset points" ,arrowprops = dict (arrowstyle = "->" ,connectionstyle = "arc3,rad=.2" ))
#显示图形
plt.show()
|
三 运行结果
希望本文所述对大家Python程序设计有所帮助。
原文链接:http://blog.csdn.net/chengqiuming/article/details/78601369