Python使用Matplotlib繪制散點(diǎn)趨勢(shì)線(xiàn)的代碼詳解
Matplotlib繪制散點(diǎn)趨勢(shì)線(xiàn)
散點(diǎn)圖是一種數(shù)據(jù)可視化,它使用點(diǎn)來(lái)表示兩個(gè)不同變量的值。水平軸和垂直軸上每個(gè)點(diǎn)的位置表示單個(gè)數(shù)據(jù)點(diǎn)的值。散點(diǎn)圖用于觀(guān)察變量之間的關(guān)系。
1.創(chuàng)建基本散點(diǎn)圖
讓我們從創(chuàng)建一個(gè)基本的散點(diǎn)圖開(kāi)始。為了簡(jiǎn)單起見(jiàn),我們將使用隨機(jī)數(shù)據(jù)。
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(50)
y = np.random.rand(50)
plt.scatter(x, y)
plt.title("Basic Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

2.添加線(xiàn)性趨勢(shì)線(xiàn)
線(xiàn)性趨勢(shì)線(xiàn)是最能代表散點(diǎn)圖上數(shù)據(jù)的直線(xiàn)。要添加線(xiàn)性趨勢(shì)線(xiàn),我們可以使用NumPy的polyfit()函數(shù)來(lái)計(jì)算最佳擬合線(xiàn)。
# Calculate the best-fit line
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
# Plot the scatter plot and the trend line
plt.scatter(x, y)
plt.plot(x, p(x), "r--") # 'r--' is for a red dashed line
plt.title("Scatter Plot with Linear Trend Line")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

3.添加多項(xiàng)式趨勢(shì)線(xiàn)
有時(shí),線(xiàn)性趨勢(shì)線(xiàn)可能不足以捕捉變量之間的關(guān)系。在這種情況下,多項(xiàng)式趨勢(shì)線(xiàn)可能更合適。我們可以使用polyfit()函數(shù),它的階數(shù)更高。
# Calculate the polynomial trend line (degree 2)
z = np.polyfit(x, y, 2)
p = np.poly1d(z)
# Plot the scatter plot and the polynomial trend line
plt.scatter(x, y)
plt.plot(x, p(x), "g-") # 'g-' is for a green solid line
plt.title("Scatter Plot with Polynomial Trend Line")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

自定義趨勢(shì)線(xiàn)
Matplotlib允許對(duì)圖進(jìn)行廣泛的自定義,包括趨勢(shì)線(xiàn)的外觀(guān)。您可以修改趨勢(shì)線(xiàn)的顏色、線(xiàn)型和寬度。
# Calculate the best-fit line
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
# Plot the scatter plot and the customized trend line
plt.scatter(x, y)
plt.plot(x, p(x), color="purple", linewidth=2, linestyle="--")
plt.title("Scatter Plot with Customized Trend Line")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

多條趨勢(shì)線(xiàn)
在某些情況下,您可能希望比較同一散點(diǎn)圖上的不同趨勢(shì)線(xiàn)。這可以通過(guò)計(jì)算和繪制多條趨勢(shì)線(xiàn)來(lái)實(shí)現(xiàn)。
# Generate random data
x = np.random.rand(50)
y = np.random.rand(50)
# Calculate the linear and polynomial trend lines
z1 = np.polyfit(x, y, 1)
p1 = np.poly1d(z1)
z2 = np.polyfit(x, y, 2)
p2 = np.poly1d(z2)
# Plot the scatter plot and both trend lines
plt.scatter(x, y)
plt.plot(x, p1(x), "r--", label="Linear Trend Line")
plt.plot(x, p2(x), "g-", label="Polynomial Trend Line")
plt.title("Scatter Plot with Multiple Trend Lines")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()

總結(jié)
在Matplotlib中向散點(diǎn)圖添加趨勢(shì)線(xiàn)是可視化和理解變量之間關(guān)系的強(qiáng)大方法。無(wú)論您需要簡(jiǎn)單的線(xiàn)性趨勢(shì)線(xiàn)還是更復(fù)雜的多項(xiàng)式趨勢(shì)線(xiàn),Matplotlib都提供了創(chuàng)建信息豐富且視覺(jué)上吸引人的圖表所需的工具。
到此這篇關(guān)于Python使用Matplotlib繪制散點(diǎn)趨勢(shì)線(xiàn)的代碼詳解的文章就介紹到這了,更多相關(guān)Python Matplotlib散點(diǎn)趨勢(shì)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 經(jīng)典面試題 21 道【不可錯(cuò)過(guò)】
這篇文章主要介紹了Python 經(jīng)典面試題 21 道,在python面試過(guò)程中這21道是經(jīng)常被問(wèn)到了,感興趣的朋友跟隨小編一起看看吧2018-09-09
Pygame實(shí)戰(zhàn)之實(shí)現(xiàn)扎氣球游戲
這篇文章主要為大家介紹了利用Python中的Pygame模塊實(shí)現(xiàn)的一個(gè)扎氣球游戲,文中的示例代碼講解詳細(xì),對(duì)我們了解Pygame模塊有一定的幫助,感興趣的可以學(xué)習(xí)一下2021-12-12
Pandas技巧分享之創(chuàng)建測(cè)試數(shù)據(jù)
學(xué)習(xí)pandas的過(guò)程中,為了嘗試pandas提供的各類(lèi)功能強(qiáng)大的函數(shù),常常需要花費(fèi)很多時(shí)間去創(chuàng)造測(cè)試數(shù)據(jù),本篇介紹了一些快速創(chuàng)建測(cè)試數(shù)據(jù)的方法,需要的可以參考一下2023-07-07
python UNIX_TIMESTAMP時(shí)間處理方法分析
這篇文章主要介紹了python UNIX_TIMESTAMP時(shí)間處理方法,結(jié)合實(shí)例形式分析了Python針對(duì)UNIX_TIMESTAMP時(shí)間的常見(jiàn)運(yùn)算技巧,需要的朋友可以參考下2016-04-04
Python識(shí)別驗(yàn)證碼的實(shí)現(xiàn)示例
這篇文章主要介紹了Python識(shí)別驗(yàn)證碼的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python實(shí)現(xiàn)控制臺(tái)輸入密碼的方法
這篇文章主要介紹了Python實(shí)現(xiàn)控制臺(tái)輸入密碼的方法,實(shí)例對(duì)比分析了幾種輸入密碼的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05

