簡單介紹
matplotlib庫是Python數(shù)據(jù)挖掘中的庫之一,主要用于2D繪圖,簡單的3D繪圖,數(shù)據(jù)可視化的庫。

Python數(shù)據(jù)挖掘相關(guān)擴(kuò)展庫
簡單使用
(一)畫根直線

y=x+10的直線
代碼:
def print_line_draw():
"""
畫直線圖
:return:
"""
# 創(chuàng)建一個0-10之間以1為間隔的numpy數(shù)組
x = np.arange(0, 10, 1)
y = x + 10
# 繪制圖形
plt.plot(x, y, color='blue', linestyle='--', marker='*', linewidth=1, label=' y = x + 10 ')
# 保存圖片,dpi表示
plt.savefig('first.png', dpi=50)
# 顯示圖例
plt.legend()
# 顯示圖形
plt.show()
(二)畫個餅圖

餅圖
代碼:
def print_pie_draw():
"""
畫餅狀圖
:return:
"""
# 指定每個切片大?。ū壤? slice = [2,3,4,9]
# 指定標(biāo)簽
activities =['sleeping','eating','working','playing']
# 顏色
cols = ['c','m','r','b']
plt.pie(slice,
labels=activities,
colors=cols,
startangle=90, #開始角度,默認(rèn)是0度,從x軸開始,90度從y軸開始
shadow=True,
explode=(0,0.1,0,0), # 拉出第二個切片,如果全為0,就不拉出,這里的數(shù)字相對于圓心的距離
autopct='%1.1f%%') # 顯示百分比
plt.title('Activities analysis')
# explode 這個詞里面的參數(shù)第一個0代表第一個切片,距離原點為0距離
plt.show()
(三)畫個散點圖

散點圖
代碼:
def print_scatter_draw():
"""
畫散點圖
:return:
"""
x = np.random.rand(1000)
y = np.random.rand(len(x))
# 繪圖
plt.scatter(x,y,color='r',alpha=0.3,label='scatter draw ',marker='p')
plt.legend()
#plt.axis([0,2,0,2])# 設(shè)置坐標(biāo)的范圍
plt.show()
(四)畫個直方圖

有點丑的直方圖
代碼:
def print_hist_draw():
"""
繪制直方圖
:return:
"""
x = np.random.randint(1,1000,200)
axit = plt.gca() # 得到當(dāng)前的繪圖對象
#bins 直方圖的個數(shù),histtype 直方圖的樣式 normed 直方圖歸一化,顯示概率密度(默認(rèn)false)
axit.hist(x,bins=35,facecolor='r',normed=True,histtype='bar',alpha=0.5)
axit.set_xlabel('values') # 設(shè)置x的標(biāo)簽
axit.set_ylabel('Frequery') # 設(shè)置y的標(biāo)簽
axit.set_title('HIST')
plt.show()
(五)簡單的三角函數(shù)

三角函數(shù)圖
代碼:
def print_sinandcos_line():
x = np.linspace(0,10,1000)
y = np.sin(x) +1
z = np.cos(x**2) +1
# 設(shè)置圖像大小
plt.figure(figsize=(8,4))
# 作圖,設(shè)置標(biāo)簽,線條顏色,線條大小
plt.plot(x,y,label='$\sin x +1$',color='red',linewidth=2)
# 作圖,設(shè)置標(biāo)簽,線條類型
plt.plot(x,z,'b--',label='$\cos x^2+1$')
# 設(shè)置x軸名稱
plt.xlabel('Time(s)')
# 設(shè)置y軸名稱
plt.ylabel('Volt')
# 設(shè)置標(biāo)題
plt.title('A simple Example')
# 顯示的y軸范圍
plt.ylim(0,2.2)
# 顯示圖例
plt.legend()
# 顯示作圖結(jié)果
plt.show()
以上就是對今天matplotlib函數(shù)的簡單使用
全部代碼
# -*- coding: utf-8 -*-
# @Time : 2018/3/26 15:09
# @Author : 蛇崽
# @Email : 643435675@QQ.com
# @File : test_matplotlib.py
import matplotlib.pyplot as plt
import numpy as np
def print_line_draw():
"""
畫直線圖
:return:
"""
# 創(chuàng)建一個0-10之間以1為間隔的numpy數(shù)組
x = np.arange(0, 10, 1)
y = x + 10
# 繪制圖形
plt.plot(x, y, color='blue', linestyle='--', marker='*', linewidth=1, label=' y = x + 10 ')
# 保存圖片,dpi表示
plt.savefig('first.png', dpi=50)
# 顯示圖例
plt.legend()
# 顯示圖形
plt.show()
def print_pie_draw():
"""
畫餅狀圖
:return:
"""
# 指定每個切片大?。ū壤? slice = [2,3,4,9]
# 指定標(biāo)簽
activities =['sleeping','eating','working','playing']
# 顏色
cols = ['c','m','r','b']
plt.pie(slice,
labels=activities,
colors=cols,
startangle=90, #開始角度,默認(rèn)是0度,從x軸開始,90度從y軸開始
shadow=True,
explode=(0,0.1,0,0), # 拉出第二個切片,如果全為0,就不拉出,這里的數(shù)字相對于圓心的距離
autopct='%1.1f%%') # 顯示百分比
plt.title('Activities analysis')
# explode 這個詞里面的參數(shù)第一個0代表第一個切片,距離原點為0距離
plt.show()
def print_scatter_draw():
"""
畫散點圖
:return:
"""
x = np.random.rand(1000)
y = np.random.rand(len(x))
# 繪圖
plt.scatter(x,y,color='r',alpha=0.3,label='scatter draw ',marker='p')
plt.legend()
#plt.axis([0,2,0,2])# 設(shè)置坐標(biāo)的范圍
plt.show()
pass
def print_hist_draw():
"""
繪制直方圖
:return:
"""
x = np.random.randint(1,1000,200)
axit = plt.gca() # 得到當(dāng)前的繪圖對象
#bins 直方圖的個數(shù),histtype 直方圖的樣式 normed 直方圖歸一化,顯示概率密度(默認(rèn)false)
axit.hist(x,bins=35,facecolor='r',normed=True,histtype='bar',alpha=0.5)
axit.set_xlabel('values') # 設(shè)置x的標(biāo)簽
axit.set_ylabel('Frequery') # 設(shè)置y的標(biāo)簽
axit.set_title('HIST')
plt.show()
pass
def print_sinandcos_line():
x = np.linspace(0,10,1000)
y = np.sin(x) +1
z = np.cos(x**2) +1
# 設(shè)置圖像大小
plt.figure(figsize=(8,4))
# 作圖,設(shè)置標(biāo)簽,線條顏色,線條大小
plt.plot(x,y,label='$\sin x +1$',color='red',linewidth=2)
# 作圖,設(shè)置標(biāo)簽,線條類型
plt.plot(x,z,'b--',label='$\cos x^2+1$')
# 設(shè)置x軸名稱
plt.xlabel('Time(s)')
# 設(shè)置y軸名稱
plt.ylabel('Volt')
# 設(shè)置標(biāo)題
plt.title('A simple Example')
# 顯示的y軸范圍
plt.ylim(0,2.2)
# 顯示圖例
plt.legend()
# 顯示作圖結(jié)果
plt.show()
pass
if __name__ == '__main__':
# print_line_draw()
# print_pie_draw()
# print_scatter_draw()
# print_hist_draw()
print_sinandcos_line()