简体   繁体   中英

Creating a heatmap without a colorbar in matplotlib

I create 3 python plots with colorbar() from different data in separate folders using the following code:

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()

Then I insert it in my LaTeX file side by side with:

\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}

I get:

LaTeX输出

Clearly that does not look good, I played around LaTeX and until now that was the best I could get in order to make it readable. My idea was maybe to create only one plot showing the vertical color bar (the last plot), the first plot with only the "y label" and the middle one with the "x label", that would look better I think.

My question is how to create a colorbar plot without showing the bar? I tried to comment the pp.colorbar().ax.tick_params(labelsize=20) line but that messed up the plot.

The plots still need bigger label and ticks font size.

You can get your data from different directories with genfromtxt by giving the path to genfromtxt :

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

You can created all 3 heatmaps in one figure, using subplots.

Then, just add a new axes to the right, and plot the colorbar on that axes there (using the cax kwarg).

Finally, its easy to only add the ylabel to the left hand plot (only put it on ax1 ).

I've used subplots_adjust to set the margins in sensible places, and make room on the right for the colorbar. You'll notice that the bottom and top of the subplots_adjust command are reused to make the colorbar axis, so it all lines up nicely.

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()

在此处输入图片说明

An alternative way to line up all your subplots is to use AxesGrid , from mpl_toolkits.axes_grid1 , which will do that all automatically. Here's the script from above modified for 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM