bar图设置距离 python_python画图设置坐标轴的位置及角度及设置colorbar
用python畫圖
設置y軸在右邊顯示
f, ax = plt.subplots(figsize = (14, 10))
sns.heatmap(corr,cmap='RdBu', linewidths = 0.05, ax = ax)
ax.set_title('Correlation between features', fontsize=18, position=(0.5,1.05))
將y軸或者x軸進行逆序
ax.invert_yaxis()
ax.invert_xaxis()
ax.set_xlabel('X Label',fontsize=10)
設置Y軸標簽的字體大小和字體顏色
ax.set_ylabel('Y Label',fontsize=15, color='r')
設置坐標軸刻度的字體大小
matplotlib.axes.Axes.tick_params
ax.tick_params(axis='y',labelsize=8) # y軸
ax.tick_params(axis='x',labelsize=6, colors='b', labeltop=True, labelbottom=False) # x軸
將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) # x軸
修改tick的字體顏色
ax.tick_params(axis='x', colors='b') # x軸
旋轉軸刻度上文字方向的兩種方法
ax.set_xticklabels(ax.get_xticklabels(), rotation=-90)
ax.set_xticklabels(corr.index, rotation=90)
單獨設置y軸或者x軸刻度的字體大小, 調整字體方向
ax.set_yticklabels(ax.get_yticklabels(),fontsize=6)
ax.set_xticklabels(ax.get_xticklabels(), rotation=-90)
旋轉軸刻度上文字方向的兩種方法
ax.set_xticklabels(ax.get_xticklabels(), rotation=-90)
ax.set_xticklabels(corr.index, rotation=90)
將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)import osimport matplotlib.pyplot as pltimport pandas as pdimport numpy as npimport mathimport seaborn as snsimport matplotlib.gridspec as mgfrom sklearn import preprocessingos.chdir('C:/Users/86178/Desktop')x = pd.read_table('TME_Sender.csv',index_col=20,sep = ',')x.iloc[:,0:20] = preprocessing.scale(x.iloc[:,0:20])y = pd.read_table('ligand_receptor_matrix.txt',sep = '\t',index_col = 0)z = pd.read_csv('TSK_receiver.csv',index_col= 0)z.iloc[:,0:22] = preprocessing.scale(z.iloc[:,0:22])z = z.Tgs = mg.GridSpec(5, 5)plt.subplot(gs[0:4,1:4])zz = sns.heatmap(y,cmap='PuRd',linewidths= 1,yticklabels=False,cbar = False)zz.xaxis.set_ticks_position('top')zz.set_ylabel('')zz.set_xticklabels(zz.get_xticklabels(),rotation = 90,family = 'Times New Roman')plt.subplot(gs[4,1:4])a = sns.heatmap(x,cmap='bwr',linewidths= 1,xticklabels=False,cbar = False)a.yaxis.set_ticks_position('right')a.set_yticklabels(a.get_yticklabels(), rotation=0,family = 'Times New Roman')a.set_xticklabels(a.get_xticklabels(),family = 'Times New Roman')plt.ylabel('')plt.subplot(gs[:4,0])x = sns.heatmap(z,cbar = False,cmap = 'bwr')x.xaxis.set_ticks_position('top')x.set_xticklabels(x.get_xticklabels(),family = 'Times New Roman',rotation = 90)x.set_yticklabels(x.get_yticklabels(),family = 'Times New Roman')x.set_xlabel('')#x.xaxis.set_ticks_position('top')plt.show()
圖片1.png
python設置colorbar
自己設置colorbar包含兩方面:自己設置colorbar的顏色組合及顏色占比自己設置colorbar的位置和大小
這兩項比較簡單和實用,matplotlib和seaborn都可以嘗試。對于某些特殊的數據分布類型,想在一張圖內顯示的情況比較適合。
cmap的自己設置
cmap本質是一個RGBA格式的顏色列表,元素類型為np.array() ,np.array()里包含4個0-1的元素,前3個是RGB值,第4個為透明度。
seaborn取顏色列表可以用以下方式:sns.light_palette('blue',reverse=True,n_colors=5)plt.cm.get_cmap('Blues', 5)plt.cm.get_cmap('cubehelix', 5)
假如數據中有兩組相差比較大的數據構成,可考慮取兩組顏色值合并,可通過n_colors參數控制兩組顏色的占比,假如存在極值,極值能設置為特殊顏色。
colorbar的位置和大小
可以把colorbar作為單獨的axes,自由地定義其位置和占圖比例,例如colorbar可以這樣設置:cbar_ax = fig.add_axes([0.7, 0.75, 0.025, 0.2]),在seaborn熱圖中有對應的參數接受自己設置的colorbar。#!/usr/bin/env python# coding: utf-8 -*- import pandas as pdimport numpy as np## 以下為MACOS設置,linux請改為 ?matplotlib.use('Agg')matplotlib.use('TkAgg')## juypter notebook顯示圖像設置%matplotlib inlineimport matplotlib.pyplot as pltimport seaborn as snscmap= sns.light_palette('blue',reverse=True,n_colors=5)cmap2=sns.light_palette('red',reverse=False,n_colors=15)cmap.extend(cmap2)cmap.append(np.array([.3,.7,.6,1]))cmap.insert(0,np.array([.7,.7,.5,1]))fig = plt.figure(figsize=(4,7))ax = fig.add_axes([0.38, 0.3, 0.3, 0.65], facecolor = 'white')cbar_ax = fig.add_axes([0.7, 0.75, 0.025, 0.2])df = pd.DataFrame(np.random.rand(12,5))ax = sns.heatmap(df, ax=ax,annot=False, cmap=cmap, linewidths=.5, cbar_ax = cbar_ax)
下圖的效果比照更顯著
圖片.png
畫圖時候marker參數的設置
marker type 含義
“.” point 點
“,” pixel 像素
“o” circle 圓
“v” triangle_down 下三角
“^” triangle_up 上三角
“
“>” triangle_right 右三角
“1” tri_down 相似奔馳的標志
“2” tri_up 相似奔馳的標志
“3” tri_left 相似奔馳的標志
“4” tri_right 相似奔馳的標志
“8” octagon 八角形
“s” square 正方形
“p” pentagon 五角星
“*” star 星號
“h” hexagon1 六邊形1
“H” hexagon2 六邊形2
“+” plus 加號
“x” x x
“D” diamond 鉆石
“d” thin_diamond 細的鉆石
“ “ vline
“-“ hline 水平方向的線
“TICKLEFT” octagon 像素
去掉刻度線
plt.tick_params(bottom=False,top=False,left=False,right=False)
總結
以上是生活随笔為你收集整理的bar图设置距离 python_python画图设置坐标轴的位置及角度及设置colorbar的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 「已解决」锰铜分流器是什么
- 下一篇: python restful 框架_re