使用Matplotlib绘制常用的折线图、纵向柱状图、横向柱状图和散点图
import matplotlib. pyplot as plt
import pandas as pd
unrate = pd. read_csv( 'UNRATE.csv' )
x = unrate[ 'DATE' ] [ 0 : 12 ]
y = unrate[ 'VALUE' ] [ 0 : 12 ]
unrate. head( 12 )
plt. figure( figsize= ( 6 , 5 ) , dpi = 100 )
plt. plot( x, y, linestyle = '-' , linewidth = 1 , color = 'r' , label = '$1948$' )
plt. title( 'The unemployment rate of USA' )
plt. xlabel( 'Month' )
plt. ylabel( 'Unrate' )
plt. xlim( 0 , 12 )
plt. xticks( rotation = 60 )
plt. ylim( 0 , 5 )
plt. legend( )
plt. show( )
import numpy as np
import matplotlib. pyplot as plt
a = np. random. random( 8 )
b = np. random. random( 8 )
c = np. random. random( 8 )
x = np. arange( 0 , 8 )
total_width = 0.8
per_width = total_width/ 3
x_position = x - ( total_width - per_width) / 2
plt. figure( figsize= ( 8 , 5 ) )
plt. bar( x_position, a, width= per_width, label = 'a' , color = 'blue' )
plt. bar( x_position + per_width, b, width = per_width, label = 'b' , color = 'red' )
plt. bar( x_position + 2 * per_width, c, width= per_width, label = 'c' , color = 'orange' )
plt. legend( )
plt. show( )
import numpy as np
import matplotlib. pyplot as plt
bar_value = np. random. random( 5 )
x_position = np. arange( 5 ) + 1
plt. barh( x_position, bar_value, height= 0.3 , color = 'r' )
plt. xlabel( 'RANDOM' )
Text(0.5, 0, 'RANDOM')
import numpy as np
import matplotlib. pyplot as plt
x = np. random. random( 1000 )
y = np. random. random( 1000 )
plt. scatter( x, y, color = 'r' , alpha = 0.5 )
plt. xlim( 0 , 1 )
plt. ylim( 0 , 1 )
plt. xlabel( 'Scatter Image' , labelpad= 10 )
Text(0.5, 0, 'Scatter Image')