Python繪制正余弦函數(shù)圖像的方法
今天打算通過繪制正弦和余弦函數(shù),從默認(rèn)的設(shè)置開始,一步一步地調(diào)整改進(jìn),讓它變得好看,變成我們初高中學(xué)習(xí)過的圖象那樣。通過這個(gè)過程來學(xué)習(xí)如何進(jìn)行對(duì)圖表的一些元素的進(jìn)行調(diào)整。
01. 簡(jiǎn)單繪圖
matplotlib有一套允許定制各種屬性的默認(rèn)設(shè)置。你可以幾乎控制matplotlib中的每一個(gè)默認(rèn)屬性:圖像大小,每英寸點(diǎn)數(shù),線寬,色彩和樣式,子圖(axes),坐標(biāo)軸和網(wǎng)格屬性,文字和字體屬性,等等。
安裝
pip install matplotlib
雖然matplotlib的默認(rèn)設(shè)置在大多數(shù)情況下相當(dāng)好,你卻可能想要在一些特別的情形下更改一些屬性。
from pylab import * x = np.linspace(-np.pi, np.pi, 256,endpoint=True) C,S = np.cos(x), np.sin(x) plot(x,C) plot(x,S) show()
show image
02. 設(shè)置基本元素
這邊的基本元素主要有幾下幾點(diǎn):
線的顏色,粗細(xì),和線型 刻度和標(biāo)簽 還有圖例
代碼比較簡(jiǎn)單,基本上在我的第一講內(nèi)容里都講過了。
import numpy as np from matplotlib import pyplot as plt plt.figure(figsize=(10,6), dpi=80) x = np.linspace(-np.pi, np.pi, 256,endpoint=True) C,S = np.cos(x), np.sin(x) # 設(shè)置線的顏色,粗細(xì),和線型 plt.plot(x, C, color="blue", linewidth=2.5, linestyle="-", label=r'$sin(x)$') plt.plot(x, S, color="red", linewidth=2.5, linestyle="-", label=r'$cos(x)$') # 如果覺得線條離邊界太近了,可以加大距離 plt.xlim(x.min()*1.2, x.max()*1.2) plt.ylim(C.min()*1.2, C.max()*1.2) # 當(dāng)前的刻度并不清晰,需要重新設(shè)定,并加上更直觀的標(biāo)簽 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([-1,0,1], [r'$-1$', r'$0$', r'$1$']) # 添加圖例 plt.legend() plt.show()
show image
03. 移動(dòng)軸線
還記得我們?cè)诔醺咧袑W(xué)習(xí)的三角函數(shù)圖象,可不是這樣,它應(yīng)該是有四個(gè)象限的。而這里卻是一個(gè)四四方方的圖表。
所以接下來,我們要做的就是移動(dòng)軸線,讓它變成我們熟悉的樣子。
我們只需要兩軸線(x和y軸),所以我們需要將頂部和右邊的軸線給隱藏起來(顏色設(shè)置為None即可)。
# plt.gca(),全稱是get current axis ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') # 由于我們移動(dòng)的是左邊和底部的軸,所以不用設(shè)置這兩個(gè)也可以 ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # 指定data類型,就是移動(dòng)到指定數(shù)值 ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0))
關(guān)于 set_position()
這個(gè)函數(shù)中的data是啥意思?我查了下官網(wǎng)。解釋如下
然后最后發(fā)現(xiàn),上面的寫法可以用一定更簡(jiǎn)潔的方式設(shè)置,是等價(jià)的。
ax.spines['bottom'].set_position('zero') ax.spines['left'].set_position('zero')
show image
04. 添加注釋
現(xiàn)在的圖形部分已經(jīng)成型,接下讓我們現(xiàn)在使用annotate命令注解一些我們感興趣的點(diǎn)。
我們選擇 2π/3
作為我們想要注解的正弦和余弦值。我們將在曲線上做一個(gè)標(biāo)記和一個(gè)垂直的虛線。然后,使用annotate命令來顯示一個(gè)箭頭和一些文本。
t = 2*np.pi/3 # 利用plt.plot繪制向下的一條垂直的線,利用plt.scatter繪制一個(gè)點(diǎn)。 plt.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=2.5, linestyle="--") plt.scatter([t,],[np.cos(t),], 50, color ='blue') plt.annotate(r'$sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$', xy=(t, np.sin(t)), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) # 利用plt.plot繪制向上的一條垂直的線,利用plt.scatter繪制一個(gè)點(diǎn)。 plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, linestyle="--") plt.scatter([t,],[np.sin(t),], 50, color ='red') plt.annotate(r'$cos(\frac{2\pi}{3})=-\frac{1}{2}$', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
在這里,你可能會(huì)對(duì) plt.annotate
這個(gè)函數(shù)的用法,有所陌生。這里也解釋一下。
第一個(gè)參數(shù),就是注釋內(nèi)容; 第二個(gè)參數(shù), xy
,就是對(duì)哪一點(diǎn)進(jìn)行注釋; 第三個(gè)參數(shù), xycoords
,指定類型,data 是說基于數(shù)值來定位; 第四個(gè)參數(shù), xytext
,是注釋的位置,結(jié)合第五個(gè)參數(shù),就是根據(jù)偏移量來決定注釋位置; 第五個(gè)參數(shù), textcoords
,值為offset points,就是說是相對(duì)位置; 第六個(gè)參數(shù), fontsize
,注釋大??; 第七個(gè)參數(shù), arrowprops
,對(duì)箭頭的類型的一些設(shè)置。
show image
05. 完整代碼
以上都是對(duì)片段代碼進(jìn)行解釋,這里放出完整的代碼
import numpy as np from matplotlib import pyplot as plt plt.figure(figsize=(10,6), dpi=80) x = np.linspace(-np.pi, np.pi, 256,endpoint=True) C,S = np.cos(x), np.sin(x) # 設(shè)置線的顏色,粗細(xì),和線型 plt.plot(x, C, color="blue", linewidth=2.5, linestyle="-", label=r'$sin(x)$') plt.plot(x, S, color="red", linewidth=2.5, linestyle="-", label=r'$cos(x)$') # 如果覺得線條離邊界太近了,可以加大距離 plt.xlim(x.min()*1.2, x.max()*1.2) plt.ylim(C.min()*1.2, C.max()*1.2) # 當(dāng)前的刻度并不清晰,需要重新設(shè)定,并加上更直觀的標(biāo)簽 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([-1,1], [r'$-1$', r'$1$']) # 添加圖例 plt.legend(loc='upper left') # plt.gca(),全稱是get current axis ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') # 由于我們移動(dòng)的是左邊和底部的軸,所以不用設(shè)置這兩個(gè)也可以 ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # 指定data類型,就是移動(dòng)到指定數(shù)值 # ax.spines['bottom'].set_position('zero') ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0)) t = 2*np.pi/3 # 利用plt.plot繪制向下的一條垂直的線,利用plt.scatter繪制一個(gè)點(diǎn)。 plt.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=2.5, linestyle="--") plt.scatter([t,],[np.cos(t),], 50, color ='blue') plt.annotate(r'$sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$', xy=(t, np.sin(t)), xycoords='data', xytext=(+10, +30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) # 利用plt.plot繪制向上的一條垂直的線,利用plt.scatter繪制一個(gè)點(diǎn)。 plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, linestyle="--") plt.scatter([t,],[np.sin(t),], 50, color ='red') plt.annotate(r'$cos(\frac{2\pi}{3})=-\frac{1}{2}$', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) plt.show()
繪制拋物線:
X1=np.linspace(-4,4,100,endpoint=True) plt.plot(X1,(X1**2)/9)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用sqlite3第三方庫讀寫SQLite數(shù)據(jù)庫的方法步驟
數(shù)據(jù)庫非常重要,程序的數(shù)據(jù)增刪改查需要數(shù)據(jù)庫支持,python處理數(shù)據(jù)庫非常簡(jiǎn)單,而且不同類型的數(shù)據(jù)庫處理邏輯方式大同小異,下面這篇文章主要給大家介紹了關(guān)于Python使用sqlite3第三方庫讀寫SQLite數(shù)據(jù)庫的方法步驟,需要的朋友可以參考下2022-07-07利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼
這篇文章主要介紹了利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11TensorFlow2.X結(jié)合OpenCV 實(shí)現(xiàn)手勢(shì)識(shí)別功能
這篇文章主要介紹了TensorFlow2.X結(jié)合OpenCV 實(shí)現(xiàn)手勢(shì)識(shí)別功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Python BeautifulSoup [解決方法] TypeError: list indices must be
這篇文章主要介紹了Python BeautifulSoup [解決方法] TypeError: list indices must be integers or slices, not str,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08django傳值給模板, 再用JS接收并進(jìn)行操作的實(shí)例
今天小編就為大家分享一篇django傳值給模板, 再用JS接收并進(jìn)行操作的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05