首頁 > 後端開發 > Python教學 > 引入Python' s Matplotlib庫

引入Python' s Matplotlib庫

Joseph Gordon-Levitt
發布: 2025-02-27 09:26:10
原創
429 人瀏覽過

生產出版物就緒的圖表對研究人員至關重要。 儘管存在各種工具,但是實現視覺吸引力的結果可能具有挑戰性。 本教程演示了Python的matplotlib庫如何簡化此過程,並以最小的代碼生成了高質量的數字。

>matplotlibmatplotlib網站所述:“

是一個python 2d繪製庫,以各種平台的各種格式生產出版物質量數字。” 它的用途廣泛,可在腳本,外殼,Web應用程序和各種GUI工具包中使用。

> matplotlib本指南涵蓋

安裝和基本繪圖示例。 有關更深入的Python數據處理技能,請考慮探索相關的在線課程(省略為簡潔的鏈接)。

matplotlib安裝

pip安裝很簡單。 使用matplotlib(存在其他方法,請參見

>安裝頁面以獲取詳細信息):>
curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip install matplotlib
登入後複製

基本繪圖示例

我們將使用matplotlib.pyplot,提供類似MATLAB的接口。

1。線圖

考慮繪製要點:

x = (4, 8, 13, 17, 20)y = (54, 67, 98, 78, 45)>

import matplotlib.pyplot as plt
plt.plot([4, 8, 13, 17, 20], [54, 67, 98, 78, 45], 'g--d') # Green dashed line with diamond markers
plt.show()
登入後複製

Introducing Python's Matplotlib Library

讓我們想像一下紐約市的平均降雨量:

month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
rainfall = [83, 81, 97, 104, 107, 91, 102, 102, 102, 79, 102, 91]
plt.plot(month, rainfall)
plt.xlabel("Month")
plt.ylabel("Rainfall (mm)")
plt.title("Average Rainfall in New York City")
plt.show()
登入後複製

Introducing Python's Matplotlib Library

2。散點圖

說明兩個數據集之間的關係:

x = [2, 4, 6, 7, 9, 13, 19, 26, 29, 31, 36, 40, 48, 51, 57, 67, 69, 71, 78, 88]
y = [54, 72, 43, 2, 8, 98, 109, 5, 35, 28, 48, 83, 94, 84, 73, 11, 464, 75, 200, 54]
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Scatter Plot')
plt.grid(True)
plt.scatter(x, y, c='green')
plt.show()
登入後複製

Introducing Python's Matplotlib Library

3。直方圖 直方圖可視化數據頻率分佈:

x = [2, 4, 6, 5, 42, 543, 5, 3, 73, 64, 42, 97, 63, 76, 63, 8, 73, 97, 23, 45, 56, 89, 45, 3, 23, 2, 5, 78, 23, 56, 67, 78, 8, 3, 78, 34, 67, 23, 324, 234, 43, 544, 54, 33, 223, 443, 444, 234, 76, 432, 233, 23, 232, 243, 222, 221, 254, 222, 276, 300, 353, 354, 387, 364, 309]
num_bins = 6
n, bins, patches = plt.hist(x, num_bins, facecolor='green')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Histogram')
plt.show()
登入後複製

Introducing Python's Matplotlib Library

結論

賦予研究人員有效創建具有視覺吸引力和出版物的圖表。它的易用性和廣泛的自定義選項使其成為數據可視化的寶貴工具。 探索

文檔和示例以獲取進一步的功能。 matplotlib

以上是引入Python' s Matplotlib庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板