常用头
import matplotlib. pyplot as plt
import numpy as np
import matplotlib as mpl
mpl. rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
mpl. rcParams[ 'axes.unicode_minus' ] = False
柱状图
x = np. arange( 9 ) [ 1 : ]
y = np. random. randint( 1 , 10 , 8 )
plt. ylabel( '箱子编码' )
plt. xlabel( '箱子重量(kg)' )
直方图
h = np. random. randint( 0 , 11 , 100 )
bins = range ( 0 , 12 , 1 )
plt. hist( h, bins= bins, color= 'g' , histtype= 'bar' , rwidth= 1 , alpha= 0.6 )
plt. xlabel( '箱子重量(kg)' )
plt. ylabel( '箱子个数(个)' )
饼状图
labels = [ 'SciFi' , 'Drama' , 'Thriller' , 'Comedy' , 'Action' , 'Romance' ]
sizes = [ 5 , 15 , 10 , 20 , 40 , 10 ]
explode = ( 0 , 0 , 0 , 0 , 0.1 , 0 )
plt. pie( sizes, labels= labels, explode= explode,
autopct= '%1.1f%%' , shadow= True , startangle= 90 )
plt. axis( 'equal' )
极坐标图
Depts = [ "COGS" , "IT" , "Payroll" , "R & D" , "Sales & Marketing" ]
rp = [ 30 , 15 , 25 , 10 , 20 , 30 ]
ra = [ 32 , 20 , 23 , 11 , 14 , 32 ]
theta = np. linspace( 0 , 2 * np. pi, len ( rp) )
plt. figure( figsize= ( 10 , 6 ) )
plt. subplot( polar= True )
( lines, labels) = plt. thetagrids( range ( 0 , 360 , int ( 360 / len ( Depts) ) ) , ( Depts) )
plt. plot( theta, rp)
plt. fill( theta, rp, 'b' , alpha= 0.1 )
plt. plot( theta, ra)
plt. legend( labels= ( 'Plan' , 'Actual' ) , loc= 1 )
plt. title( 'Plan versus Actual spend by Department' )
气泡图
a = np. random. randn( 100 )
b = np. random. randn( 100 )
plt. scatter( a, b, s= np. power( 10 * a + 20 * b, 2 ) , c= np. random. rand( 100 ) , cmap= mpl. cm. RdYlBu, marker= 'o' )
棉棒图
x = np. linspace( 0.5 , 2 * np. pi, 20 )
y = np. random. randn( 20 )
plt. stem( x, y, linefmt= '-.' , markerfmt= 'o' , basefmt= '-' )
箱线图
x = np. random. randn( 1000 )
plt. boxplot( x)
plt. xticks( [ 1 ] , [ '随机数生成器' ] )
plt. ylabel( '随机数值' )
plt. title( '随机生成器抗干扰能力的稳定性' )
堆叠图
x = np. arange( 7 ) [ 1 : ]
Apr = np. random. randint( 0 , 6 , 6 )
May = np. random. randint( 0 , 6 , 6 )
June = np. random. randint( 0 , 6 , 6 )
labels = [ 'April' , 'May' , 'June' ]
fig, ax = plt. subplots( )
ax. stackplot( x, Apr, May, June, labels= labels)
ax. legend( loc= 2 )
plt. xlabel( 'defect code' )
plt. ylabel( 'the num of defects' )
plt. title( 'Product Defect' )
plt. show( )