导包
from matplotlib import pyplot as plt
from mxnet import nd, gluon;
x , y 接收的类型为list 或 numpy;plt.plot可以写多个,那就显示在同一个图上了
1. 折线图
def print ( x, y, xlabel= 'x/y_val' ,ylabel= 'x/y_label' ,title= 'TEST 2019/9/17' , figsize= ( 3.5 , 2.5 ) ) :
plt. figure( figsize= figsize) ;
plt. plot( x, y, c= 'red' , label= 'TEST01' ) ;
plt. xlabel( xlabel) ;
plt. ylabel( ylabel) ;
plt. title( title) ;
plt. legend( loc= 'best' )
plt. grid( color= 'r' , linestyle= '-.' ) ;
plt. savefig( 'zhifang.png' )
plt. show( ) ;
r= list ( range ( 100 ) ) ;
v= nd. power( nd. array( r) , 3 ) + 100 ;
pp( r, v. asnumpy( ) )
2.水平条形图
import matplotlib. pyplot as plt
import matplotlib
"""
绘制水平条形图方法barh
参数一:y轴
参数二:x轴
"""
def printy ( data, names, xlabel= "价格" , title= "不同平台图书价格" ) :
plt. barh( range ( len ( names) ) , price, height= 0.7 , color= 'steelblue' , alpha= 0.8 )
plt. yticks( range ( len ( names) ) , names)
plt. xlim( 30 , 47 )
plt. xlabel( xlabel)
plt. title( title)
for x, y in enumerate ( data) :
plt. text( y + 0.2 , x - 0.1 , '%s' % y) ;
plt. savefig( 'tu.png' )
plt. show( )
matplotlib. rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
matplotlib. rcParams[ 'axes.unicode_minus' ] = False
price = [ 39.5 , 39.9 , 45.4 , 38.9 , 33.34 ]
names= [ '亚马逊' , '当当网' , '中国图书网' , '京东' , '天猫' ]
printy( price, names)
3.饼状图
import matplotlib. pyplot as plt
import matplotlib
matplotlib. rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
matplotlib. rcParams[ 'axes.unicode_minus' ] = False
def printy ( size, label_list = [ "第一部分" , "第二部分" , "第三部分" ] ,
color = [ "red" , "green" , "blue" ] , explode = [ 0.05 , 0 , 0 ] ) :
patches, l_text, p_text = plt. pie( size, explode= explode, colors= color, labels= label_list, labeldistance= 1.1 , autopct= "%1.1f%%" , shadow= False , startangle= 90 , pctdistance= 0.6 )
plt. axis( "equal" )
plt. legend( )
plt. savefig( 'pip.png' )
plt. show( )
size = [ 55 , 35 , 10 ]
printy( size)
4.柱形图
import matplotlib. pyplot as plt
import matplotlib
import random
def printy ( y1_data, y2_data, label1= 'legend1' , label2= 'legend2' ,
x_data = ( 'A' , 'B' , 'C' , 'D' , 'E' ) , bar_width = 0.35 ) :
plt. figure( 3 ) ;
x_index = np. arange( len ( y1_data) )
rects1 = plt. bar( x_index, y1_data, width= bar_width, alpha= 0.4 , color= 'b' , label= label1)
rects2 = plt. bar( x_index + bar_width, y2_data, width= bar_width, alpha= 0.5 , color= 'r' , label= label2)
plt. xticks( x_index + bar_width/ 2 , x_data)
plt. legend( )
plt. tight_layout( )
for a, b in enumerate ( y1_data) :
plt. text( a, b+ 0.05 , '%.0f' % b, ha= 'center' , va= 'bottom' , fontsize= 14 ) ;
for a, b in enumerate ( y2_data) :
plt. text( a+ bar_width, b, '%.0f' % b, ha= 'center' , va= 'bottom' , fontsize= 14 ) ;
plt. savefig( 'shuang.png' )
plt. show( )
x_data = ( 'A' , 'B' , 'C' , 'D' , 'E' )
y1_data = list ( [ random. randint( 12 , 60 ) for i in range ( 5 ) ] )
y2_data = list ( [ random. randint( 16 , 70 ) for i in range ( 5 ) ] ) ;
printy( y1_data, y2_data, x_data= x_data)
5.散点图
import numpy as np
import matplotlib. pyplot as plt
def printy ( x, y, title= '散点图' ) :
plt. scatter( x, y) ;
plt. title( title)
plt. savefig( 'san.png' )
plt. show( )
N = 1000
x = np. random. randn( N)
y = np. random. randn( N) ;
printy( x, y)
6.三维图
import numpy as np
import matplotlib. pyplot as plt
import mpl_toolkits. mplot3d
def printy ( x, y, xl= 'x' , yl= 'y' , zl= 'z' ) :
ax = plt. subplot( 111 , projection = '3d' )
ax. plot_surface( x, y, z, rstride = 2 , cstride = 1 , cmap = plt. cm. Blues_r) ;
ax. set_xlabel( xl)
ax. set_xlabel( yl)
ax. set_xlabel( zl) ;
plt. savefig( 'df.png' )
plt. show( )
x, y = np. mgrid[ - 2 : 2 : 20j , - 2 : 2 : 20j ] ;
z = 50 * ( x+ y)
printy( x, y, z)