python - Matplotlib:子图的高度相同

标签 python matplotlib subplot imshow

在下面的示例中,如何将两个子图设置为相同的高度?

#minimal example
import matplotlib.pyplot as plt
import numpy as np
f, (ax1, ax2) = plt.subplots(1, 2)
im = np.random.random((100,100))
ax1.imshow(im)
ax1.set_xlim(0, im.shape[1])
ax1.set_ylim(0, im.shape[0])
x = np.arange(100)
ax2.plot(x, x**2)

最佳答案

您可以使用matplotlib.gridspec:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

# Add subplots using gridspec instead of plt.subplots()
gs = gridspec.GridSpec(1,2, height_ratios=[1,1])
f = plt.figure()
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

im = np.random.random((100,100))
ax1.imshow(im)
ax1.set_xlim(0, im.shape[1])
ax1.set_ylim(0, im.shape[0])
x = np.arange(100)
ax2.plot(x, x**2)

产生如下输出:

Example image for same height using gridspec

关于python - Matplotlib:子图的高度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29117623/

相关文章:

Python - 字典中函数的错误分配

python - 正则表达式 - 将单词匹配限制为以特定单词开头的行

Python:Matplotlib twin() 问题:绘制分布

python - 如何从 Matplotlib 中的两个轴取消设置 `sharex` 或 `sharey`

python - matplotlib 绘图几乎没有错误

python - 使用seaborn调整子图的大小

php - php 文件如何找到其他 php 文件中的 namespace ?

python - 将 pybrain 预测的输出作为数组

python - Opencv 转换为灰度无法正常工作

python - 在线图中放置间隙/中断