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

如你所见,它是不同象限中的图的组合。
我知道如何单独生成每个象限。例如,对于'x invert‘象限的绘图,我会简单地使用:
plt.plot(x, y)
plt.gca().invert_yaxis()
plt.show()来绘制这个图。它可以正确地反转x轴。但是,它只会为我生成左上角象限的图。
如何生成上图中描述的图的组合?每个象限都有自己的图,带有不同的倒转轴。
我最好的想法是将它合并到像画图这样的工具中。
发布于 2019-11-05 03:35:11
我没有足够的名气来在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()这将产生以下图:

https://stackoverflow.com/questions/58699063
复制相似问题