独立颜色栏

问题描述 投票:0回答:6

我正在使用 matplotlib 在 python 中渲染一些图形,并将它们包含到 LaTeX 论文中(使用 LaTex 漂亮的表格对齐方式,而不是摆弄 matplotlib 的

ImageGrid
等)。 我想使用
savefig
创建并保存独立的颜色条,而不需要使用
imshow

(可以明确提供

vlim, vmax
参数以及
cmap

我能找到的唯一方法非常复杂,并且(根据我的理解)在画布上绘制一个硬编码的矩形: http://matplotlib.org/examples/api/colorbar_only.html

有没有一种优雅的方法来使用 matplotlib 创建独立的颜色条?

python matplotlib colorbar
6个回答
42
投票

您可以创建一些虚拟图像,然后隐藏它的斧头。在自定义轴中绘制颜色条。

import pylab as pl
import numpy as np

a = np.array([[0,1]])
pl.figure(figsize=(9, 1.5))
img = pl.imshow(a, cmap="Blues")
pl.gca().set_visible(False)
cax = pl.axes([0.1, 0.2, 0.8, 0.6])
pl.colorbar(orientation="horizontal", cax=cax)
pl.savefig("colorbar.pdf")

结果:

enter image description here


15
投票

使用与 HYRY 的答案相同的想法,如果您想要一个“独立”颜色条,即它独立于图形上的项目(与它们的颜色不直接相关),您可以执行以下操作:

from matplotlib import pyplot as plt
import numpy as np

# create dummy invisible image
# (use the colormap you want to have on the colorbar)
img = plt.imshow(np.array([[0,1]]), cmap="Oranges")
img.set_visible(False)

plt.colorbar(orientation="vertical")

# add any other things you want to the figure.
plt.plot(np.random.rand(30))

10
投票

http://matplotlib.org/examples/api/colorbar_only.html 的引用为我解决了这个问题。这个例子有点冗长,所以这里有一个制作独立颜色条的简单方法(供后代使用)...

import matplotlib.pyplot as plt
import matplotlib as mpl

fig = plt.figure()
ax = fig.add_axes([0.05, 0.80, 0.9, 0.1])

cb = mpl.colorbar.ColorbarBase(ax, orientation='horizontal', 
                               cmap='RdBu')

plt.savefig('just_colorbar', bbox_inches='tight')

当然,您可以指定颜色栏的许多其他方面

import matplotlib.pyplot as plt
import matplotlib as mpl

fig = plt.figure()
ax = fig.add_axes([0.05, 0.80, 0.9, 0.1])

cb = mpl.colorbar.ColorbarBase(ax, orientation='horizontal', 
                               cmap='gist_ncar',
                               norm=mpl.colors.Normalize(0, 10),  # vmax and vmin
                               extend='both',
                               label='This is a label',
                               ticks=[0, 3, 6, 9])

plt.savefig('just_colorbar', bbox_inches='tight')


10
投票

因此,基于这里的答案,如果您像我一样并且想避免这个丑陋的假plt.imshow(),您基本上可以用两行来完成此操作:

import matplotlib as mpl
import matplotlib.pyplot as plt


fig, ax = plt.subplots()
col_map = plt.get_cmap('nipy_spectral')
mpl.colorbar.ColorbarBase(ax, cmap=col_map, orientation = 'vertical')

# As for a more fancy example, you can also give an axes by hand:
c_map_ax = fig.add_axes([0.2, 0.8, 0.6, 0.02])
c_map_ax.axes.get_xaxis().set_visible(False)
c_map_ax.axes.get_yaxis().set_visible(False)

# and create another colorbar with:
mpl.colorbar.ColorbarBase(c_map_ax, cmap=col_map, orientation = 'horizontal')


5
投票

该解决方案还可以用于独立于 ax 的内容绘制颜色条。 只需设置

fraction = .05

代码

import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)

fraction = 1  # .05

norm = mpl.colors.Normalize(vmin=-3, vmax=99)
cbar = ax.figure.colorbar(
            mpl.cm.ScalarMappable(norm=norm, cmap='Blues'),
            ax=ax, pad=.05, extend='both', fraction=fraction)

ax.axis('off')
plt.show()


1
投票

要添加到@blalockbk的答案(这是一个很好的解决方案,甚至可以添加到已经创建的图形中),对我来说,

cmap
参数不会采用颜色图名称的字符串,但是
cmap = plt.cm.viridis
可以工作,如果有人遇到和我一样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.