gpt4 book ai didi

python - Matplotlib:倒置图的组合

转载 作者:行者123 更新时间:2023-12-01 06:59:33 24 4
gpt4 key购买 nike

我需要使用matplotlib实现以下效果:

enter image description here

正如您所看到的,它是不同象限中的图的组合。

我确实知道如何单独生成每个象限。例如,对于“x 反转”象限图,我将简单地使用:

plt.plot(x, y)
plt.gca().invert_yaxis()
plt.show()

绘制情节。它正确地反转了 x 轴。但是,它只会为我生成左上象限的图。

如何生成上图中描述的图的组合?每个象限都有自己的图,具有不同的倒轴。

我最好的想法是将其合并到像 Paint 这样的工具中。

最佳答案

我没有足够的声誉来添加评论以添加到 ImportanceOfBeingErnest 的评论中,但是当您创建 4 个子图时,您将需要删除图之间的空间以及共享轴(并清理重叠的部分)蜱)。

有多种方法可以制作子图,但我更喜欢 gridspec。您可以使用 gridspec 创建一个 2x2 网格并执行所有这些操作,这是一个示例:

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

fig = plt.figure()

# lines to plot
x = np.arange(0, 10)
y = np.arange(0, 10)

# gridspec for 2 rows, 2 cols with no space between
grid = gridspec.GridSpec(nrows=2, ncols=2, hspace=0, wspace=0, figure=fig)

x_y = fig.add_subplot(grid[0, 1], zorder=3)
x_y.plot(x, y)
x_y.margins(0)

invx_y = fig.add_subplot(grid[0, 0], zorder=2, sharey=x_y)
invx_y.plot(-x, y)
invx_y.margins(0)

invx_invy = fig.add_subplot(grid[1, 0], zorder=0, sharex=invx_y)
invx_invy.plot(-x, -y)
invx_invy.margins(0)

x_invy = fig.add_subplot(grid[1, 1], zorder=1, sharey=invx_invy, sharex=x_y)
x_invy.plot(x, -y)
x_invy.margins(0)

# clean up overlapping ticks
invx_y.tick_params(labelleft=False, length=0)
invx_invy.tick_params(labelleft=False, labelbottom=False, length=0)
x_invy.tick_params(labelbottom=False, length=0)

x_y.set_xticks(x_y.get_xticks()[1:-1])
invx_y.set_xticks(invx_y.get_xticks()[1:-1])
x_invy.set_yticks(x_invy.get_yticks()[1:-1])

plt.show()

这会产生下图:

4 plots shared axes 2x2 gridspec example

关于python - Matplotlib:倒置图的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58699063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com