【问题标题】:Python: Merge subplot over two subplotcells in matplotlib or with other libaries [duplicate]Python:在matplotlib或其他库中合并两个子图的子图[重复]
【发布时间】:2021-12-28 09:55:43
【问题描述】:

基于this 使用 python 在 matplotlib 上的多个子图的文章,例如以 2x2 矩阵的形式创建子图非常容易。使用示例 4 中的代码,我可以获得以下形式的支持: 带代码:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()

我现在的目标是以下形式的 3 个 supblots,但我没有在网络上找到可用的资源来获得这个:

【问题讨论】:

标签: python matplotlib subplot


【解决方案1】:

最简单的可能是使用 subplots_mosaic: https://matplotlib.org/stable/tutorials/provisional/mosaic.html

import matplotlib.pyplot as plt
import numpy as np

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, axd = plt.subplot_mosaic([['left', 'right'],['bottom', 'bottom']],
                              constrained_layout=True)
axd['left'].plot(x, y, 'C0')
axd['right'].plot(x, y, 'C1')
axd['bottom'].plot(x, y, 'C2')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2011-03-27
    相关资源
    最近更新 更多