参数
(1)显示中文
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.figure(figsize=(10,6),facecolor(背景颜色)='#ffffcc',edgecolor(边框颜色)='#ffffcc',dpi=None(设置分辨率)) 放最前面
一个比较重要的子图写法:
plt.subplot()
参数:projection选择:'aitoff', 'hammer', 'lambert', 'mollweide', 'polar', 'rectilinear'
其中一般默认是None(线形图),另一个常用的是ploar。
常用的方式
plt.plot(x, y2, 'r--'(r表示颜色red,--表示线的类型,'--*'线的两端以*结尾,'bo-'表示蓝色圆点实线))顺序就是color,marker,linetype
plot(x,y2,color='green', marker='o', linestyle='dashed', linewidth=1, markersize=6)
marker表示线条两端的符号。
setp参数
plt.setp(ax1.get_xticklabels(), visible=False) # 获得当前图像x轴,设置不可见
坐标轴参数
plt.xticks([0, 100, 200, 300, 400, 500, 600, 700])
plt.tick_params(labelsize=13)
plt.xlabel([0, 100, 200, 300, 400, 500, 600, 700])
plt.xlabel('',fontsize=20(字体大小))
# 设置图例字体大小
ax.legend(..., fontsize=20)
其他的参数,family设置字体,size设置大小
plt.grid(axis='y',color='#8f8f8f',linestyle='--', linewidth=1 )显示网格
plt.xlim([-10,15]) #指X轴的显示范围
plt.ylim([-100,3000]) #指Y轴的显示范围
plt.title('标题', color='blue')
plt.xlabel('x 轴标签', color='blue')
plt.ylabel('y 轴标签', color='blue')
plt.text((x.max()+x.min())/2,(y2.max()+y2.min())/2,'show text',ha='center')#图像上显示文本信息
for a,b in zip(x, y2): plt.text(a, b, str(b)) #plt.text 显示y值
plt.legend(loc=2)#图像标签说明,设置label的都显示(loc=2位于第二个角洛)
#plt.legend(handles=[doc], loc=2) #只显示一个图的标签
#plt.xticks((-4,-2,0,2,4,6,8,10),(-4,-2,0,2,4,6,8,10))#替换x轴刻度值
plt.yticks((0,500,1000,1500,2000),('0','0.5k','1k','1.5k','2k'))#替换y轴刻度值
plt.axis('tight') #tight:坐标轴数据显示更明细(有不同选项)
plt.show() #显示图像
热力图矩阵块颜色参数
#cmap(颜色) import matplotlib.pyplot as pltimport seaborn as sns% matplotlib inline f, (ax1,ax2) = plt.subplots(figsize = (6,4),nrows=2) # cmap用cubehelix map颜色cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True)pt = df.corr() # pt为数据框或者是协方差矩阵sns.heatmap(pt, linewidths = 0.05, ax = ax1, vmax=900, vmin=0, cmap=cmap)ax1.set_title('cubehelix map')ax1.set_xlabel('')ax1.set_xticklabels([]) #设置x轴图例为空值ax1.set_ylabel('kind') # cmap用matplotlib colormapsns.heatmap(pt, linewidths = 0.05, ax = ax2, vmax=900, vmin=0, cmap='rainbow') # rainbow为 matplotlib 的colormap名称ax2.set_title('matplotlib colormap')ax2.set_xlabel('region')ax2.set_ylabel('kind')
#center的用法(颜色) f, (ax1,ax2) = plt.subplots(figsize = (6, 4),nrows=2) cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True)sns.heatmap(pt, linewidths = 0.05, ax = ax1, cmap=cmap, center=None )ax1.set_title('center=None')ax1.set_xlabel('')ax1.set_xticklabels([]) #设置x轴图例为空值ax1.set_ylabel('kind') # 当center设置小于数据的均值时,生成的图片颜色要向0值代表的颜色一段偏移sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=200) ax2.set_title('center=3000')ax2.set_xlabel('region')ax2.set_ylabel('kind')
#robust的用法(颜色) f, (ax1,ax2) = plt.subplots(figsize = (6,4),nrows=2) cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True) sns.heatmap(pt, linewidths = 0.05, ax = ax1, cmap=cmap, center=None, robust=False )ax1.set_title('robust=False')ax1.set_xlabel('')ax1.set_xticklabels([]) #设置x轴图例为空值ax1.set_ylabel('kind') sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=None, robust=True ) ax2.set_title('robust=True')ax2.set_xlabel('region')ax2.set_ylabel('kind')
热力图矩阵块注释参数
#annot(矩阵上数字),annot_kws(矩阵上数字的大小颜色字体)matplotlib包text类下的字体设置 import numpy as npnp.random.seed(20180316)x = np.random.randn(4, 4) f, (ax1, ax2) = plt.subplots(figsize=(6,6),nrows=2) sns.heatmap(x, annot=True, ax=ax1) sns.heatmap(x, annot=True, ax=ax2, annot_kws={'size':9,'weight':'bold', 'color':'blue'})# Keyword arguments for ax.text when annot is True. http://stackoverflow.com/questions/35024475/seaborn-heatmap-key-words
#fmt(字符串格式代码,矩阵上标识数字的数据格式,比如保留小数点后几位数字) import numpy as npnp.random.seed(0)x = np.random.randn(4,4) f, (ax1, ax2) = plt.subplots(figsize=(6,6),nrows=2) sns.heatmap(x, annot=True, ax=ax1) sns.heatmap(x, annot=True, fmt='.1f', ax=ax2)
热力图矩阵块之间间隔及间隔线参数
#linewidths(矩阵小块的间隔),linecolor(切分热力图矩阵小块的线的颜色) import matplotlib.pyplot as pltf, ax = plt.subplots(figsize = (6,4))cmap = sns.cubehelix_palette(start = 1, rot = 3, gamma=0.8, as_cmap = True) sns.heatmap(pt, cmap = cmap, linewidths = 0.05, linecolor= 'red', ax = ax) ax.set_title('Amounts per kind and region')ax.set_xlabel('region')ax.set_ylabel('kind')
#xticklabels,yticklabels横轴和纵轴的标签名输出 import matplotlib.pyplot as pltf, (ax1,ax2) = plt.subplots(figsize = (5,5),nrows=2) cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True) p1 = sns.heatmap(pt, ax=ax1, cmap=cmap, center=None, xticklabels=False)ax1.set_title('xticklabels=None',fontsize=8) p2 = sns.heatmap(pt, ax=ax2, cmap=cmap, center=None, xticklabels=2, yticklabels=list(range(5))) ax2.set_title('xticklabels=2, yticklabels is a list',fontsize=8)ax2.set_xlabel('region')
#mask对某些矩阵块的显示进行覆盖 f, (ax1,ax2) = plt.subplots(figsize = (5,5),nrows=2) cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True) p1 = sns.heatmap(pt, ax=ax1, cmap=cmap, xticklabels=False, mask=None)ax1.set_title('mask=None')ax1.set_ylabel('kind') p2 = sns.heatmap(pt, ax=ax2, cmap=cmap, xticklabels=True, mask=(pt<800)) #mask对pt进行布尔型转化,结果为True的位置用白色覆盖ax2.set_title('mask: boolean DataFrame')ax2.set_xlabel('region')ax2.set_ylabel('kind')
用mask实现:突出显示某些数据
f,(ax1,ax2) = plt.subplots(figsize=(4,6),nrows=2)x = np.array([[1,2,3],[2,0,1],[-1,-2,0]])sns.heatmap(x, annot=True, ax=ax1)sns.heatmap(x, mask=x < 1, ax=ax2, annot=True, annot_kws={"weight": "bold"}) #把小于1的区域覆盖掉
参数
plt.xlim([0,12]) # x轴边界
plt.ylim([0,1.5]) # y轴边界
plt.xticks(range(12)) # 设置x刻度
plt.yticks([0,0.2,0.4,0.6,0.8,1.0,1.2]) # 设置y刻度
fig.set_xticklabels("%.1f" %i for i in range(12)) #x轴刻度标签
fig.set_yticklabels("%.1f" %i for i in [0,0.2,0.4,0.6,0.8,1.0,1.2]) #y轴刻度标签
plt.grid(True,linestyle = "--",color = 'gray' ,linewidth = '0.5',axis='both')
分面
g = sns.FacetGrid(pd_iris, col='class')
g = sns.FacetGrid(pd_iris1,row='flowering',col='class',height=4)
python画小提琴图
import seaborn as sns
help(sns.violinplot)
###我们来看看参数
x, y, hue :注意的是hue,按照hue进行分组。
order :指x轴的排列顺序、
bw 控制拟合程度,一般不设置(图不好看的时候多调整一下)
cut 参数,类似于ggplot的trim = True
scale 参数:测度小提琴图的宽度: area-面积相同,count-按照样本数量决定宽度,width-宽度一样
gridsize :控制小提琴图的边界的锚点,越多越平滑,一般不设置。
ax 参数,这个和大多数一样
网友评论