數據可視化庫一--matplotlib

1、

import matplotlib.pyplot as plt

Function Description
plt.figure(figsize,dpi,facecolor,edgecolor,frameon)

Create a new figure:

figsize(寬度,高度,單位:英寸),默認rcParams[figure.figsize]:[6.4,4.8];確保圖片有一個確定的大小以及存儲到硬盤時的長寬比

dpi:圖的分辨率,整數,默認rcParams[figure.dpi]: 100;

facecolor:背景顏色,默認rcParams[figure.facecolor]:'w';

edgecolor:邊框顏色,默認rcParams[figure.facecolor]:'w';

frameon:bool,默認True,若False則禁止繪製圖形框;

return:Figure

plt.subplot()

在當前圖形上添加子圖:

plt.subplots(nrows,ncols,sharex,sharey,subplot_kw)

創建一個圖和一組子圖:

nrows:子圖的行數;ncols:子圖的列數;

sharex:所有子圖使用相同的x軸刻度,調整xlim會影響所有子圖;

sharey:所有子圖使用相同的y軸刻度,調整ylim會影響所有子圖;

subplot_kw:傳入add_subplot的關鍵字參數字典,用於生成子圖;

plt.subplots_adjust(left=Nonebottom=Noneright=None,top=None

wspace=Nonehspace=None)

調整子圖佈局:

wspace和hspace分別控制圖片的寬度和高度百分比,用作子圖間的間距;

plt.xlabel(xlabel)、plt.ylabel(ylabel)

設置x軸、y軸標籤:

xlabel:str,標籤文本;

ylabel:str,標籤文本;

plt.xticks(ticks,labels,rotation)

獲取或設置當前x軸的當前刻度位置和標籤:

ticks:刻度,xtick位置列表;

labels:標籤,放置在給定刻度線位置的標籤;

rotation:刻度旋轉的角度;

return:x刻度位置列表、xlabel Text對象列表;

plt.yticks(ticks,labels)

獲取或設置當前y軸的當前刻度位置和標籤:

ticks:刻度,ytick位置列表;

labels:標籤,放置在給定刻度線位置的標籤;

return:y刻度位置列表、ylabel Text對象列表;

plt.xlim()、plt.ylim()

獲取或設置當前軸的x極限、y極限:

return:新x軸極限的元組

plt.xscale(value)

設置x軸比例:

value:{“ linear”,“ log”,“ symlog”,“ logit”,...}要應用的軸比例類型;

plt.title(label,loc)

設置標題:

label:標題文本;loc:{'center', 'left', 'right'},默認center

plt.text() 向軸添加文本:
plt.annotate() 用文本text註釋點xy:
plt.arrow() 向軸添加箭頭:
plt.figtext() 在圖上添加文本:
plt.imsave() 將數組另存爲圖像文件:
plt.draw() 重畫當前圖形:
plt.legend(loc)

繪製圖例:

loc:best、upper right、upper left、lower left、lower right、right、center left、center right、lower center、upper center、center;

plt.vlines() 繪製垂直線
   
plt.rc() 設置當前的rc參數:
plt.savefig() 保存當前圖形:
plt.grid() 繪製網格線:
plt.show()

顯示所有圖形:

plt.close() 關閉圖形窗口:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2、繪圖

Function Description
plt.plot() 繪製折線圖:
plt.scatter() 繪製散點圖:
plt.pie() 繪製餅圖:
plt.hist(x,bins,)

繪製直方圖:

 

plt.bar(x,height,width=0.8,bottom,aligh='center')

繪製條形圖:

x:條形的x座標;

width:條的寬度,默認0.8;height:條高;

left:條形圖底的y座標;

aligh:{'center','edge'},默認:'center',基準與x座標對齊;

plt.barh(y,width,height=0.8,left,aligh='center')

繪製水平條形圖:

y:條形的y座標;

width:條的寬度;height:條高;

left:條形圖左側的x座標;

aligh:{'center','edge'},默認:'center',基準與y座標對齊;

plt.box()  
plt.boxplot(x,) 繪製盒圖:
plt.violinplot() 繪製小提琴圖:
   
   
   
   
   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3、繪圖實例:

(1)簡單圖形

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

unrate=pd.read_csv("UNRATE.csv")
unrate["MONTH"]=unrate["DATE"].dt.month
fig=plt.figure(figsize=(8,5),dpi=80)

colors=["red","cyan","yellow","blue","black"]
for i in range(len(colors)):   
    plt.plot(unrate[12*i:12*(i+1)]["MONTH"],unrate[12*i:12*(i+1)]["VALUE"],c=colors[i],label=str(1948+i))
    
plt.legend(loc="best")
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')
plt.savefig("month-rate.png")
plt.show()

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

unrate=pd.read_csv("UNRATE.csv")
unrate["MONTH"]=unrate["DATE"].dt.month
fig=plt.figure(figsize=(8,5),dpi=80)

colors=["red","cyan","yellow","blue","black"]
for i in range(len(colors)):   
    plt.plot(unrate[12*i:12*(i+1)]["MONTH"],unrate[12*i:12*(i+1)]["VALUE"],c=colors[i],label=str(1948+i))
    
plt.legend(loc="best")
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')
plt.savefig("month-rate.png")
plt.show()
import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0,2.0,0.01)
s = 1+np.sin(2*np.pi*t)

fig,ax = plt.subplots()
ax.plot(t,s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()
fig.savefig("test.png")
plt.show()

(2)子圖

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax4 = fig.add_subplot(2,2,4)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章