独立色条(matplotlib)

use*_*ser 14 python matplotlib

我正在使用matplotlib在python中渲染一些图形,并将它们包含在LaTeX文件中(使用LaTex的漂亮表格对齐而不是摆弄matplotlib ImageGrid等).我想创建并保存一个独立的颜色条savefig,而无需使用imshow.

(vlim, vmax参数,以及cmap可以明确提供)

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

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

HYR*_*YRY 24

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

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="h", cax=cax)
pl.savefig("colorbar.pdf")
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

  • 它为我抛出一个AssertionError`` assert position =='top'或position =='bottom'``.将代码更改为``pl.colorbar(orientation ='horizo​​ntal',cax = cax)``修复问题并生成我正在寻找的结果.可能是一些版本问题左右. (8认同)

bla*_*kbk 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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

当然,您可以指定 Colorbar 的许多其他方面

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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Mar*_*ark 8

使用与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))
Run Code Online (Sandbox Code Playgroud)


Xar*_*rio 8

所以,基于这里的这个答案,如果你像我一样想要避免这个丑陋的假 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')
Run Code Online (Sandbox Code Playgroud)

结果图的图片

  • 只是补充.... 这是官方版本:https://matplotlib.org/tutorials/colors/colorbar_only.html?highlight=colorbarbase (6认同)

Mar*_*hke 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述