【问题标题】:Python Plots - Plotting a subplots in a subplotsPython Plots - 在子图中绘制子图
【发布时间】:2020-07-27 11:04:24
【问题描述】:

我想根据不同的变量绘制一个表示变化的图表。示例图如下所示。

这个想法是在子图中绘制子图。注意它不同于使用预定义行数和列数的 subplot 绘制图形,即 matplotlib.pyplot.subplots(nrows=2, ncols=2)

我可以使用 matplotlib/seaborn 绘制这些数字吗?

【问题讨论】:

  • 这个想法是在一个子图中绘制子图,但另一个想法是在每两个子图周围画一个框架......你必须做所有自己的几何图形,但它应该可以完美地工作
  • 您尝试使用 inset_axes 吗?

标签: python matplotlib seaborn


【解决方案1】:

我已经绘制了框架并将轴放置在框架内,一切都基于没有。子图/框架,没有。框架网格的行列数以及不同元素的物理尺寸。

我想大部分代码都是不言自明的,除了我们将轴放置在精确位置的部分,这是从Demo Fixed Size Axes偷来的,如果你看到需要说明的地方,请询问

import matplotlib
from mpl_toolkits.axes_grid1 import Divider, Size
from mpl_toolkits.axes_grid1.mpl_axes import Axes
import matplotlib.pyplot as plt
import numpy as np
from itertools import product

mm = lambda d: d/25.4

nplots = 2
wp, hp = mm(40), mm(28)
dxp, dyp = mm(16), mm(12)

nrows, ncols = 3, 2
wf, hf = nplots*(wp+dxp), hp+dyp
dxf, dyf = mm(10), mm(8)

xcorners, ycorners = (np.arange(dxf/2,ncols*(wf+dxf),wf+dxf),
                      np.arange(dyf/2,nrows*(hf+dyf),hf+dyf))

# plus 10 mm for suptitle
fig = plt.figure(figsize=(ncols*(wf+dxf), nrows*(hf+dyf)+mm(10))) 

rect = lambda xy: plt.Rectangle(xy, wf, hf,
                                transform=fig.dpi_scale_trans,
                                figure=fig,
                                edgecolor='k', facecolor='none')
fig.patches.extend([rect(xy) for xy in product(xcorners, ycorners)])

t = np.linspace(0,3.14,315); s = np.sin(t)

for nframe, (y, x) in enumerate(product(ycorners, xcorners), 1):
    for n in range(nplots):
        divider = Divider(fig, (0.0, 0.0, 1., 1.),
                          [Size.Fixed(x+0.7*dxp+n*(wp+dxp)), Size.Fixed(wp)],
                          [Size.Fixed(y+0.7*dyp           ), Size.Fixed(hp)],
                          aspect=False)
        ax = Axes(fig, divider.get_position())
        ax.set_axes_locator(divider.new_locator(nx=1, ny=1))
        ax.plot(t, s)
        fig.add_axes(ax)
        fig.text(x, y, 'Frame %d'%nframe, transform=fig.dpi_scale_trans)

figsize = fig.get_size_inches()
width = figsize[0]*25.4 # mm
fig.suptitle('Original figure width is %.2f mm - everything is scaled'%width)
fig.savefig('pippo.png', dpi=118, facecolor='#f8f8f0')

【讨论】:

  • 感谢@gboffi 提供的漂亮且不言自明的代码。
【解决方案2】:

您需要使用 Matplotlib 来绘制这些图表

您可以按照以下示例使用图表创建自己的图形:

import matplotlib.pyplot as plt

plt.subplot(1, 2, 1)  # Args ( Lines, Columns, Reference )
plt.plot(x, y, 'r')  # Reference will say what graph we are modding
plt.subplot(1, 2, 2)
plt.plot(y, x, 'g')
plt.show()

代码将创建一个如下图:

您可以使用plt.xlabel('name')plt.ylabel('name')plt.title('name') 来定义图形的标签和标题

注意:上面的代码将创建一个带有 2 个图表的图像,您可以在另一个代码块中使用此代码来创建您想要的图像。

您也可以使用以下代码:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=5, ncols=5, figsize=(5, 5))
ax[0, 0].plot(x, y)  # The method ax is now one array and is referred by indexes
ax[0, 0].set_title('Title')
ax[1, 1].plot(x, y)
ax[1, 1].set_title('Title')
plt.tight_layout()  # It will separate the graphs to avoid overlays
plt.show()

它将创建以下图像:

【讨论】:

  • 您好...我们的想法不是使用子图绘制,而是绘制“子图中的子图”
猜你喜欢
  • 2022-08-16
  • 2019-02-05
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2021-11-17
相关资源
最近更新 更多