matplotlib繪製正弦和余弦曲線

2021-08-11 03:53:04 字數 3172 閱讀 3129

一 介紹

官網:

二 **

import

numpy

asnp

import

matplotlib

.pyplot

asplt

#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

inax

.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

=1plt

.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

()

三 執行結果

大小: 64.8 kb

Lua繪製正弦曲線

夜半突來興致,看了下wxlua的圖形處理部分,感覺挺好玩,就試著寫了一段畫正弦曲線的 為了實現動畫效果特意新增了延遲效果,感覺延遲的效率好低,畫的時候視窗拖都拖不動。效果圖 部分 點和線實現正弦曲線 wxlua支援 aurhor chuwp date 2010.10.30 require wx lo...

matplotlib繪製等直線

利用contour contourf 描繪等值線 contourf 帶有填充效果 def fig2 y,x np.ogrid 2 2 200j,3 3 300j z x np.exp x 2 y 2 extent np.min x np.max x np.min y np.max y plt.fig...

Python使用matplotlib繪製三維曲線

本文主要演示如何使用matplotlib繪製三維圖形 如下 coding utf 8 import matplotlib as mpl from mpl toolkits.mplot3d import axes3d import numpy as np import matplotlib.pyplo...