2015-11-19 167 views
2

一個彩條創建3個蟒蛇地塊與colorbar()使用下面的代碼在不同的文件夾不同的數據:創建一個熱圖沒有matplotlib

import numpy as np 
from matplotlib import pyplot as pp 
pp.rcParams.update({'figure.autolayout': True}) 

data=np.loadtxt("performance.dat") 
A=np.reshape(data, (21,21,4)) 
x=A[:,:,0] 
y=A[:,:,1] 
rn=A[:,:,3] 

f=pp.figure() 
ax=f.add_subplot(111,) 
fs=20 
for tick in ax.axes.xaxis.get_major_ticks(): 
      tick.label.set_fontsize(fs) 
for tick in ax.yaxis.get_major_ticks(): 
      tick.label.set_fontsize(fs) 

pp.pcolor(np.log10(x),np.log10(y),rn) 
pp.clim(0,2) 
pp.xlabel(r"$\log \, \sigma_1$", size=28) 
pp.ylabel(r"$\log \, \sigma_2$",size=28) 
pp.colorbar().ax.tick_params(labelsize=20) 
pp.show() 

然後我把它插入我的LaTeX的文件並排着:

\begin{figure}[H] 

\makebox[\linewidth][c]{ 
\begin{subfigure}[b]{.4\textwidth} 
    \centering 
    \includegraphics[width=.95\textwidth, bb = -0 -0 530 420]{m200_l10_p90}%-0 -0 588 444 
\end{subfigure} 
% 
\begin{subfigure}[b]{.4\textwidth} 
    \centering 
    \includegraphics[width=.95\textwidth, bb = -0 -0 530 420]{m200_l10_p95}% 
\end{subfigure} 
% 
\begin{subfigure}[b]{.4\textwidth} 
    \centering 
    \includegraphics[width=.95\textwidth, bb = -0 -0 530 420]{m200_l10_P99} 
\end{subfigure} 
} 
\caption{bla} 
\label{fig:bla} 
\end{figure} 

我得到:

LaTeX output

很明顯,這看起來不太好,我玩過LaTeX,直到現在,這是我能夠獲得的最好的,以使它可讀。我的想法可能是創建一個只顯示垂直顏色條(最後一個圖)的圖,第一個圖只顯示「y標籤」,而第二個圖顯示「x標籤」,看起來會更好。

我的問題是如何創建一個沒有顯示欄的顏色條圖? 我試圖評論pp.colorbar().ax.tick_params(labelsize=20)一行,但這搞砸了陰謀。

情節仍然需要更大的標籤和蜱字體大小。

+0

你可以考慮一個替代方案是PGFplots,這將直接在LaTeX的創建地塊。它包含用於在共享軸的多個繪圖時省略軸標籤的內置工具。但這超出了這個問題的範圍。 –

+0

謝謝@DavidZ,從來沒有聽說過。但我真的更喜歡直接的Python解決方案!感謝您的編輯 – lailaw

+1

此外,我試圖創建這種類型的情節沒有'colorbar'調用,它工作正常。您能否詳細說明在嘗試刪除該行時出了什麼問題? –

回答

3

您可以從不同的目錄與genfromtxt通過給予genfromtxt路徑讓您的數據:

np.genfromtxt('/path/to/performance.dat') 

您可以創建的所有3個熱圖中的一個人物,次要情節使用。

然後,只需在右側添加一個新軸,然後在那裏繪製colorbar那個axes(使用cax kwarg)。

最後,其易於只將ylabel添加到左側的情節(只將它放在ax1)。

我已經使用subplots_adjust來在合理的位置設置頁邊距,併爲彩條右側留出空間。您會注意到subplots_adjust命令的底部和頂部被重新用於製作色條軸,所以它們都很好地排列起來。

import matplotlib.pyplot as plt 
import numpy as np 

# Sample data. You can get yours with np.genfromtxt('/path/to/performance.dat') 
data1 = np.random.rand(20,20)*2 
data2 = np.random.rand(20,20)*2 
data3 = np.random.rand(20,20)*2 

fig = plt.figure(figsize=(9,3)) 

bottom,top,left,right = 0.2,0.9,0.1,0.85 
fig.subplots_adjust(bottom=bottom,left=left,right=right,top=top) 

# fancy new colormap, just because... 
plt.viridis() 

# Add the 3 subplots 
ax1 = fig.add_subplot(131) 
ax2 = fig.add_subplot(132) 
ax3 = fig.add_subplot(133) 

# Plot the data 
for ax,data in zip(fig.axes,[data1,data2,data3]): 
    p=ax.pcolor(data,vmin=0,vmax=2) 
    # xlabel on all subplots 
    ax.set_xlabel(r"$\log \, \sigma_1$", size=28) 

# ylabel only on the left 
ax1.set_ylabel(r"$\log \, \sigma_2$", size=28) 

# add_axes takes (left,bottom,width,height) 
# height is just top-bottom 
cax = fig.add_axes([right+0.05,bottom,0.03,top-bottom]) 
fig.colorbar(p,cax=cax) 

plt.show() 

enter image description here

的另一種方式排隊所有的次要情節是使用AxesGrid,從mpl_toolkits.axes_grid1,這將這樣做,所有自動。以上是修改爲AxesGrid的腳本。

import matplotlib.pyplot as plt 
import numpy as np 
from mpl_toolkits.axes_grid1 import AxesGrid 

# Sample data. You can get yours with np.genfromtxt('/path/to/performance.dat') 
data1 = np.random.rand(20,20)*2 
data2 = np.random.rand(20,20)*2 
data3 = np.random.rand(20,20)*2 

fig = plt.figure(figsize=(9,3)) 

fig.subplots_adjust(bottom=0.2) 
plt.viridis() 

grid = AxesGrid(fig, 111, 
       nrows_ncols=(1, 3), 
       axes_pad=0.2, 
       share_all=True, 
       label_mode="L", 
       cbar_location="right", 
       cbar_mode="single", 
       ) 

for ax,data in zip(grid,[data1,data2,data3]): 
    p=ax.pcolor(data,vmin=0,vmax=2) 
    ax.set_xlabel(r"$\log \, \sigma_1$", size=28) 

grid[0].set_ylabel(r"$\log \, \sigma_2$", size=28) 

grid.cbar_axes[0].colorbar(p) 

plt.show() 

enter image description here

+0

謝謝!讓他們從不同的路徑是一個好主意,我不知道那個! – lailaw

+0

是的,這也是我的建議(我只是沒有寫到答案中)。 –