多个 imshow-subplots,每个都有颜色条 [英] Multiple imshow-subplots, each with colorbar

查看:88
本文介绍了多个 imshow-subplots,每个都有颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个由四个子图组成的图.其中两个是通常的线图,其中两个是 imshow-images.

我可以将imshow-images本身格式化为适当的图,因为它们中的每个都需要有自己的颜色条,一个修改的轴和另一个已删除的轴.然而,这对于子图似乎绝对没用.有人可以帮我吗?

我用它来显示上面常规"图的数据作为颜色图(通过将输入数组 i 缩放为 [i,i,i,i,i,i ] 用于 2D 并用它调用 imshow().

下面的代码首先显示了我需要的子图,第二个显示了我能做的所有事情,这还不够.

#!/usr/bin/env python导入matplotlib.pyplot作为plt从 matplotlib.colors 导入 LogNorms = { 't':1, 'x':[1,2,3,4,5,6,7,8], 'D':[0.3,0.5,0.2,0.3,0.5,0.5,0.3,0.4]}宽度= 40# 我如何在一个情节中做到这一点总= []对于范围(宽度)中的 i:tot.append(s['D'])plt.imshow(tot, norm=LogNorm(vmin=0.001, vmax=1))plt.colorbar()plt.axes().axes.get_xaxis().set_visible(False)plt.yticks([0,2,4,6],[s ['x'] [0],s ['x'] [2],s ['x'] [4],s ['x'] [6]])plt.show()f = plt.figure(figsize=(20,20))plt.subplot(211)plt.plot(s['x'], s['D'])plt.ylim([0, 1])#colorplotsp = f.add_subplot(212)#reshape(只需要看到一些东西)tot = []对于我在范围(宽度)中:tot.append(s ['D'])sp.imshow(tot,norm = LogNorm(vmin = 0.001,vmax = 1))#我现在无法做但需要做的事情:#sp.colorbar()#sp.axes().axes.get_xaxis().set_visible(False)#sp.yticks([0,200,400,600,800,1000],[s ['x'] [0],s ['x'] [200],s ['x'] [400],s['x'][600], s['x'][800], s['x'][1000]])plt.show()

解决方案

为了更好地控制每个轴,您可以使用 matplotlibs 面向对象的接口而不是状态机接口.此外,要控制颜色栏的高度/宽度,您可以使用 :

<块引用>

axis_divider模块提供了辅助功能make_axes_locatable,可能很有用.它需要一个现有的轴实例并为它创建一个分隔符.

  ax =子图(1,1,1)除法器= make_axes_locatable(ax)

make_axes_locatable 返回 AxesLocator 类的一个实例,源自定位器.它提供了 append_axes 方法来创建给定一侧的新轴(顶部",右侧",底部"和左侧")的原始轴.

I want to have a figure consisting of, let's say, four subplots. Two of them are usual line-plots, two of them imshow-images.

I can format the imshow-images to proper plots itself, because every single one of them needs its own colorbar, a modified axis and the other axis removed. This, however, seems to be absolutely useless for the subplotting. Can anyone help me with that?

I use this for displaying the data of the "regular" plots above as a colormap (by scaling the input-array i to [ i, i, i, i, i, i ] for 2D and calling imshow() with it).

The following code first displays what I need as a subplot and the second one shows all I can do, which is not sufficient.

#!/usr/bin/env python

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

s = { 't':1, 'x':[1,2,3,4,5,6,7,8], 'D':[0.3,0.5,0.2,0.3,0.5,0.5,0.3,0.4] }
width = 40

# how I do it in just one plot
tot = []
for i in range(width):
    tot.append(s['D'])

plt.imshow(tot, norm=LogNorm(vmin=0.001, vmax=1))
plt.colorbar()
plt.axes().axes.get_xaxis().set_visible(False)
plt.yticks([0, 2, 4, 6], [s['x'][0], s['x'][2], s['x'][4], s['x'][6]])

plt.show()


f = plt.figure(figsize=(20,20))

plt.subplot(211)
plt.plot(s['x'], s['D'])
plt.ylim([0, 1])

