Python實(shí)現(xiàn)繪制水平線
我們將介紹如何在Python中創(chuàng)建一條水平線。 我們還將介紹 Python 中的 Matplotlib 庫。
Python 中的水平線
水平線是從左到右或從右到左的任何直線。 當(dāng)我們在坐標(biāo)平面中看到它時,它是一條平行于 x 軸的線。
在 Python 中,Matplotlib 廣泛用于繪圖。 有多種方法可以繪制水平線,如下所示。
- 通過
plot()
函數(shù)繪制水平線。 - 通過
hlines()
函數(shù)繪制水平線。 - 通過
axhline()
函數(shù)繪制水平線。
在Python中使用plot()函數(shù)
當(dāng)我們的目標(biāo)是生成 2D 繪圖時,我們可以使用 Plot()
函數(shù)。 X 點(diǎn)是朝向繪圖的 x 軸點(diǎn),Y 點(diǎn)是 y 軸點(diǎn)。
代碼:
# python import matplotlib.pyplot as plotLine xAxis = [3, 5, 7, 9] yAxis = [0, 0, 0, 0] plotLine.plot(xAxis,yAxis) plotLine.show()
輸出:
首先,我們導(dǎo)入了 matplotlib.pyplot 庫,然后概述了我們想要繪制的數(shù)據(jù)點(diǎn)。 在本例中,我們將 y 軸點(diǎn)設(shè)置為 0,因?yàn)槲覀兊哪繕?biāo)是繪制一條水平線。
我們應(yīng)用 plotLine.plot()
函數(shù)來繪制一條線,出于視覺目的,我們使用了 plotLine.show()
。
在 Python 中使用 hlines() 函數(shù)
當(dāng)我們想要繪制一條穿過軸的水平線時,我們使用 hlines() 函數(shù)。 這個函數(shù)將簡化我們的任務(wù)。
語法:
# python hlines(Yaxis, XaxisStart, XaxisEnd, lineColor, lineStyle)
這里使用了四個參數(shù),Yaxis 表示當(dāng)我們需要繪制一條線時在 y 軸上的位置。 XaxisStart 和 XaxisEnd 指示線的開始位置和結(jié)束位置。
lineColor 將為線條添加所需的顏色,lineStyle 將添加我們指定的線條的樣式或類型。
代碼:
# python import matplotlib.pyplot as plotLine plotLine.hlines(3, 5, 10, color='blue') plotLine.show()
輸出:
我們使用 matplotlib.pyplot 庫在 hlines()
函數(shù)的幫助下創(chuàng)建水平線。 作為參數(shù),我們傳遞了值并得到了如上所示的結(jié)果。
在 Python 中使用 axhline() 函數(shù)
axhline() 函數(shù)旨在在繪圖上繪制水平線。 axhline()
函數(shù)具有與 hlines()
函數(shù)類似的參數(shù)。
代碼:
# python import matplotlib.pyplot as plotLine plotLine.axhline(y=1.3, xmin=0.2, xmax=0.7) plotLine.show()
輸出:
我們畫了一條水平線,授權(quán)y、xmin、xmax為參數(shù),固定為1.3、0.2、0.7。
Python 中的水平虛線
Matplotlib 庫還允許我們繪制虛線。 當(dāng)我們需要一條水平虛線時,我們必須將線條樣式更改為虛線,這樣就可以滿足我們的需要。
Matplotlib.pyplot 庫提供了 linestyle 參數(shù)來設(shè)置線類型。
代碼:
# python import matplotlib.pyplot as plotLine plotLine.axhline(y=1.3, xmin=0.2, xmax=0.7, linestyle='dotted') plotLine.show()
輸出:
axhline()
函數(shù)有四個參數(shù) y、xmin、xmax 和 linestyle。 我們的目標(biāo)是實(shí)現(xiàn)水平線的點(diǎn)線風(fēng)格,所以我們將linestyle固定為點(diǎn)線。
Python 中帶標(biāo)簽的水平線
我們還可以借助 axhline()
函數(shù)實(shí)現(xiàn)帶有標(biāo)簽的水平線。 我們必須將標(biāo)簽設(shè)置為參數(shù)。
代碼:
# python import matplotlib.pyplot as plotLine plotLine.axhline(y=1.3, xmin=0.2, xmax=0.7, label= 'Line Label') plotLine.legend(loc='upper left') plotLine.show()
輸出:
我們可以使用 label 參數(shù)輕松地為水平線創(chuàng)建標(biāo)簽。 我們可以使用另一個函數(shù) legend()
來定義標(biāo)簽的位置。
多條水平線 Matplotlib
我們還可以在Python中的matplotlib中實(shí)現(xiàn)多條水平線。 有兩種方法可以實(shí)現(xiàn)我們的目標(biāo):使用 axhline()
方法或使用 hlines()
方法。
Axhline()
方法允許我們在圖中獲得多條水平線。
代碼:
# python import matplotlib.pyplot as plotLine plotLine.axhline(y=1.3, xmin=0.2, xmax=0.7, label= 'Blue Line Label') plotLine.legend(loc='upper left') plotLine.axhline(y=1.8, xmin=0.6, xmax=0.9, label= 'Red Line Label', color="red") plotLine.legend(loc='upper left') plotLine.axhline(y=1.5, xmin=0.5, xmax=0.9, label= 'Yellow Line Label', color="yellow") plotLine.legend(loc='upper left') plotLine.show()
輸出:
到此這篇關(guān)于Python實(shí)現(xiàn)繪制水平線的文章就介紹到這了,更多相關(guān)python繪制水平線內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python多進(jìn)程共享numpy 數(shù)組的方法
這篇文章主要介紹了Python多進(jìn)程共享numpy 數(shù)組的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07node.js獲取參數(shù)的常用方法(總結(jié))
下面小編就為大家?guī)硪黄猲ode.js獲取參數(shù)的常用方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Python內(nèi)置函數(shù)issubclass()的具體使用
issubclass()是Python中一個非常有用的內(nèi)置函數(shù),它提供了一種簡單的方式來檢查類的繼承關(guān)系,本文主要介紹了Python內(nèi)置函數(shù)issubclass()的具體使用,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2007-03-03Python列表list操作相關(guān)知識小結(jié)
今天,本喵帶大家仔細(xì)溫習(xí)一下Python的列表,溫故而知新,不亦說乎,需要的朋友可以參考下2020-01-01python中使用mysql數(shù)據(jù)庫詳細(xì)介紹
這篇文章主要介紹了python中使用mysql數(shù)據(jù)庫詳細(xì)介紹,本文起講解了安裝mysql、安裝MySQL-python、mysql 的基本操作、python 操作mysql數(shù)據(jù)庫基礎(chǔ)等內(nèi)容,需要的朋友可以參考下2015-03-03Python項(xiàng)目實(shí)戰(zhàn)之使用Django框架實(shí)現(xiàn)支付寶付款功能
這篇文章主要介紹了Python項(xiàng)目實(shí)戰(zhàn)之使用Django框架實(shí)現(xiàn)支付寶付款功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))
這篇文章主要給大家介紹了關(guān)于利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2018-03-03pandas DataFrame 賦值的注意事項(xiàng)說明(index)
這篇文章主要介紹了pandas DataFrame 賦值的注意事項(xiàng)說明(index),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04