Python繪圖之詳解matplotlib
一、matplotlib介紹
matplotlib是python從matlab繼承的繪圖庫,可以滿足大部分的日常使用,是目前最流行的底層繪圖庫。
二、matplotlib的使用
(一)導(dǎo)入模塊【中文顯示】
顯示中文方面mac和windows根據(jù)自己電腦系統(tǒng)選一個(gè)即可
import matplotlib.pyplot as plt # 顯示中文(mac) from matplotlib.font_manager import FontManager fm = FontManager() mat_fonts = set(f.name for f in fm.ttflist) print(mat_fonts) plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] #顯示中文(windows) from pylab import mpl #以黑體顯示中文 mpl.rcParams['font.sans-serif']=[SimHei] #解決保存圖像是負(fù)號(hào) 顯示為方塊的問題 mpl.rcParams['axes.unicode_minus']=False # 導(dǎo)入numpy 方便下面繪圖展示 import numpy as np
(二)畫布與畫板,簡(jiǎn)單繪圖
和現(xiàn)實(shí)世界繪圖一樣,在matplotlib里繪圖我們也需定義畫布和畫板,其中一個(gè)畫布里可以存在多個(gè)畫板。在繪圖時(shí)首先要指明在哪個(gè)畫板上繪圖。
# 建立一張畫布 其中包括2行三列六張畫板 fig,axes=plt.subplots(nrows=2,ncols=3,figsize=(20,8)) # data x為測(cè)試數(shù)據(jù) np.random.seed(100) data=np.random.randn(50) x=np.arange(50) # 在第一個(gè)子圖上做折線圖 axes[0,0].plot(x,data,linestyle='-',color='b',marker='o') # 在第二個(gè)子圖上做直方圖 axes[0,1].hist(data,bins=20,facecolor='c') # 在第三個(gè)子圖上做垂直條形圖同時(shí)加上折線 axes[0,2].bar(x,data) axes[0,2].plot(x,data,linestyle='-.',color='r') # 在第四個(gè)子圖上做水平條形圖 axes[1,0].barh(x,data) # 在第五個(gè)子圖上做餅圖 explode為突出顯示的部分 explode=[x*0 for x in range(50)] explode[40]=0.1 axes[1,1].pie(data,explode=explode) # 在第六個(gè)子圖上做散點(diǎn)圖 explode為突出顯示的部分 axes[1,2].scatter(x,data,c='r',marker='o') plt.show()
(三)添加圖片信息
import matplotlib.pyplot as plt # 顯示中文【mac】 from matplotlib.font_manager import FontManager fm = FontManager() mat_fonts = set(f.name for f in fm.ttflist) print(mat_fonts) plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] #設(shè)置所需數(shù)據(jù) age = range(11, 31) jack = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1] tom = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1] # dpi為設(shè)置像素大小 fig = plt.figure(figsize=(20, 8), dpi=80) # jack與tom11到30交友記錄 plt.plot(age, jack, 'r', label='jack', linestyle='-', linewidth=5, marker='o') plt.plot(age, tom, 'g', label='tom', linestyle='-.', linewidth=5, marker='*') # 設(shè)置x軸數(shù)據(jù)刻度 plt.xticks(age) # 設(shè)置x軸數(shù)據(jù)標(biāo)簽 plt.xlabel("age", fontsize=20) # 設(shè)置y軸數(shù)據(jù)標(biāo)簽 plt.ylabel("numbers", fontsize=20) # 設(shè)置圖表標(biāo)題 plt.title("friends made from 11 to 30", fontsize=20) # 設(shè)置網(wǎng)格線 plt.grid() # 設(shè)置圖例位置 plt.legend(loc=0) # 添加水印 plt.text(30, 2, "交友記錄", fontsize=200, color='black', ha='right', va='bottom', alpha=0.1) # 添加數(shù)據(jù)標(biāo)簽 plt.text(23, 6, 'max num', fontsize=20, color='b', verticalalignment='center') #將圖保存到當(dāng)前目錄 命名為test.png plt.savefig('test.png') plt.show()
到此這篇關(guān)于Python繪圖之詳解matplotlib的文章就介紹到這了,更多相關(guān)python繪圖之matplotlib內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決C++ openCV無法讀取視頻但是可以讀取圖像的問題記錄
在使用OpenCV的cv::VideoCapture讀取視頻文件時(shí),可能會(huì)遇到無法讀取特定格式,如MP4的視頻文件的問題,本文介紹解決C++ openCV無法讀取視頻但是可以讀取圖像的問題記錄,感興趣的朋友跟隨小編一起看看吧2024-09-09C語言中回調(diào)函數(shù)的含義與使用場(chǎng)景詳解(2)
這篇文章主要為大家詳細(xì)介紹了C語言中回調(diào)函數(shù)的含義與使用場(chǎng)景,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03C++求1到n中1出現(xiàn)的次數(shù)以及數(shù)的二進(jìn)制表示中1的個(gè)數(shù)
這篇文章主要介紹了C++求1到n中1出現(xiàn)的次數(shù)以及數(shù)的二進(jìn)制表示中1的個(gè)數(shù),兩道基礎(chǔ)的算法題目,文中也給出了解題思路,需要的朋友可以參考下2016-02-02C++實(shí)現(xiàn)獲取IP、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS等本機(jī)網(wǎng)絡(luò)參數(shù)的方法
這篇文章主要介紹了C++實(shí)現(xiàn)獲取IP、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS等本機(jī)網(wǎng)絡(luò)參數(shù)的方法,需要的朋友可以參考下2014-07-07C++使用QTcreator創(chuàng)建動(dòng)態(tài)庫流程
在工程中,經(jīng)常會(huì)根據(jù)不同的場(chǎng)景需求將類封裝成庫文件,本文主要介紹了C++使用QTcreator創(chuàng)建動(dòng)態(tài)庫流程,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06