Python使用Matplotlib繪制散點(diǎn)趨勢線的代碼詳解
Matplotlib繪制散點(diǎn)趨勢線
散點(diǎn)圖是一種數(shù)據(jù)可視化,它使用點(diǎn)來表示兩個(gè)不同變量的值。水平軸和垂直軸上每個(gè)點(diǎn)的位置表示單個(gè)數(shù)據(jù)點(diǎn)的值。散點(diǎn)圖用于觀察變量之間的關(guān)系。
1.創(chuàng)建基本散點(diǎn)圖
讓我們從創(chuàng)建一個(gè)基本的散點(diǎ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.添加線性趨勢線
線性趨勢線是最能代表散點(diǎn)圖上數(shù)據(jù)的直線。要添加線性趨勢線,我們可以使用NumPy的polyfit()函數(shù)來計(jì)算最佳擬合線。
# 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í),線性趨勢線可能不足以捕捉變量之間的關(guān)系。在這種情況下,多項(xiàng)式趨勢線可能更合適。我們可以使用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()
自定義趨勢線
Matplotlib允許對(duì)圖進(jì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()
多條趨勢線
在某些情況下,您可能希望比較同一散點(diǎn)圖上的不同趨勢線。這可以通過計(jì)算和繪制多條趨勢線來實(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)圖添加趨勢線是可視化和理解變量之間關(guān)系的強(qiáng)大方法。無論您需要簡單的線性趨勢線還是更復(fù)雜的多項(xiàng)式趨勢線,Matplotlib都提供了創(chuàng)建信息豐富且視覺上吸引人的圖表所需的工具。
到此這篇關(guān)于Python使用Matplotlib繪制散點(diǎn)趨勢線的代碼詳解的文章就介紹到這了,更多相關(guān)Python Matplotlib散點(diǎn)趨勢圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python動(dòng)態(tài)創(chuàng)建類實(shí)例詳解
這篇文章主要為大家介紹了Python動(dòng)態(tài)創(chuàng)建類實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Python爬蟲過程解析之多線程獲取小米應(yīng)用商店數(shù)據(jù)
這篇文章主要介紹了Python爬蟲過程解析之多線程獲取小米應(yīng)用商店數(shù)據(jù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11python ForMaiR實(shí)現(xiàn)自定義規(guī)則的郵件自動(dòng)轉(zhuǎn)發(fā)工具
這篇文章主要為大家介紹了python ForMaiR實(shí)現(xiàn)自定義規(guī)則的郵件自動(dòng)轉(zhuǎn)發(fā)工具示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12python實(shí)現(xiàn)一個(gè)函數(shù)版的名片管理系統(tǒng)過程解析
這篇文章主要介紹了python實(shí)現(xiàn)一個(gè)函數(shù)版的名片管理系統(tǒng)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python中的進(jìn)程操作模塊(multiprocess.process)
這篇文章介紹了Python中的進(jìn)程操作模塊(multiprocess.process),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05Python使用jpype模塊調(diào)用jar包過程解析
這篇文章主要介紹了Python使用jpype模塊調(diào)用jar包過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07使用Matplotlib繪制不同顏色的帶箭頭的線實(shí)例
這篇文章主要介紹了使用Matplotlib繪制不同顏色的帶箭頭的線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04