python plt figure_python – Matplotlib:如何在使用plt.figure()和figure.addaxes()时显示图像颜色条...

我试图用六个单独的地块制作一个数字,组成两排三个地块.每行图应具有其自己的颜色条,其对应于水平组中的三个图中所示的图像.从视觉上看,图形看起来像:

image_type1 | image_type1 | image_type1 | colorbar_for_type1_images

image_type2 | image_type2 | image_type2 | colorbar_for_type2_images

上面表示中的垂直线只是为了分离图中的不同组件.我的图中实际上并不需要垂直线条.

下面显示了我正在尝试做的一个示例,以及我尝试使用每行中的第三个图像绘制颜色条的尝试失败.

我以前能够成功地完成这项工作,其代码类似于下面的内容,当我使用我自己的彩色地图绘制一系列绘制的线条时,而不是像我在下面尝试的那样使用图像.

import numpy as np

from matplotlib import pyplot as plt

from matplotlib.cbook import get_sample_data

#Make 6 plotting areas of the same dimensions

figuresizex = 9.0

figuresizey = 6.1

lowerx = .07

lowery = .09

upperx = .92

uppery = .97

xspace = .05

yspace = .11

xwidth = (upperx-lowerx-2*xspace)/3.

ywidth = (uppery-lowery-yspace)/2.

fig = plt.figure(figsize=(figuresizex,figuresizey))

ax1 = fig.add_axes([lowerx,lowery+ywidth+yspace,xwidth,ywidth])

ax2 = fig.add_axes([lowerx+xwidth+xspace,lowery+ywidth+yspace,xwidth,ywidth])

ax3 = fig.add_axes([lowerx+2*xwidth+2*xspace,lowery+ywidth+yspace,xwidth,ywidth])

ax4 = fig.add_axes([lowerx,lowery,xwidth,ywidth])

ax5 = fig.add_axes([lowerx+xwidth+xspace,lowery,xwidth,ywidth])

ax6 = fig.add_axes([lowerx+2*xwidth+2*xspace,lowery,xwidth,ywidth])

axlist = [ax1,ax2,ax3,ax4,ax5,ax6]

#Start plotting images

image = np.identity(5)

for i in range(0,3):

vmin, vmax = image.min(),image.max()

axuse = axlist[i]

im = axuse.imshow(image, vmin=vmin, vmax=vmax)

if i == 3:

cbar = axuse.colorbar(im)

cbar = plt.colorbar(im)

image_2 = np.arange(16).reshape((4,4))

for i in range(0,3):

vmin, vmax = image_2.min(),image_2.max()

axuse = axlist[i+3]

axuse.imshow(image_2,vmin=vmin, vmax=vmax)

if i == 3:

cbar = axuse.colorbar()

cbar = plt.colorbar()

plt.show()

最佳答案 我建议采用

this question中概述的方法.

除了简单地添加颜色条并且不依赖于第三个图像(应该是i == 2)之外,使用ImageGrid还无需明确(痛苦地)定义所有6个轴并变得更加灵活如果您的图像数量发生变化

更新:我添加了第三行,以显示可以使用vmin和vmax参数将相同的比例应用于每行中的所有图像.

import numpy as np

from matplotlib import pyplot as plt

from mpl_toolkits.axes_grid1 import ImageGrid

figuresizex = 9.0

figuresizey = 6.1

# generate images

image1 = np.identity(5)

image2 = np.arange(16).reshape((4,4))

fig = plt.figure(figsize=(figuresizex,figuresizey))

# create your grid objects

top_row = ImageGrid(fig, 311, nrows_ncols = (1,3), axes_pad = .25,

cbar_location = "right", cbar_mode="single")

middle_row = ImageGrid(fig, 312, nrows_ncols = (1,3), axes_pad = .25,

cbar_location = "right", cbar_mode="single")

bottom_row = ImageGrid(fig, 313, nrows_ncols = (1,3), axes_pad = .25,

cbar_location = "right", cbar_mode="single")

# plot the images

for i in range(3):

vmin, vmax = image1.min(),image1.max()

ax = top_row[i]

im1 = ax.imshow(image1, vmin=vmin, vmax=vmax)

for i in range(3):

vmin, vmax = image2.min(),image2.max()

ax =middle_row[i]

im2 = ax.imshow(image2, vmin=vmin, vmax=vmax)

# Update showing how to use identical scale across all 3 images

# make some slightly different images and get their bounds

image2s = [image2,image2 + 5,image2 - 5]

# inelegant way to get the absolute upper and lower bounds from the three images

i_max, i_min = 0,0

for im in image2s:

if im.max() > i_max:

i_max= im.max()

if im.min() < i_min:

i_min = im.min()

# plot these as you would the others, but use identical vmin and vmax for all three plots

for i,im in enumerate(image2s):

ax = bottom_row[i]

im2_scaled = ax.imshow(im, vmin = i_min, vmax = i_max)

# add your colorbars

cbar1 = top_row.cbar_axes[0].colorbar(im1)

middle_row.cbar_axes[0].colorbar(im2)

bottom_row.cbar_axes[0].colorbar(im2_scaled)

# example of titling colorbar1

cbar1.set_label_text("label"))

# readjust figure margins after adding colorbars,

# left and right are unequal because of how

# colorbar labels don't appear to factor in to the adjustment

plt.subplots_adjust(left=0.075, right=0.9)

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值