Python 设置matplotlib ColorbarBase对象的大小
我有一个补丁集合,我想为它显示一个彩色地图。由于我在colormap上做了一些操作,所以我不可能使用Python 设置matplotlib ColorbarBase对象的大小,python,matplotlib,colorbar,color-mapping,Python,Matplotlib,Colorbar,Color Mapping,我有一个补丁集合,我想为它显示一个彩色地图。由于我在colormap上做了一些操作,所以我不可能使用matplotlib.colorbar实例来定义它。至少据我所知不是这样;这样做会去除我对颜色所做的一些操作,这些操作会清空缺少数据的面片: cmap = matplotlib.cm.YlOrRd colors = [cmap(n) if pd.notnull(n) else [1,1,1,1] for n in plt.Normalize(0, 1)([nullity for
matplotlib.colorbar
实例来定义它。至少据我所知不是这样;这样做会去除我对颜色所做的一些操作,这些操作会清空缺少数据的面片:
cmap = matplotlib.cm.YlOrRd
colors = [cmap(n) if pd.notnull(n) else [1,1,1,1]
for n in plt.Normalize(0, 1)([nullity for _, nullity in squares])]
# Now we draw.
for i, ((min_x, max_x, min_y, max_y), _) in enumerate(squares):
square = shapely.geometry.Polygon([[min_x, min_y], [max_x, min_y],
[max_x, max_y], [min_x, max_y]])
ax0.add_patch(descartes.PolygonPatch(square, fc=colors[i],
ec='white', alpha=1, zorder=4))
因此,我定义了一个matplotlib.colorbar.colorbase
实例,它可以:
matplotlib.colorbar.ColorbarBase(ax1, cmap=cmap, orientation='vertical',
norm=matplotlib.colors.Normalize(vmin=0, vmax=1))
其结果是,例如:
我的问题是,我想缩小这个色条的大小(特别是,缩小到一个特定的垂直大小,比如说500像素),但我看不到任何明显的方法来做到这一点。如果我有一个colorbar
实例,我可以使用its轻松地调整它,但是colorbase
缺少这些
供进一步参考:
- (警告:冗长)
cax = fig.add_axes([0.125, 0.925, 0.775, 0.0725]) #has to be as a list - starts with x, y coordinates for start and then width and height in % of figure width
norm = mpl.colors.Normalize(vmin = low_val, vmax = high_val)
mpl.colorbar.ColorbarBase(cax, cmap = self.cmap, norm = norm, orientation = 'horizontal')
尺寸和形状由轴定义。这是我的代码片段,我将2个图组合在一起,并在顶部独立添加一个颜色条。我一直在玩add_axes实例中的值,直到得到适合我的大小:
cax = fig.add_axes([0.125, 0.925, 0.775, 0.0725]) #has to be as a list - starts with x, y coordinates for start and then width and height in % of figure width
norm = mpl.colors.Normalize(vmin = low_val, vmax = high_val)
mpl.colorbar.ColorbarBase(cax, cmap = self.cmap, norm = norm, orientation = 'horizontal')
这个问题可能有点老了,但我找到了另一个解决方案,它可以帮助那些不愿意为ColorbarBase类手动创建colorbar轴的人 下面的解决方案使用matplotlib.colorbar.make_axes类从给定轴创建从属子_轴。然后可以为ColorbarBase类提供子_轴,以创建colorbar 该代码源自中描述的matplotlib代码示例 下面是一段代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.colorbar as mcbar
from matplotlib import ticker
import matplotlib.colors as mcolors
# Make some illustrative fake data:
x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2 * np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B
n_bins = [3, 6, 10, 100] # Discretizes the interpolation into bins
cmap_name = 'my_list'
fig, axs = plt.subplots(2, 2, figsize=(9, 7))
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
for n_bin, ax in zip(n_bins, axs.ravel()):
# Create the colormap
cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
# Fewer bins will result in "coarser" colomap interpolation
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm)
ax.set_title("N bins: %s" % n_bin)
cax, cbar_kwds = mcbar.make_axes(ax, location = 'right',
fraction=0.15, shrink=0.5, aspect=20)
cbar = mcbar.ColorbarBase(cax, cmap=cm,
norm=mcolors.Normalize(clip=False),
alpha=None,
values=None,
boundaries=None,
orientation='vertical', ticklocation='auto', extend='both',
ticks=n_bins,
format=ticker.FormatStrFormatter('%.2f'),
drawedges=False,
filled=True,
extendfrac=None,
extendrect=False, label='my label')
if n_bin <= 10:
cbar.locator = ticker.MaxNLocator(n_bin)
cbar.update_ticks()
else:
cbar.locator = ticker.MaxNLocator(5)
cbar.update_ticks()
fig.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
从matplotlib.colors导入LinearSegmentedColormap
将matplotlib.colorbar作为mcbar导入
从matplotlib导入代码
将matplotlib.colors导入为MCOLOR
#制作一些说明性的假数据:
x=np.arange(0,np.pi,0.1)
y=np.arange(0,2*np.pi,0.1)
十、 Y=np.meshgrid(X,Y)
Z=np.cos(X)*np.sin(Y)*10
颜色=[(1,0,0),(0,1,0),(0,0,1)]\R->G->B
n_bin=[3,6,10,100]#将插值离散化为bin
cmap_名称='我的_列表'
图,axs=plt.子批次(2,2,figsize=(9,7))
图子批次调整(左=0.02,下=0.06,右=0.95,上=0.94,wspace=0.05)
对于n_-bin,ax-in-zip(n_-bin,axs.ravel()):
#创建颜色映射
cm=LinearSegmentedColormap.from_列表(cmap_名称、颜色,N=N_bin)
#更少的容器将导致“更粗糙”的colomap插值
im=ax.imshow(Z,插值为最近,原点为较低,cmap=cm)
ax.set\u title(“N个料仓:%s”%N\u料仓)
cax,cbar\u kwds=mcbar.make\u轴(ax,位置='右',
分数=0.15,收缩=0.5,纵横比=20)
cbar=mcbar.colorbase(cax,cmap=cm,
norm=mcolors.Normalize(clip=False),
α=无,
值=无,
边界=无,
方向为“垂直”,位置为“自动”,延伸为“两个”,
滴答声=n_桶,
格式=ticker.FormatStrFormatter('%.2f'),
付款人=假,
填充=真,
extendfrac=None,
extendrect=False,label='my label')
如果n_bin这个问题可能有点老了,但是我找到了另一个解决方案,它可以帮助那些不愿意为ColorbarBase类手动创建colorbar轴的人
下面的解决方案使用matplotlib.colorbar.make_axes类从给定轴创建从属子_轴。然后可以为ColorbarBase类提供子_轴,以创建colorbar
该代码源自中描述的matplotlib代码示例
下面是一段代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.colorbar as mcbar
from matplotlib import ticker
import matplotlib.colors as mcolors
# Make some illustrative fake data:
x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2 * np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B
n_bins = [3, 6, 10, 100] # Discretizes the interpolation into bins
cmap_name = 'my_list'
fig, axs = plt.subplots(2, 2, figsize=(9, 7))
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
for n_bin, ax in zip(n_bins, axs.ravel()):
# Create the colormap
cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
# Fewer bins will result in "coarser" colomap interpolation
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm)
ax.set_title("N bins: %s" % n_bin)
cax, cbar_kwds = mcbar.make_axes(ax, location = 'right',
fraction=0.15, shrink=0.5, aspect=20)
cbar = mcbar.ColorbarBase(cax, cmap=cm,
norm=mcolors.Normalize(clip=False),
alpha=None,
values=None,
boundaries=None,
orientation='vertical', ticklocation='auto', extend='both',
ticks=n_bins,
format=ticker.FormatStrFormatter('%.2f'),
drawedges=False,
filled=True,
extendfrac=None,
extendrect=False, label='my label')
if n_bin <= 10:
cbar.locator = ticker.MaxNLocator(n_bin)
cbar.update_ticks()
else:
cbar.locator = ticker.MaxNLocator(5)
cbar.update_ticks()
fig.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
从matplotlib.colors导入LinearSegmentedColormap
将matplotlib.colorbar作为mcbar导入
从matplotlib导入代码
将matplotlib.colors导入为MCOLOR
#制作一些说明性的假数据:
x=np.arange(0,np.pi,0.1)
y=np.arange(0,2*np.pi,0.1)
十、 Y=np.meshgrid(X,Y)
Z=np.cos(X)*np.sin(Y)*10
颜色=[(1,0,0),(0,1,0),(0,0,1)]\R->G->B
n_bin=[3,6,10,100]#将插值离散化为bin
cmap_名称='我的_列表'
图,axs=plt.子批次(2,2,figsize=(9,7))
图子批次调整(左=0.02,下=0.06,右=0.95,上=0.94,wspace=0.05)
对于n_-bin,ax-in-zip(n_-bin,axs.ravel()):
#创建颜色映射
cm=LinearSegmentedColormap.from_列表(cmap_名称、颜色,N=N_bin)
#更少的容器将导致“更粗糙”的colomap插值
im=ax.imshow(Z,插值为最近,原点为较低,cmap=cm)
ax.set\u title(“N个料仓:%s”%N\u料仓)
cax,cbar\u kwds=mcbar.make\u轴(ax,位置='右',
分数=0.15,收缩=0.5,纵横比=20)
cbar=mcbar.colorbase(cax,cmap=cm,
norm=mcolors.Normalize(clip=False),
α=无,
值=无,
边界=无,
方向为“垂直”,位置为“自动”,延伸为“两个”,
滴答声=n_桶,
格式=ticker.FormatStrFormatter('%.2f'),
付款人=假,
填充=真,
extendfrac=None,
extendrect=False,label='my label')
如果n_bin,则颜色条似乎占据了给定给它的整个轴,因此减小其大小的唯一方法是