设置matplotlib ColorbarBase对象的大小 [英] Setting the size of a matplotlib ColorbarBase object

查看:54
本文介绍了设置matplotlib ColorbarBase对象的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个补丁集合,我想为其显示颜色图.由于我在颜色映射表上进行了一些操作,因此无法使用 matplotlib.colorbar 实例对其进行定义.至少据我所知;这样做会去除我对颜色所做的一些操作,从而消除缺少数据的色块:

I have an patch collection that I'd like to display a color map for. Because of some manipulations I do on top of the colormap, it's not possible for me to define it using a matplotlib.colorbar instance. At least not as far as I can tell; doing so strips some manipulations I do with my colors that blank out patches lacking data:

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.ColorbarBase 实例,该实例有效:

So I define a matplotlib.colorbar.ColorbarBase instance instead, which works:

matplotlib.colorbar.ColorbarBase(ax1, cmap=cmap, orientation='vertical',
                                 norm=matplotlib.colors.Normalize(vmin=0, vmax=1))

结果例如:

我遇到的问题是我想减小此颜色条的大小(特别是将其缩小到特定的垂直大小,例如500像素),但是我看不到任何明显的方法.如果我有一个 colorbar 实例,则可以使用其

The problem I have is that I want to reduce the size of this colorbar (specifically, the shrink it down to a specific vertical size, say, 500 pixels), but I don't see any obvious way of doing this. If I had a colorbar instance, I could adjust this easily using its axis property arguments, but ColorbarBase lacks these.

进一步的参考:

  • The example my implementation is based on.
  • The source code in question (warning: lengthy).

推荐答案

尺寸和形状由轴定义.这是我编写的代码片段,其中将2个图组合在一起,并在顶部单独添加了一个颜色栏.我玩过那个add_axes实例中的值,直到获得适合我的大小:

The size and shape is defined with the axis. This is a snippet from code I have where I group 2 plots together and add a colorbar at the top independently. I played with the values in that add_axes instance until I got a size that worked for me:

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

这篇关于设置matplotlib ColorbarBase对象的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