Matplotlib: Same height for subfigures

假装没事ソ 提交于 2019-12-11 03:08:43

问题


in the following example, how can I set both subfigures to the same height?

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

回答1:


You can use 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)

Produces output like:



来源:https://stackoverflow.com/questions/29117623/matplotlib-same-height-for-subfigures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!