带颜色渐变填充的 Matplotlib 矩形 [英] Matplotlib Rectangle With Color Gradient Fill

查看:75
本文介绍了带颜色渐变填充的 Matplotlib 矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的轴实例 (ax1) 坐标系中的任意位置绘制一个矩形,从左到右使用渐变颜色填充.

我的第一个想法是创建一个路径补丁并以某种方式将其填充设置为颜色渐变.但是,根据

可以指定颜色图、使用什么插值等等.我觉得很有趣的另一件事是可以指定要使用颜色图的哪一部分.这是通过 vmin vmax 来完成的:

  pyplot.imshow([[64,192],[64,192]],cmap = pyplot.cm.Greens,插值='bicubic',vmin = 0,vmax = 255)

受到此示例

的启发

附加说明:

我选择 X = [[0.,1.],[0.,1.]] 使渐变从左向右变化.通过将数组设置为 X = [[0.,0.],[1.,1.]] 之类的东西,您将获得从上到下的渐变.通常,可以为每个角指定颜色,其中 X = [[i00,i01],[i10,i11]] i00 i01 i10 i11 分别为左上角,右上角,左下角和右下角指定颜色.增加 X 的大小显然可以为更具体的点设置颜色.

I want to draw a rectangle, with a gradient color fill from left to right, at an arbitrary position with arbitrary dimensions in my axes instance (ax1) coordinate system.

My first thought was to create a path patch and somehow set its fill as a color gradient. But according to THIS POST there isn't a way to do that.

Next I tried using a colorbar. I created a second axes instance ax2 using fig.add_axes([left, bottom, width, height]) and added a color bar to that.

ax2 = fig.add_axes([0, 0, width, height/8])
colors = [grad_start_color, grad_end_color]
index  = [0.0, 1.0]
cm = LinearSegmentedColormap.from_list('my_colormap', zip(index, colors))
colorbar.ColorbarBase(ax2, cmap=cm, orientation='horizontal')

But the positional parameters passed to fig.add_axes() are in the coordinate system of fig, and don't match up with the coordinate system of ax1.

How can I do this?

解决方案

I have been asking myself a similar question and spent some time looking for the answer to find in the end that this can quite easily be done by imshow:

from matplotlib import pyplot

pyplot.imshow([[0.,1.], [0.,1.]], 
  cmap = pyplot.cm.Greens, 
  interpolation = 'bicubic'
)

It is possible to specify a colormap, what interpolation to use and much more. One additional thing, I find very interesting, is the possibility to specify which part of the colormap to use. This is done by means of vmin and vmax:

pyplot.imshow([[64, 192], [64, 192]], 
  cmap = pyplot.cm.Greens, 
  interpolation = 'bicubic', 
  vmin = 0, vmax = 255
)

Inspired by this example


Additional Note:

I chose X = [[0.,1.], [0.,1.]] to make the gradient change from left to right. By setting the array to something like X = [[0.,0.], [1.,1.]], you get a gradient from top to bottom. In general, it is possible to specify the colour for each corner where in X = [[i00, i01],[i10, i11]], i00, i01, i10 and i11 specify colours for the upper-left, upper-right, lower-left and lower-right corners respectively. Increasing the size of X obviously allows to set colours for more specific points.

这篇关于带颜色渐变填充的 Matplotlib 矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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