
matplotlib是我們經(jīng)常會用到的一款python繪圖庫,操作簡單,幾行代碼就能很輕松地畫一些或簡單或復雜地圖形,線圖、直方圖、功率譜、條形圖、錯誤圖、散點圖以及費笛卡爾坐標圖等都不在話下。今天小編就具體給大家介紹一下matplotlib繪圖教程。
一、首先來了解一下matplotlib
1.matplotlib是基于python語言的開源數(shù)據(jù)繪圖包。matplotlib的對象體系嚴謹而有趣,為我們提供了巨大的發(fā)揮空間。在熟悉了核心對象之后,我們可以輕易的定制圖像。matplotlib使用numpy進行數(shù)組運算,并調用一系列其他的python庫來實現(xiàn)硬件交互。
2.matplotlib安裝
pip install matplotlib
3.Matplotlib導入
import matplotlib.pyplot as plt#為方便簡介為plt
import numpy as np#畫圖過程中會使用numpy
import pandas as pd#畫圖過程中會使用pandas
二、matplotlib繪圖
import numpy as np import pandas as pd from pandas import Series, DataFrame import matplotlib.pyplot as plt %matplotlib inline fig = plt.figure(figsize=(10,8)) #建立一個大小為10*8的畫板 ax1 = fig.add_subplot(331) #在畫板上添加3*3個畫布,位置是第1個 ax2 = fig.add_subplot(3,3,2) ax3 = fig.add_subplot(3,3,3) ax4 = fig.add_subplot(334) ax5 = fig.add_subplot(3,3,5) ax6 = fig.add_subplot(3,3,6) ax7 = fig.add_subplot(3,3,7) ax8 = fig.add_subplot(3,3,8) ax9 = fig.add_subplot(3,3,9) ax1.plot(np.random.randn(10)) _ = ax2.scatter(np.random.randn(10),np.arange(10),color='r') #作散點圖 ax3.hist(np.random.randn(20),bins=10,alpha=0.3) #作柱形圖 ax4.bar(np.arange(10),np.random.randn(10)) #做直方圖 ax5.pie(np.random.randint(1,15,5),explode=[0,0,0.2,0,0]) #作餅形圖 x = np.arange(10) y = np.random.randn(10) ax6.plot(x,y,color='green') ax6.bar(x,y,color='k') data = DataFrame(np.random.randn(1000,10), columns=['one','two','three','four','five','six','seven','eight','nine','ten']) data2 = DataFrame(np.random.randint(0,20,(10,2)),columns=['a','b']) data.plot(x='one',y='two',kind='scatter',ax=ax7) #針對DataFrame的一些作圖 data2.plot(x='a',y='b',kind='bar',ax=ax8,color='red',legend=False) data2.plot(x='a',y='b',kind='barh',color='m',ax=ax9) #plt.tight_layout() #避免出現(xiàn)疊影 #plt.show()
2.蠟燭圖
import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.finance as mpf from pandas import Series, DataFrame from matplotlib.pylab import date2num %matplotlib inline plt.rcParams['figure.autolayout'] = True plt.rcParams['figure.figsize'] = 25,6 plt.rcParams['grid.alpha'] = .4 plt.rcParams['axes.unicode_minus'] = False plt.rcParams['font.sans-serif'] = ['SimHei'] fig, ax = plt.subplots(1,1,figsize=(12,5)) mpf.candlestick_ohlc(ax=ax,quotes=data2.values[::3],width=.002,colorup='red',colordown='green') plt.xticks(data2.date[::25],data.date.map(lambda x:x[:5])[::25],rotation=0) ax.twiny().plot(data3.Open) plt.tight_layout();
3.熱圖
import numpy as np import pandas as pd from pandas import Series, DataFrame import matplotlib.pyplot as plt %matplotlib inline df = DataFrame(np.random.randn(10,10)) fig = plt.figure(figsize=(12,5)) ax = fig.add_subplot(111) axim = ax.imshow(df.values,interpolation='nearest')#cmap=plt.cm.gray_r, #cmap用來顯示顏色,可以另行設置 plt.colorbar(axim) plt.show()
以上就是小編今天跟大家分享的matplotlib繪圖的一些方法啦,希望對與大家使用matplotlib有所幫助。
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉換:從基礎用法到實戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關聯(lián)查詢效率:打破 “拆分必慢” 的認知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結構數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結構數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預期算子的內涵、作用與應用解析 動態(tài)隨機一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結構數(shù)據(jù)特征價值的專業(yè)核心 表結構數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結構化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實戰(zhàn)應用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時,“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗與 t 檢驗:差異、適用場景與實踐應用 在數(shù)據(jù)分析與統(tǒng)計學領域,假設檢驗是驗證研究假設、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結構數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結構數(shù)據(jù)(以 “行 - 列” 存儲的結構化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計劃中 rows 數(shù)量的準確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調優(yōu)中,EXPLAIN執(zhí)行計劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實踐指南 在 Python 進行 HTTP 網(wǎng)絡請求開發(fā)時(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結構數(shù)據(jù)價值的核心操盤手 表格結構數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點數(shù)據(jù)的科學計數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點數(shù)據(jù)時的科學計數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務數(shù)據(jù)分析步驟的落地者與價值優(yōu)化者 業(yè)務數(shù)據(jù)分析是企業(yè)解決日常運營問題、提升執(zhí)行效率的核心手段,其價值 ...
2025-09-12用 SQL 驗證業(yè)務邏輯:從規(guī)則拆解到數(shù)據(jù)把關的實戰(zhàn)指南 在業(yè)務系統(tǒng)落地過程中,“業(yè)務邏輯” 是連接 “需求設計” 與 “用戶體驗 ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅動下的精準零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當下,精準營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務數(shù)據(jù)分析:概念辨析與協(xié)同價值 在數(shù)據(jù)驅動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實踐到業(yè)務價值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價值導向 統(tǒng)計模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10