溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python繪制正余弦函數(shù)圖像的方法

發(fā)布時(shí)間:2020-09-10 14:21:32 來(lái)源:腳本之家 閱讀:285 作者:Python編程時(shí)光 欄目:開(kāi)發(fā)技術(shù)

今天打算通過(guò)繪制正弦和余弦函數(shù),從默認(rèn)的設(shè)置開(kāi)始,一步一步地調(diào)整改進(jìn),讓它變得好看,變成我們初高中學(xué)習(xí)過(guò)的圖象那樣。通過(guò)這個(gè)過(guò)程來(lái)學(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

Python繪制正余弦函數(shù)圖像的方法

02. 設(shè)置基本元素

這邊的基本元素主要有幾下幾點(diǎn):

線的顏色,粗細(xì),和線型 刻度和標(biāo)簽 還有圖例

代碼比較簡(jiǎn)單,基本上在我的第一講內(nèi)容里都講過(guò)了。

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, line, label=r'$sin(x)$')
plt.plot(x, S, color="red", linewidth=2.5, line, label=r'$cos(x)$')

# 如果覺(jué)得線條離邊界太近了,可以加大距離
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

Python繪制正余弦函數(shù)圖像的方法

03. 移動(dòng)軸線

還記得我們?cè)诔醺咧袑W(xué)習(xí)的三角函數(shù)圖象,可不是這樣,它應(yīng)該是有四個(gè)象限的。而這里卻是一個(gè)四四方方的圖表。

所以接下來(lái),我們要做的就是移動(dòng)軸線,讓它變成我們熟悉的樣子。

我們只需要兩軸線(x和y軸),所以我們需要將頂部和右邊的軸線給隱藏起來(lái)(顏色設(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類(lèi)型,就是移動(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)。解釋如下

Python繪制正余弦函數(shù)圖像的方法

然后最后發(fā)現(xiàn),上面的寫(xiě)法可以用一定更簡(jiǎn)潔的方式設(shè)置,是等價(jià)的。

ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')

show image

Python繪制正余弦函數(shù)圖像的方法

04. 添加注釋

現(xiàn)在的圖形部分已經(jīng)成型,接下讓我們現(xiàn)在使用annotate命令注解一些我們感興趣的點(diǎn)。

我們選擇 2π/3 作為我們想要注解的正弦和余弦值。我們將在曲線上做一個(gè)標(biāo)記和一個(gè)垂直的虛線。然后,使用annotate命令來(lái)顯示一個(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, line)
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(arrow, connection))

# 利用plt.plot繪制向上的一條垂直的線,利用plt.scatter繪制一個(gè)點(diǎn)。
plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, line)
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(arrow, connection))

在這里,你可能會(huì)對(duì) plt.annotate 這個(gè)函數(shù)的用法,有所陌生。這里也解釋一下。

第一個(gè)參數(shù),就是注釋內(nèi)容; 第二個(gè)參數(shù), xy ,就是對(duì)哪一點(diǎn)進(jìn)行注釋?zhuān)?第三個(gè)參數(shù), xycoords ,指定類(lèi)型,data 是說(shuō)基于數(shù)值來(lái)定位; 第四個(gè)參數(shù), xytext ,是注釋的位置,結(jié)合第五個(gè)參數(shù),就是根據(jù)偏移量來(lái)決定注釋位置; 第五個(gè)參數(shù), textcoords ,值為offset points,就是說(shuō)是相對(duì)位置; 第六個(gè)參數(shù), fontsize ,注釋大??; 第七個(gè)參數(shù), arrowprops ,對(duì)箭頭的類(lèi)型的一些設(shè)置。

show image

Python繪制正余弦函數(shù)圖像的方法

05. 完整代碼

以上都是對(duì)片段代碼進(jìn)行解釋?zhuā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, line, label=r'$sin(x)$')
plt.plot(x, S, color="red", linewidth=2.5, line, label=r'$cos(x)$')

# 如果覺(jué)得線條離邊界太近了,可以加大距離
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類(lèi)型,就是移動(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, line)
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(arrow, connection))

# 利用plt.plot繪制向上的一條垂直的線,利用plt.scatter繪制一個(gè)點(diǎn)。
plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=2.5, line)
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(arrow, connection))

plt.show()

繪制拋物線:

X1=np.linspace(-4,4,100,endpoint=True)
plt.plot(X1,(X1**2)/9)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI