python subplots_python – 多个imshow-subplots,每个都有colorbar

本文展示了如何使用matplotlib的面向对象接口和AxesGrid工具包创建多个imshow子图,每个子图都有其自己的colorbar。通过示例代码,详细说明了设置子图标题、调整刻度、添加colorbar以及自定义colorbar刻度和标签的方法。
部署运行你感兴趣的模型镜像

您可以使用matplotlibs面向对象的接口,而不是状态机间隔,以便更好地控制每个轴。另外,为了控制colorbar的高度/宽度,您可以使用matplotlib的

AxesGrid工具包。

例如:

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

您可以使用set_ticks和set_ticklabels方法更改任一轴上刻度线的位置和标签,如上例所示。

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.

06001

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.

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值