gpt4 book ai didi

python - 在 matplotlib 中创建没有颜色条的热图

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:48 25 4
gpt4 key购买 nike

我使用以下代码从不同文件夹中的不同数据创建了 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 output

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

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

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

最佳答案

您可以使用 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()

enter image description here

排列所有子图的另一种方法是使用 mpl_toolkits.axes_grid1 中的 AxesGrid,它会自动执行所有操作。这是为 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

关于python - 在 matplotlib 中创建没有颜色条的热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33804118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com