搜尋
首頁後端開發Python教學Python中matplotlib函式庫的用法介紹

這篇文章帶給大家的內容是關於Python中matplotlib庫的用法介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

介紹

matplotlib函式庫是Python中的一個2D繪圖函式庫。

安裝

$ pip install matplotlib

入門

繪製折線圖

pyplot函數繪製折線圖

這裡畫一個月份與溫度的折線圖:

import matplotlib.pyplot as plt

x_month = [3,4,5,6,7,8,9]
y_temperature = [18,24,27,30,35,37,31]
plt.plot(x_month,y_temperature)
plt.show()
# plt.savefig('xxx.png') # 保存图标函数

這裡導入pyplot模組並將其命名為plt。
show()用來展示圖片。 savefig()則用來儲存圖片。

設定x,y軸

import matplotlib.pyplot as plt

x_month = [1,2,3,4,5,6,7,8]
y_temperature = [11,22,23,43,20,23,42,42]
plt.plot(x_month,y_temperature,linewidth=5) # linewidth定义折线宽度
plt.title("This is Title")  # 标题
plt.xlabel("month",fontsize=14) # x轴
plt.ylabel("temperature",fontsize=14) # y轴
plt.tick_params(axis='both',labelsize=14) # 影响xy刻度的函数
plt.show() # 展示
# plt.savefig('xxx.png') # 保存图标函数

fontsize用來設定各軸的字體大小。

繪製單點圖

scatter函數繪製單點圖

import matplotlib.pyplot as plt

plt.scatter(1,8,s=50,c='red') 
plt.show()

參數s設定了繪製圖形時使用的點的尺寸。 c參數設定點的顏色,也可以用RDB方式來指定:c=(0,0,0.8)

繪製一系列點

import matplotlib.pyplot as plt
x_values = list(range(1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,s=40,edgecolors='none') # edgecolors参数用于去除外轮廓
plt.show()

edgecolors可以指定whiteblack等。

繪製顏色映射圖

import matplotlib.pyplot as plt


x_values = list(range(1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,s=40,edgecolors='none',c=y_values,cmap=plt.cm.Blues)

將y_values參數傳入c中,並使用cmap告訴函數使用哪個顏色進行映射。

#

以上是Python中matplotlib函式庫的用法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:segmentfault思否。如有侵權,請聯絡admin@php.cn刪除
對於循環和python中的循環時:每個循環的優點是什麼?對於循環和python中的循環時:每個循環的優點是什麼?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解釋嗎?揭穿神話Python:它是真正的解釋嗎?揭穿神話May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

與同一元素的Python串聯列表與同一元素的Python串聯列表May 11, 2025 am 12:08 AM

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

解釋與編譯語言:Python的位置解釋與編譯語言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境