2015-11-19 76 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

是的,这也是我的建议(我只是没有写到答案中)。 –