matplotlib:关联图correlation
散点图
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib. pyplot as plt
import seaborn as sns
% matplotlib inline
x1 = np. random. randn( 10 )
x2 = x1 + x1** 2 - 10
plt. figure( figsize= ( 8 , 4 ) )
plt. scatter( x1, x2, s= 50 , c= 'blue' , label= 'Positive' )
plt. legend( )
plt. show( )
标签
X = np. random. randn( 10 , 2 )
y = np. array( [ 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 ] )
plt. figure( figsize= ( 8 , 4 ) )
plt. scatter( X[ : , 0 ] , X[ : , 1 ] , s= 50 , c= y)
plt. show( )
X = np. random. randn( 10 , 2 )
y = np. array( [ 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 ] )
colors = [ 'red' , 'black' ]
labels = [ 'Zero' , 'One' ]
for i in range ( X. shape[ 1 ] ) :
plt. scatter( X[ y== i, 0 ] ,
X[ y== i, 1 ] ,
c= colors[ i] ,
label = labels[ i] )
plt. legend( )
plt. show( )
midwest = pd. read_csv( 'https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv' )
categories = np. unique( midwest[ 'category' ] )
从光谱中选取一个颜色
color1 = plt. cm. tab10( 5.2 )
np. array( color1) . reshape( 1 , - 1 )
绘制复杂图
plt. figure( figsize= ( 16 , 10 ) )
for i in range ( len ( categories) ) :
plt. scatter( midwest. loc[ midwest[ 'category' ] == categories[ i] , 'area' ] ,
midwest. loc[ midwest[ 'category' ] == categories[ i] , 'poptotal' ] ,
s= 20 ,
c= np. array( plt. cm. tab10( i/ len ( categories) ) ) . reshape( 1 , - 1 ) ,
label= categories[ i] )
plt. gca( ) . set ( xlim= ( 0.0 , 0.12 ) , ylim= ( 0 , 80000 ) )
plt. xticks( fontsize= 12 )
plt. yticks( fontsize= 12 )
plt. ylabel( 'Population' , fontsize= 22 )
plt. xlabel( 'Area' , fontsize= 22 )
plt. title( 'Scatterplot of Midwest Area vs Population' , fontsize= 22 )
plt. legend( fontsize= 12 )
plt. show( )
排序图:柱状图vlines
X = [ 1 , 2 , 3 , 4 , 5 ]
y = [ 14 , 13 , 20 , 19 , 7 ]
plt. vlines( x= X, ymin= 0 , ymax= y)
<matplotlib.collections.LineCollection at 0x7f1c94724750>
plt. vlines( x= X, ymin= 0 , ymax= y,
linestyles= 'solid' ,
linewidth = 10 ,
colors = 'red' )
<matplotlib.collections.LineCollection at 0x7f1c934d44d0>
面向对象的matplotlib画图
命令式绘图和面向对象的绘图可以混用,不会报错(即plt和ax可以混用)
fig, ax = plt. subplots( figsize= ( 10 , 6 ) , facecolor= 'white' , dpi= 80 )
ax. vlines( x= df. index, ymin= 0 , ymax= df. cty, color= 'firebrick' , alpha= 0.7 , linewidth= 15 )