#colorplot
sp = f.add_subplot(212)

#reshape (just necessary to see something)
tot = []
for i in range(width):
    tot.append(s['D'])

sp.imshow(tot, norm=LogNorm(vmin=0.001, vmax=1))

    #what I can't do now but needs to be done:
    #sp.colorbar()
#sp.axes().axes.get_xaxis().set_visible(False)
#sp.yticks([0, 200, 400, 600, 800, 1000], [s['x'][0], s['x'][200], s['x'][400], s['x'][600], s['x'][800], s['x'][1000]])

plt.show()

解决方案

You can make use of matplotlibs object oriented interface rather than the state-machine interace in order to get better control over each axes. Also, to get control over the height/width of the colorbar you can make use of the AxesGrid toolkit of matplotlib.

For example:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.colors import LogNorm
from matplotlib.ticker import MultipleLocator

s = {'t': 1,
     'x': [1, 2, 3, 4, 5, 6, 7, 8],
     'T': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
     'D': [0.3, 0.5, 0.2, 0.3, 0.5, 0.5, 0.3, 0.4]}

width = 40

tot = np.repeat(s['D'],width).reshape(len(s['D']), width)
tot2 = np.repeat(s['T'],width).reshape(len(s['D']), width)

fig, (ax1, ax2, ax3, ax4) = plt.subplots(1,4)

fig.suptitle('Title of figure', fontsize=20)

# Line plots
ax1.set_title('Title of ax1')
ax1.plot(s['x'], s['T'])
ax1.set_ylim(0,1)

ax2.set_title('Title of ax2')
ax2.plot(s['x'], s['D'])
# Set locations of ticks on y-axis (at every multiple of 0.25)
ax2.yaxis.set_major_locator(MultipleLocator(0.25))
# Set locations of ticks on x-axis (at every multiple of 2)
ax2.xaxis.set_major_locator(MultipleLocator(2))
ax2.set_ylim(0,1)

ax3.set_title('Title of ax3')
# Display image, `aspect='auto'` makes it fill the whole `axes` (ax3)
im3 = ax3.imshow(tot, norm=LogNorm(vmin=0.001, vmax=1), aspect='auto')
# Create divider for existing axes instance
divider3 = make_axes_locatable(ax3)
# Append axes to the right of ax3, with 20% width of ax3
cax3 = divider3.append_axes("right", size="20%", pad=0.05)
# Create colorbar in the appended axes
# Tick locations can be set with the kwarg `ticks`
# and the format of the ticklabels with kwarg `format`
cbar3 = plt.colorbar(im3, cax=cax3, ticks=MultipleLocator(0.2), format="%.2f")
# Remove xticks from ax3
ax3.xaxis.set_visible(False)
# Manually set ticklocations
ax3.set_yticks([0.0, 2.5, 3.14, 4.0, 5.2, 7.0])

ax4.set_title('Title of ax4')
im4 = ax4.imshow(tot2, norm=LogNorm(vmin=0.001, vmax=1), aspect='auto')
divider4 = make_axes_locatable(ax4)
cax4 = divider4.append_axes("right", size="20%", pad=0.05)
cbar4 = plt.colorbar(im4, cax=cax4)
ax4.xaxis.set_visible(False)
# Manually set ticklabels (not ticklocations, they remain unchanged)
ax4.set_yticklabels([0, 50, 30, 'foo', 'bar', 'baz'])

plt.tight_layout()
# Make space for title
plt.subplots_adjust(top=0.85)
plt.show()


You can change the locations and labels of the ticks on either axis with the set_ticks and set_ticklabels methods as in the example above.


As for what the make_axes_locatable function does, from the matplotlib site about the AxesGrid toolkit:

The axes_divider module provides a helper function make_axes_locatable, which can be useful. It takes a existing axes instance and create a divider for it.

ax = subplot(1,1,1)
divider = make_axes_locatable(ax)

make_axes_locatable returns an instance of the AxesLocator class, derived from the Locator. It provides append_axes method that creates a new axes on the given side of ("top", "right", "bottom" and "left") of the original axes.

这篇关于多个 imshow-subplots,每个都有颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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