gpt4 book ai didi

python - 如何在单个子图中绘制图形列表?

转载 作者:行者123 更新时间:2023-12-04 08:13:24 25 4
gpt4 key购买 nike

我有 2 个数字列表及其轴。
我需要在单个子图中绘制每个图形,以便这些图形成为一个大的子图中。我怎样才能做到这一点?
我试过 for 循环,但没有用。
这是我尝试过的:

import ruptures as rpt
import matplotlib.pyplot as plt

# make random data with 100 samples and 9 columns
n_samples, n_dims, sigma = 100, 9, 2
n_bkps = 4
signal, bkps = rpt.pw_constant(n_samples, n_dims, n_bkps, noise_std=sigma)

figs, axs = [], []
for i in range(signal.shape[1]):
points = signal[:,i]
# detection of change points
algo = rpt.Dynp(model='l2').fit(points)
result = algo.predict(n_bkps=2)
fig, ax = rpt.display(points, bkps, result, figsize=(15,3))
figs.append(fig)
axs.append(ax)
plt.show()
enter image description here
enter image description here

最佳答案

我看了source code of ruptures.display() ,并且它接受传递给 matplotlib 的 **kwargs。这允许我们将输出重定向到单个图形,并且使用 gridspec,我们可以在该图形中定位单个子图:

import ruptures as rpt
import matplotlib.pyplot as plt

n_samples, n_dims, sigma = 100, 9, 2
n_bkps = 4
signal, bkps = rpt.pw_constant(n_samples, n_dims, n_bkps, noise_std=sigma)

#number of subplots
n_subpl = signal.shape[1]
#give figure a name to refer to it later
fig = plt.figure(num = "ruptures_figure", figsize=(8, 15))
#define grid of nrows x ncols
gs = fig.add_gridspec(n_subpl, 1)


for i in range(n_subpl):
points = signal[:,i]
algo = rpt.Dynp(model='l2').fit(points)
result = algo.predict(n_bkps=2)
#rpt.display(points, bkps, result)
#plot into predefined figure
_, curr_ax = rpt.display(points, bkps, result, num="ruptures_figure")
#position current subplot within grid
curr_ax[0].set_position(gs[i].get_position(fig))
curr_ax[0].set_subplotspec(gs[i])

plt.show()
示例输出:
enter image description here

关于python - 如何在单个子图中绘制图形列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65837926/

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