最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python?NumPy庫(kù)及Matplotlib庫(kù)繪制數(shù)學(xué)函數(shù)圖像

 更新時(shí)間:2022年04月14日 14:42:34   作者:manchan4869  
最近開(kāi)始學(xué)習(xí)數(shù)學(xué)了,有一些題目的函數(shù)圖像非常有特點(diǎn),下面這篇文章主要給大家介紹了關(guān)于利用Python?NumPy庫(kù)及Matplotlib庫(kù)繪制數(shù)學(xué)函數(shù)圖像的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

最近開(kāi)始學(xué)習(xí)數(shù)學(xué)了,有一些題目的函數(shù)圖像非常有特點(diǎn),有一些函數(shù)圖像手繪比較麻煩,那么有沒(méi)有什么辦法做出又標(biāo)準(zhǔn)又好看的數(shù)學(xué)函數(shù)圖像呢?

答案是有很多的,有很多不錯(cuò)的軟件都能畫(huà)出函數(shù)圖像,但是,我想到了Python的數(shù)據(jù)可視化。Python在近些年非常火熱,在數(shù)據(jù)分析以及深度學(xué)習(xí)等方面得到廣泛地運(yùn)用,其豐富的庫(kù)使其功能愈加強(qiáng)大。

這里我們使用Python的NumPy庫(kù)以及Matplotlib庫(kù)進(jìn)行繪圖。

NumPy與Matplotlib

NumPy(Numerical Python) 是 Python 語(yǔ)言的一個(gè)擴(kuò)展程序庫(kù),支持大量的維度數(shù)組與矩陣運(yùn)算,此外也針對(duì)數(shù)組運(yùn)算提供大量的數(shù)學(xué)函數(shù)庫(kù)。

Matplotlib 是 Python 的繪圖庫(kù)。 它可與 NumPy 一起使用,提供了一種有效的 MatLab 開(kāi)源替代方案。

函數(shù)繪圖

所需庫(kù)函數(shù)語(yǔ)法

import 語(yǔ)句

想使用 Python 源文件,只需在另一個(gè)源文件里執(zhí)行 import 語(yǔ)句,語(yǔ)法如下:

