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 Django編寫(xiě)接口并用Jmeter測(cè)試的方法
這篇文章主要介紹了python Django編寫(xiě)接口并用Jmeter測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python正則表達(dá)式常用函數(shù)總結(jié)
這篇文章主要介紹了Python正則表達(dá)式常用函數(shù),結(jié)合實(shí)例形式總結(jié)分析了Python正則表達(dá)式常用函數(shù)功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06
使用Python判斷質(zhì)數(shù)(素?cái)?shù))的簡(jiǎn)單方法講解
這篇文章主要介紹了使用Python判斷質(zhì)數(shù)(素?cái)?shù))的簡(jiǎn)單方法講解,經(jīng)常被用來(lái)做科學(xué)計(jì)算的Python處理這種小問(wèn)題當(dāng)然手到擒來(lái)^_-需要的朋友可以參考下2016-05-05
python數(shù)據(jù)結(jié)構(gòu):數(shù)據(jù)類(lèi)型
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)中的數(shù)據(jù)類(lèi)型,在?Python?以及其他所有面向?qū)ο缶幊陶Z(yǔ)言中,類(lèi)都是對(duì)數(shù)據(jù)的構(gòu)成(狀態(tài))以及數(shù)據(jù)?能做什么(行為)的描述,下面我們就來(lái)你看看python數(shù)據(jù)結(jié)構(gòu)中的數(shù)據(jù)類(lèi)型商務(wù)詳細(xì)介紹,需要的小伙伴可以參考一下2021-12-12
解決django后臺(tái)樣式丟失,css資源加載失敗的問(wèn)題
今天小編就為大家分享一篇解決django后臺(tái)樣式丟失,css資源加載失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

