import numpy as np
import pandas as pd
import matplotlib. pyplot as plt
% matplotlib inline
1 柱狀圖與堆疊圖
fig, axes = plt. subplots( 3 , 1 , figsize= ( 10 , 10 ) )
s = pd. Series( np. random. randint( 0 , 10 , 16 ) , index = list ( 'abcdefghijklmnop' ) )
df = pd. DataFrame( np. random. rand( 10 , 3 ) , columns= [ 'a' , 'b' , 'c' ] )
s. plot( kind= 'bar' , color = 'k' , grid = True , alpha = 0.5 , ax = axes[ 0 ] )
df. plot( kind= 'bar' , ax = axes[ 1 ] , grid = True , colormap= 'Reds_r' )
df. plot( kind= 'bar' , ax = axes[ 2 ] , grid = True , colormap= 'Blues_r' , stacked= True )
2 面積圖
fig, axes = plt. subplots( 2 , 1 , figsize = ( 10 , 10 ) )
df1 = pd. DataFrame( np. random. rand( 10 , 4 ) , columns= [ 'a' , 'b' , 'c' , 'd' ] )
df2 = pd. DataFrame( np. random. randn( 10 , 4 ) , columns= [ 'a' , 'b' , 'c' , 'd' ] )
df1. plot. area( colormap = 'Greens_r' , alpha = 0.5 , ax = axes[ 0 ] )
plt. legend( loc= 'upper right' )
df2. plot. area( stacked= False , colormap = 'Set2' , alpha = 0.5 , ax = axes[ 1 ] )
3 餅圖
s = pd. Series( 3 * np. random. rand( 4 ) , index= [ 'a' , 'b' , 'c' , 'd' ] , name= 'series' )
plt. axis( 'equal' )
plt. pie( s,
explode = [ 0.1 , 0 , 0 , 0 ] ,
labels = s. index,
colors= [ 'r' , 'g' , 'b' , 'c' ] ,
autopct= '%.2f%%' ,
pctdistance= 0.6 ,
labeldistance = 1.2 ,
shadow = True ,
startangle= 0 ,
radius= 1.5 ,
frame= False )
4 直方圖+密度圖
s = pd. Series( np. random. randn( 1000 ) )
s. hist( bins = 20 ,
histtype = 'bar' ,
align = 'mid' ,
orientation = 'vertical' ,
alpha= 0.5 ,
normed = True )
s. plot( kind= 'kde' , style= 'k--' )
5 堆疊直方圖
df = pd. DataFrame( { 'a' : np. random. randn( 1000 ) + 1 , 'b' : np. random. randn( 1000 ) ,
'c' : np. random. randn( 1000 ) - 1 , 'd' : np. random. randn( 1000 ) - 2 } ,
columns= [ 'a' , 'b' , 'c' , 'd' ] )
df. plot. hist( stacked= True ,
bins= 20 ,
colormap= 'Greens_r' ,
alpha= 0.5 ,
grid= True )
6 散點圖
x = np. random. randn( 1000 )
y = np. random. randn( 1000 )
plt. scatter( x, y, marker= '.' ,
s = np. random. randn( 1000 ) * 100 ,
cmap = 'Reds' ,
c = y,
alpha = 0.8 , )
7 箱型圖
fig, axes = plt. subplots( 2 , 1 , figsize= ( 10 , 6 ) )
df = pd. DataFrame( np. random. rand( 10 , 5 ) , columns= [ 'A' , 'B' , 'C' , 'D' , 'E' ] )
color = dict ( boxes= 'DarkGreen' , whiskers= 'DarkOrange' , medians= 'DarkBlue' , caps= 'Gray' )
df. plot. box( ylim= [ 0 , 1.2 ] ,
grid = True ,
color = color,
ax = axes[ 0 ] )
df. plot. box( vert= False ,
positions= [ 1 , 4 , 5 , 6 , 8 ] ,
ax = axes[ 1 ] ,
grid = True ,
color = color)