import module1[, module2[,... moduleN]

from … import 語(yǔ)句

Python 的 from 語(yǔ)句讓你從模塊中導(dǎo)入一個(gè)指定的部分到當(dāng)前命名空間中,語(yǔ)法如下:

from modname import name1[, name2[, ... nameN]]

numpy.arange

numpy 包中的使用 arange 函數(shù)創(chuàng)建數(shù)值范圍并返回 ndarray 對(duì)象,函數(shù)格式如下:

numpy.arange(start, stop, step, dtype)

根據(jù) start 與 stop 指定的范圍以及 step 設(shè)定的步長(zhǎng),生成一個(gè) ndarray。

參數(shù)說(shuō)明:

參數(shù)描述
start起始值,默認(rèn)為0
stop終止值(不包含)
step步長(zhǎng),默認(rèn)為1
dtype返回ndarray的數(shù)據(jù)類型,如果沒(méi)有提供,則會(huì)使用輸入數(shù)據(jù)的類型。

numpy.linspace

numpy.linspace 函數(shù)用于創(chuàng)建一個(gè)一維數(shù)組,數(shù)組是一個(gè)等差數(shù)列構(gòu)成的,格式如下:

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

參數(shù)說(shuō)明:

參數(shù)描述
start序列的起始值
stop序列的終止值,如果endpoint為true,該值包含于數(shù)列中
num要生成的等步長(zhǎng)的樣本數(shù)量,默認(rèn)為50
endpoint該值為 true 時(shí),數(shù)列中包含stop值,反之不包含,默認(rèn)是True。
retstep如果為 True 時(shí),生成的數(shù)組中會(huì)顯示間距,反之不顯示。
dtypendarray 的數(shù)據(jù)類型

導(dǎo)入所需模塊

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #用來(lái)正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus'] = False #用來(lái)正常顯示負(fù)號(hào)

一元一次函數(shù)

# 一元一次函數(shù)圖像
x = np.arange(-10, 10, 0.1)#生成等差數(shù)組
y = 2 * x
plt.xlabel('x')
plt.ylabel('y')
plt.title("一元一次函數(shù)")
plt.plot(x, y)
plt.show()

一元二次函數(shù)

# 一元二次函數(shù)圖像
x = np.arange(-10, 10, 0.1)
y = x * x
plt.xlabel('x')
plt.ylabel('y')
plt.title("一元二次函數(shù)")
plt.plot(x, y)
plt.show()

指數(shù)函數(shù)

# 指數(shù)函數(shù)
x = np.arange(-10, 10, 0.1)
y = np.power(2, x)
plt.xlabel('x')
plt.ylabel('y')
plt.title("指數(shù)函數(shù)")
plt.plot(x, y)
plt.show()

正弦函數(shù)

x = np.arange(-3 * np.pi, 3 * np.pi, 0.1)
y = np.sin(x)
plt.xlabel('x')
plt.ylabel('y')
plt.title("正弦函數(shù)")
plt.plot(x, y)
plt.show()

余弦函數(shù)

x = np.arange(-3 * np.pi, 3 * np.pi, 0.1)
y = np.cos(x)
plt.xlabel('x')
plt.ylabel('y')
plt.title("余弦函數(shù)")
plt.plot(x, y)
plt.show()

高級(jí)玩法

from pylab import *
import numpy
figure(figsize=(12,8), dpi=72)

# 創(chuàng)建一個(gè)新的 1 * 1 的子圖,接下來(lái)的圖樣繪制在其中的第 1 塊(也是唯一的一塊)
subplot(1,1,1)

X = np.linspace(-np.pi*2, np.pi*2, 2048,endpoint=True)
C,S = np.cos(X), np.sin(X)

# 繪制余弦曲線,使用藍(lán)色的、連續(xù)的、寬度為 1 (像素)的線條
plot(X, C,linewidth=1.5, linestyle="-",label="正弦")

# 繪制正弦曲線,使用綠色的、連續(xù)的、寬度為 1 (像素)的線條
plot(X, S,linewidth=1.5, linestyle="-",label="余弦")
legend(loc='upper left')
# 設(shè)置橫軸的上下限
xlim(-4.5,4.5)

# 設(shè)置橫軸記號(hào)
xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
       [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

yticks([-1, 0, +1],
       [r'$-1$', r'$0$', r'$+1$'])

# 設(shè)置縱軸的上下限
ylim(-1.5,1.5)

# 設(shè)置縱軸記號(hào)
yticks(np.linspace(-1,1,5,endpoint=True))
ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

# savefig("sincosin.png",dpi=72) #以72dpi保存圖像

# 在屏幕上顯示
show()

fig = plt.figure(figsize=(12,8), dpi=72)
x = np.arange(-10, 10, 0.01)
arsinh = np.log(x+np.sqrt(x**2+1))
sinh=0.5*(e**x-e**(-x))
cosh=0.5*(e**x+e**(-x))

plt.plot(x, sinh,label="雙曲正弦")
plt.plot(x, arsinh,label="反雙曲正弦")
plt.plot(x, cosh,label="雙曲余弦")

plt.legend(loc='upper left')

ylim(-10,10)

ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

plt.show()

總結(jié) 

到此這篇關(guān)于利用Python NumPy庫(kù)及Matplotlib庫(kù)繪制數(shù)學(xué)函數(shù)圖像的文章就介紹到這了,更多相關(guān)Python繪制數(shù)學(xué)函數(shù)圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

应城市| 靖宇县| 三门峡市| 横峰县| SHOW| 洛宁县| 黄梅县| 镇雄县| 双柏县| 洪湖市| 大石桥市| 黔东| 固安县| 绥阳县| 潢川县| 东山县| 沈阳市| 米易县| 阳城县| 余庆县| 沛县| 洱源县| 兰考县| 新巴尔虎右旗| 樟树市| 伊通| 明星| 亚东县| 张北县| 大方县| 亚东县| 合阳县| 彭阳县| 淮滨县| 确山县| 枝江市| 新田县| 威海市| 陆丰市| 龙山县| 怀集县|