首页 > 编程知识 正文

qpython,python sns

时间:2023-05-04 07:42:17 阅读:41170 作者:302

1 .参数明细seaborn.heatmap

seaborn.heat map (数据,vmin=None,vmax=None,cmap=None,center=None,robust=False,annot=None,fmt=' .

data :矩阵数据集对应于numpy的数组(array ),对于pandas的数据帧,df的索引/列信息分别对应于heatmap的列、rowsvmax和vmin annotate的缩写,annot默认为False,如果annot为True,则heatmap将数据写入各方眼睛。 如果annot_kws和annot为True,则可以设置大小、颜色、粗体和斜体等参数。

SnS.heatmap(x,annot=True,ax=ax2,annot_kws={'size':9,' weight':'bold ',' color ' : ) matplotlib自定义颜色栏颜色栏-和matplorbar

口罩,口罩

使用有两个小技巧

(1)首先通过SNS.set(font_scale )修改字体比例:

SNS.set(font_scale=1.5 )2)在plt.rc中统一修改所有图字体:

PLT.RC(font )、family='Times New Roman )、size=12 )2.颜色效果cmap的参数如下:

Accent,Accent_r,Blues,Blues_r,BrBG_r,BuGn,BuGn_r,BuPu,BuPu_r,CMRmap_r,BuGn OrRd_r,cbar,Oranges_r Pastel1_r,Pastel2_r,PiYG,PiYG_r,PuBu,PuBuGn,PPP RdYlBu_r,RdYlGn,RdYlGn_r,Reds,Reds_r,Set1,Set1_r,Set2,set23http://www.Sina.com/3http://www.Sina YlGn 3358www.Sina.com/,YlOrRd_r,afmhot,afmhot_r,cbar_kws,autumn_r,binary,binary_r copper_r、cubehelix、cubehelix_r gist_gray_r、gist_heat、gist_heat_r、gist_ncar、gist_ncar_r

, gray_r, hot, hot_r(红黄), hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_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, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r

示范如下:

cmap="YlGnBu":数字越大,颜色越深

cmap="YlGnBu_r":数字越大,颜色越浅


 cmap="hot":黄色到红色,数字越大,颜色越浅

  cmap="hot_r":红色到黄色,数字越大,颜色越深

   cmap="OrRd":深红色到浅红色,类似“Oranges”。

  cmap="autumn":黄色到红色

cmap="greens":绿色,数字越大,颜色越深

 

 cmap="viridis":黄到蓝

cmap="greys":灰色

cmap="Purples":紫色

 

 cmap="rainbow":彩虹色

cmap="gist_rainbow":彩虹色

将colormap置于特定值的中心(参考链接):

>>> ax = sns.heatmap(flights, center=flights.loc["January", 1955])

 

使用遮罩绘制矩阵中的一部分

>>> corr = np.corrcoef(np.random.randn(10, 200))>>> mask = np.zeros_like(corr)>>> mask[np.triu_indices_from(mask)] = True>>> with sns.axes_style("white"):... ax = sns.heatmap(corr, mask=mask, vmax=.3, square=True)

 np.zeros_like() 返回一个零数组,其形状和类型与给定的数组相同;

np.triu_indices_from(mask) 返回数组上三角形的索引。

 

以下是一些网络上发现的配色好看的图:

原文链接: https://www.php.cn/python-tutorials-391565.html

# cmap用cubehelix map颜色

cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True)

sns.heatmap(pt, linewidths = 0.05, ax = ax1, vmax=900, vmin=0, cmap=cmap)

# cmap用matplotlib colormap

sns.heatmap(pt, linewidths = 0.05, ax = ax2, vmax=900, vmin=0, cmap='rainbow')

#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 )

 

#设置x轴图例为空值ax1.set_ylabel('kind')

# 当center设置小于数据的均值时,生成的图片颜色要向0值代表的颜色一段偏移

sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=200)    

 #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 )

sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=None, robust=True ) 

 #mask对某些矩阵块的显示进行覆盖

p2 = sns.heatmap(pt, ax=ax2, cmap=cmap, xticklabels=True, mask=(pt<800))  

#mask对pt进行布尔型转化,结果为True的位置用白色覆盖

用mask实现:突出显示某些数据

sns.heatmap(x, mask=x < 1, ax=ax2, annot=True, annot_kws={"weight": "bold"})   #把小于1的区域覆盖掉

  3. 个性化设置

个性化设置参考:Python - Seaborn可视化:图形个性化设置的几个小技巧

将x轴刻度放置在top位置的几种方法

# 将x轴刻度放置在top位置的几种方法# ax.xaxis.set_ticks_position(‘top‘)ax.xaxis.tick_top()# ax.tick_params(axis=‘x‘,labelsize=6, colors=‘b‘, labeltop=True, labelbottom=False)

设置坐标轴刻度参数,”axis”不写的时候,默认是x轴和y轴的参数同时调整。

# 设置坐标轴刻度的字体大小# matplotlib.axes.Axes.tick_paramsax.tick_params(axis=‘y‘,labelsize=8) # y轴

旋转轴刻度上文字方向的两种方法

# 旋转轴刻度上文字方向的两种方法ax.set_xticklabels(ax.get_xticklabels(), rotation=-90)# ax.set_xticklabels(corr.index, rotation=90)

更多请见:

设置Matplotlib颜色条大小以匹配图形(Set Matplotlib colorbar size to match graph)

seaborn基本使用

 

4. 代码备份 '''深入挖掘'''font = {'family': 'Times New Roman', 'size': 12, }sns.set(font_scale=1.2)plt.rc('font',family='Times New Roman')fig = plt.figure(figsize = (16, 12))ax1=fig.add_subplot(2,1,1)cor = SubShowFeatures01.corr()mask = np.zeros_like(cor)for i in range(len(mask)): for j in range(i+1, len(mask[0])): mask[i][j] = Truesns.heatmap(cor,linewidths = 0.05, ax=ax1, mask=mask, annot=True, annot_kws=font, vmax=1.0, vmin=-1.0, cmap='YlGnBu', center=0.5, cbar=True, robust=False)ax1.set_title('User features - 01', fontdict=font)ax2=fig.add_subplot(2,1,2)cor = SubShowFeatures02.corr()mask = np.zeros_like(cor)for i in range(len(mask)): for j in range(i+1, len(mask[0])): mask[i][j] = Truesns.heatmap(cor,linewidths = 0.05, ax=ax2, mask=mask, annot=True, annot_kws=font, vmax=1.0, vmin=-1.0, cmap='YlOrRd', center=0)ax2.set_title('User features - 02', fontdict=font)plt.show()

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。