<kbd id="9plqc"><label id="9plqc"></label></kbd>

        <th id="9plqc"></th>
        1. <center id="9plqc"><video id="9plqc"></video></center>
          <sub id="9plqc"><form id="9plqc"><pre id="9plqc"></pre></form></sub>
          <nav id="9plqc"><form id="9plqc"><legend id="9plqc"></legend></form></nav>
          用Python繪制數(shù)學函數(shù)圖像 您所在的位置:網(wǎng)站首頁 屬鼠的住八樓怎么緩解 用Python繪制數(shù)學函數(shù)圖像

          用Python繪制數(shù)學函數(shù)圖像

          #用Python繪制數(shù)學函數(shù)圖像| 來源: 網(wǎng)絡(luò)整理| 查看: 265

          博客文章: https://blog.manchan/post/Drawing-mathematical-function-images-with-Python/可在此處找到我

          前言

          最近開始學習數(shù)學了,有一些題目的函數(shù)圖像非常有特點,有一些函數(shù)圖像手繪比較麻煩,那么有沒有什么辦法做出又標準又好看的數(shù)學函數(shù)圖像呢?

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

          這里我們使用Python的NumPy庫以及Matplotlib庫進行繪圖。

          NumPy與Matplotlib

          NumPy(Numerical Python) 是 Python 語言的一個擴展程序庫,支持大量的維度數(shù)組與矩陣運算,此外也針對數(shù)組運算提供大量的數(shù)學函數(shù)庫。

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

          函數(shù)繪圖 所需庫函數(shù)語法 import 語句

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

          import module1[, module2[,... moduleN] from … import 語句

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

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

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

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

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

          參數(shù)說明:

          參數(shù)描述start起始值,默認為0stop終止值(不包含)step步長,默認為1dtype返回ndarray的數(shù)據(jù)類型,如果沒有提供,則會使用輸入數(shù)據(jù)的類型。 numpy.linspace

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

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

          參數(shù)說明:

          參數(shù)描述start序列的起始值stop序列的終止值,如果endpoint為true,該值包含于數(shù)列中num要生成的等步長的樣本數(shù)量,默認為50endpoint該值為 true 時,數(shù)列中包含stop值,反之不包含,默認是True。retstep如果為 True 時,生成的數(shù)組中會顯示間距,反之不顯示。dtypendarray 的數(shù)據(jù)類型 導(dǎo)入所需模塊 import numpy as np from matplotlib import pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] #用來正常顯示中文標簽 plt.rcParams['axes.unicode_minus'] = False #用來正常顯示負號 一元一次函數(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()

          高級玩法 from pylab import * import numpy figure(figsize=(12,8), dpi=72) # 創(chuàng)建一個新的 1 * 1 的子圖,接下來的圖樣繪制在其中的第 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) # 繪制余弦曲線,使用藍色的、連續(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è)置橫軸記號 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è)置縱軸記號 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()



          【本文地址】

          公司簡介

          聯(lián)系我們

          今日新聞

          推薦新聞

          專題文章
            CopyRight 2018-2019 實驗室設(shè)備網(wǎng) 版權(quán)所有
            黄色免费网站在线看,韩国精品在线观看,韩国美女一区二区,99国产热 海门市| 广水市| 长岛县| 郯城县| 沾益县| 泽普县| 凤城市| 云和县| 桐柏县| 澄迈县| 玛多县| 二手房| 台南县| 石泉县| 东阿县| 福清市| 东宁县| 定州市| 铜川市| 湘潭县| 乌拉特后旗| 岚皋县| 潍坊市| 平远县| 敖汉旗| 汕尾市| 英吉沙县| 祁东县| 井研县| 阿拉善左旗| 新化县| 怀柔区| 二手房| 车致| 鄂托克前旗| 泸定县| 桂平市| 锦屏县| 蒙自县| 江达县| 西畴县| http://444 http://444 http://444 http://444 http://444 http://444