Python?圖形繪制詳細代碼(一)
1、畫第一個圖形
第一個圖形從簡單的開始。
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ù)在畫布上繪制它們。
- 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)在同一張圖上繪制兩條線。 通過給它們一個名稱(label)來區(qū)分它們,該名稱作為 .plot() 函數(shù)的參數(shù)傳遞。
- 2)提供有關(guān)線條類型及其顏色信息的小矩形框稱為圖例。 可以使用 .legend() 函數(shù)為繪圖添加圖例。
3、自定義繪圖
下面將討論適用于幾乎所有場景的一些基本自定義。
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 代碼的部分解釋
如上面代碼所示,我們進行了一些自定義的改變:
- 1)設(shè)定線的寬度、樣式以及顏色。
- 2)設(shè)定了標(biāo)記的形狀、顏色和尺寸。
- 3)覆蓋 x 和 y 軸范圍。如果未完成覆蓋,
pyplot
模塊使用自動縮放功能來設(shè)置軸范圍和比例。
到此這篇關(guān)于Python 圖形繪制詳細代碼的文章就介紹到這了,更多相關(guān)Python 圖形繪制詳細內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用time、datetime返回工作日列表實例代碼
這篇文章主要介紹了python使用time、datetime返回工作日列表,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05在Pytorch中自定義dataset讀取數(shù)據(jù)的實現(xiàn)代碼
這篇文章給大家介紹了如何在Pytorch中自定義dataset讀取數(shù)據(jù),文中給出了詳細的圖文介紹和代碼講解,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12python爬取Ajax動態(tài)加載網(wǎng)頁過程解析
這篇文章主要介紹了python爬取Ajax動態(tài)加載網(wǎng)頁過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09解決Pytorch中Batch Normalization layer踩過的坑
這篇文章主要介紹了解決Pytorch中Batch Normalization layer踩過的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05Python使用PyQt5/PySide2編寫一個極簡的音樂播放器功能
這篇文章主要介紹了Python中使用PyQt5/PySide2編寫一個極簡的音樂播放器功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02