Python?圖形繪制詳細(xì)代碼(一)
1、畫(huà)第一個(gè)圖形
第一個(gè)圖形從簡(jiǎn)單的開(kāi)始。
1.1 代碼
# importing the required module import matplotlib.pyplot as plt # x axis values x = [1,2,3] # corresponding y axis values y = [2,4,1] # plotting the points plt.plot(x, y) # naming the x axis plt.xlabel('x - axis') # naming the y axis plt.ylabel('y - axis') # giving a title to my graph plt.title('My first graph!') # function to show the plot plt.show()
1.2 輸出
1.3 代碼的部分解釋
- 1)將 x 軸和相應(yīng)的 y 軸值定義為列表。
- 2)使用 .plot() 函數(shù)在畫(huà)布上繪制它們。
- 3)使用 .xlabel() 和 .ylabel() 函數(shù)為 x 軸和 y 軸命名。
- 4)使用 .title() 函數(shù)為繪圖命名。
- 5)使用 .show() 函數(shù)查看繪圖。
- 2、在同一圖上繪制兩條或多條線
2、在同一圖上繪制兩條或多條線
如果想在同一張圖上再繪制多條線,可反復(fù)使用.plot()
函數(shù)。
2.1 代碼
import matplotlib.pyplot as plt # line 1 points x1 = [1,2,3] y1 = [2,4,1] # plotting the line 1 points plt.plot(x1, y1, label = "line 1") # line 2 points x2 = [1,2,3] y2 = [4,1,3] # plotting the line 2 points plt.plot(x2, y2, label = "line 2") # naming the x axis plt.xlabel('x - axis') # naming the y axis plt.ylabel('y - axis') # giving a title to my graph plt.title('Two lines on same graph!') # show a legend on the plot plt.legend() # function to show the plot plt.show()
2.2 輸出
2.3 代碼的部分解釋
- 1)在同一張圖上繪制兩條線。 通過(guò)給它們一個(gè)名稱(chēng)(label)來(lái)區(qū)分它們,該名稱(chēng)作為 .plot() 函數(shù)的參數(shù)傳遞。
- 2)提供有關(guān)線條類(lèi)型及其顏色信息的小矩形框稱(chēng)為圖例。 可以使用 .legend() 函數(shù)為繪圖添加圖例。
3、自定義繪圖
下面將討論適用于幾乎所有場(chǎng)景的一些基本自定義。
3.1 代碼
import matplotlib.pyplot as plt # x axis values x = [1,2,3,4,5,6] # corresponding y axis values y = [2,4,1,5,2,6] # plotting the points plt.plot(x, y, color='green', linestyle='dashed', linewidth = 3,marker='o', markerfacecolor='blue', markersize=12) # setting x and y axis range plt.ylim(1,8) plt.xlim(1,8) # naming the x axis plt.xlabel('x - axis') # naming the y axis plt.ylabel('y - axis') # giving a title to my graph plt.title('Some cool customizations!') # function to show the plot plt.show()
3.2 輸出
3.3 代碼的部分解釋
如上面代碼所示,我們進(jìn)行了一些自定義的改變:
- 1)設(shè)定線的寬度、樣式以及顏色。
- 2)設(shè)定了標(biāo)記的形狀、顏色和尺寸。
- 3)覆蓋 x 和 y 軸范圍。如果未完成覆蓋,
pyplot
模塊使用自動(dòng)縮放功能來(lái)設(shè)置軸范圍和比例。
到此這篇關(guān)于Python 圖形繪制詳細(xì)代碼的文章就介紹到這了,更多相關(guān)Python 圖形繪制詳細(xì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用time、datetime返回工作日列表實(shí)例代碼
這篇文章主要介紹了python使用time、datetime返回工作日列表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05在Pytorch中自定義dataset讀取數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章給大家介紹了如何在Pytorch中自定義dataset讀取數(shù)據(jù),文中給出了詳細(xì)的圖文介紹和代碼講解,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12flask 實(shí)現(xiàn)token機(jī)制的示例代碼
這篇文章主要介紹了flask 實(shí)現(xiàn)token機(jī)制的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Python中OpenCV圖像特征和harris角點(diǎn)檢測(cè)
Harris角點(diǎn)檢測(cè)算子是于1988年由CHris Harris & Mike Stephens提出來(lái)的。在具體展開(kāi)之前,不得不提一下Moravec早在1981就提出來(lái)的Moravec角點(diǎn)檢測(cè)算子。本文重點(diǎn)給大家介紹OpenCV圖像特征harris角點(diǎn)檢測(cè)知識(shí),一起看看吧2021-09-09python爬取Ajax動(dòng)態(tài)加載網(wǎng)頁(yè)過(guò)程解析
這篇文章主要介紹了python爬取Ajax動(dòng)態(tài)加載網(wǎng)頁(yè)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09解決Pytorch中Batch Normalization layer踩過(guò)的坑
這篇文章主要介紹了解決Pytorch中Batch Normalization layer踩過(guò)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python使用PyQt5/PySide2編寫(xiě)一個(gè)極簡(jiǎn)的音樂(lè)播放器功能
這篇文章主要介紹了Python中使用PyQt5/PySide2編寫(xiě)一個(gè)極簡(jiǎn)的音樂(lè)播放器功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02