欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python繪圖之詳解matplotlib

 更新時(shí)間:2021年07月31日 08:32:55   作者:金融礦工  
這篇文章主要介紹了Python繪圖之詳解matplotlib,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

一、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)文章

最新評(píng)論