【问题标题】:How can joint or connect or merge 2 or more plots together continuously in python?如何在python中连续连接或连接或合并2个或多个图?
【发布时间】:2019-08-12 06:40:51
【问题描述】:

我有一些基于时间序列的不同测量点的 RNN 输出图,我可以单独绘制它们,但我想知道是否可以 connect/joint/merge 2 个或更多图连续在一起?

以下是代码:

y_pred_test=model_RNN.predict(X_test)
df_test_RNN= pd.DataFrame.from_records(y_pred_test)

MSE=mean_squared_error(Y_test, y_pred_test)
print("Test MSE: " ,MSE)

print("Plot for 100 columns")
for i in range(40): #Here (3) cycles instead of (40) cycles for simplification   
    print("*"*50)
    print("Cycle"+" :"+ " "+str(i) )
    plt.plot(Y_test[i,:][0:100],'r-')
    plt.plot(y_pred_test[i][0:100],'b-')
    plt.show()

3 个时间步长或周期的结果:

预期结果(在 Windows 7 中由绘图手动创建的 3 个周期的连续图): 我试图最小化wspace,但没有以正确的方式工作。我还检查了这个answer,这不是我的情况,因为它打印在同一个图表中 - 在同一个图表中一个低于另一个(图像)。

如果有人可以帮助我,那就太好了!

【问题讨论】:

  • 您可以在循环之外创建一个列表 y_all 和 y_pred_all,并将每个“Y_test”和“y_pred_test”附加到这些列表中。然后在列表之后绘制 y_all 和 y_pred_all。
  • 设置wspace=0 似乎是一个不错的解决方案。什么不起作用?
  • @SerAlejo 如果您留下您的意见结构的 sn-p 以便我可以尝试,我将不胜感激,例如,您可以连续连接 3 个单独的正弦图吗?在您回复之前,我会尽力满足您的建议
  • @ImportanceOfBeingErnest 我通过wspace=0 意识到我仍然无法摆脱边框,尤其是右侧它不会让结果产品图连续显示或显示!
  • 设置右脊椎不可见,ax.spines["right"].set_visible(False)

标签: python matplotlib


【解决方案1】:

我构建了您要求使用正弦波的示例。 我不完全确定我们的数据是否具有相同的形状,但这应该让您了解如何解决这个问题:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# parameters for our sine wave
fs = 100 # sample rate 
f = 2 # the frequency of the signal

num_graphs = 3 #number of indiviudal graphs

x = []
y = []

# create 3 individual sine waves
for _ in range(num_graphs):
    x.append(np.arange(fs))
    y.append(np.sin(2*np.pi*f * (x[_]/fs)))

# create subplot for 3 indiviudal graphs
fig, axs = plt.subplots(nrows=1, ncols=3)

all_y = [] # create list to combine all y

for i in range(3): 
    # plot 3 individual curves
    axs[i].plot(x[i], y[i],'r-') # plot individual
    all_y.extend(y[i]) # extend all_y

这为我们提供了带有各个子图的图表:

# create new subplot for combined graph
fig, axs = plt.subplots(nrows=1, ncols=1)
axs.plot(range(len(all_y)), all_y)

在这里我们绘制我们的组合图像:

【讨论】:

  • 看起来很酷我很快就会尝试这个想法我也发现了类似的味道here似乎他们通过使用列表来合并图像。
猜你喜欢
  • 1970-01-01
  • 2019-12-13
  • 2020-09-13
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多