【问题标题】:Creating a heatmap without a colorbar in matplotlib在 matplotlib 中创建没有颜色条的热图
【发布时间】:2015-11-19 12:34:28
【问题描述】:

我使用以下代码从不同文件夹中的不同数据创建了 3 个带有 colorbar() 的 python 图:

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 上玩过,直到现在这是我能得到的最好的,以使其可读。我的想法可能是只创建一个显示垂直颜色条的图(最后一个图),第一个图只有“y 标签”,中间一个带有“x 标签”,我认为这样看起来会更好。

我的问题是如何在不显示颜色条的情况下创建颜色条图? 我试图评论pp.colorbar().ax.tick_params(labelsize=20) 行,但这搞砸了情节。

地块仍然需要更大的标签和刻度字体大小。

【问题讨论】:

  • 您可以考虑的另一种选择是 PGFplots,它将直接在 LaTeX 中创建图。当您有多个共享轴的图时,它包含用于省略轴标签的内置工具。但这超出了这个问题的范围。
  • 谢谢@DavidZ,没听说过。但我真的更喜欢直接的 Python 解决方案!也感谢您的编辑
  • 另外,我尝试在没有colorbar 调用的情况下创建这种类型的绘图,它工作正常。您能否详细说明尝试删除该行时出了什么问题?
  • 遵循@DavidZ 的建议;为什么不能只删除第一个和第三个情节的pp.xlabel(r"$\log \, \sigma_1$", size=28),第二个和第三个情节的pp.ylabel(r"$\log \, \sigma_2$",size=28),第一和第二个情节的pp.colorbar().ax.tick_params(labelsize=20)?你说它搞砸了情节,但你的意思是情节的尺寸与其他情节不同吗?在这种情况下,您可以使用pp.figure(figsize=(width,height)) 明确设置图形大小
  • 抱歉,是的,是尺寸问题!那么只有最后一个带有颜色条的保持方形,其他的变成矩形!

标签: python matplotlib colorbar


【解决方案1】:

您可以通过提供genfromtxt 的路径,使用genfromtxt 从不同目录获取数据:

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

您可以使用子图在一个图中创建所有 3 个热图。

然后,只需在右侧添加一个新轴,并在该 axes 上绘制 colorbar(使用 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()

排列所有子图的另一种方法是使用来自mpl_toolkits.axes_grid1AxesGrid,它会自动完成所有操作。这是上面为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()

【讨论】:

  • 谢谢!!从不同的路径获取它们是个好主意,我不知道那个!
  • 是的,这也是我的建议(我只是没能把它写成答案)。
猜你喜欢
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 2015-02-19
  • 1970-01-01
相关资源
最近更新 更多