matplotlib 绘图
导入模块
import matplotlib.pyplot as plt
import numpy as np
一.画布参数
1.修改字体
plt.rcParams[ 'font.sans-serif' ] = 'SimHei'
2.修改符号显示
plt.rcParams[ 'axes.unicode_minus' ] = False
3.修正背景颜色
plt.rcParams[ 'axes.facecolor' ] = '#0D0434'
4.更改轴的颜色
plt.rcParams[ 'axes.edgecolor' ] = 'red'
5.更改刻度字体的颜色
plt.rcPrarms[ 'xticl.color' ] = 'white'
plt.rcParams[ 'ytick.color' ] = 'white'
6.字体更改
font= {
'family' : 'SimHei' ,
'weight' : 'normal' ,
'size' :16,
'color' : 'white'
}
二. 框架
1.创建画布
plt.figure( facecolor= 'black' ,figsize= ( 8,6))
2.指定 x,y 值
x= np.array( [ 1,2,3,4,5] )
y= np.array( [ 0.2,3,0.8,1,5] )
x= np.arange( 0,2*np.pi,0.2)
y= np.sin( x)
3.绘制图形:散点图
plt.scatter( x,y,s= 60,c= x,cmap= 'rainbow' ,alpha= 0.8,edgecolor= 'yellow' ,linewidths= 1.2)
好看的颜色
'' '' 好看的颜色: Accent', ' Accent_r', ' Blues', ' Blues_r', ' BrBG', ' BrBG_r', ' BuGn', ' BuGn_r', ' BuPu', ' BuPu_r', ' CMRmap', ' CMRmap_r', ' Dark2', ' Dark2_r', ' GnBu', ' GnBu_r', ' Greens', ' Greens_r', ' Greys', ' Greys_r', ' OrRd', ' OrRd_r', ' Oranges', ' Oranges_r', ' PRGn', ' PRGn_r', ' Paired', ' Paired_r', ' Pastel1', ' Pastel1_r', ' Pastel2', ' Pastel2_r', ' PiYG', ' PiYG_r', ' PuBu', ' PuBuGn', ' PuBuGn_r', ' PuBu_r', ' PuOr', ' PuOr_r', ' PuRd', ' PuRd_r', ' Purples', ' Purples_r', ' RdBu', ' RdBu_r', ' RdGy', ' RdGy_r', ' RdPu', ' RdPu_r', ' RdYlBu', ' RdYlBu_r', ' RdYlGn', ' RdYlGn_r', ' Reds', ' Reds_r', ' Set1', ' Set1_r', ' Set2', ' Set2_r', ' Set3', ' Set3_r', ' Spectral', ' Spectral_r', ' Wistia', ' Wistia_r', ' YlGn', ' YlGnBu', ' YlGnBu_r', ' YlGn_r', ' YlOrBr', ' YlOrBr_r', ' YlOrRd', ' YlOrRd_r', ' afmhot', ' afmhot_r', ' autumn', ' autumn_r', ' binary', ' binary_r', ' bone', ' bone_r', ' brg', ' brg_r', ' bwr', ' bwr_r', ' cividis', ' cividis_r', ' cool', ' cool_r', ' coolwarm', ' coolwarm_r', ' copper', ' copper_r', ' cubehelix', ' cubehelix_r', ' flag', ' flag_r', ' gist_earth', ' gist_earth_r', ' gist_gray', ' gist_gray_r', ' gist_heat', ' gist_heat_r', ' gist_ncar', ' gist_ncar_r', ' gist_rainbow', ' gist_rainbow_r', ' gist_stern', ' gist_stern_r', ' gist_yarg', ' gist_yarg_r', ' gnuplot', ' gnuplot2', ' gnuplot2_r', ' gnuplot_r', ' gray', ' gray_r', ' hot', ' hot_r', ' hsv', ' hsv_r', ' inferno', ' inferno_r', ' jet', ' jet_r', ' magma', ' magma_r', ' nipy_spectral', ' nipy_spectral_r', ' ocean', ' ocean_r', ' pink', ' pink_r', ' plasma', ' plasma_r', ' prism', ' prism_r', ' rainbow', ' rainbow_r', ' seismic', ' seismic_r', ' spring', ' spring_r', ' summer', ' summer_r', ' tab10', ' tab10_r', ' tab20', ' tab20_r', ' tab20b', ' tab20b_r', ' tab20c', ' tab20c_r', ' terrain', ' terrain_r', ' turbo', ' turbo_r', ' twilight', ' twilight_r', ' twilight_shifted', ' twilight_shifted_r', ' viridis', ' viridis_r', ' winter', ' winter_r'
' ''
图形美化
(1)坐标轴名称
plt.xlabel( 'x轴' ,fontdict= font)
plt.ylabel( 'y轴' ,rotation= 0,fontdict= font)
(2)设置显示刻度
plt.xticks( [ 1,2,3,4,5,6,7,8] )
plt.yticks( [ -1,0,1] )
(3)添加标题
plt.title( 'scatter' ,fontdict= font)
plt.suptitle( 'ooo' )
(4)截取图像的某块区域并放大散点
plt.xlim(( 1 , 3 ))
plt.ylim(( 0 , 1 ))
4.保存图像
plt.savefig( 'scatter.jpg' )
5.显示图像
plt.show( )