当我尝试使用以下代码显示 LSTM 和 RNN 模型预测结果时:
plt.figure(figsize=(5, 3))
plt.plot(y_test, c="orange", linewidth=3, label="Original values")
plt.plot(lstm_pred, c="red", linewidth=3, label="LSTM predictions")
plt.plot(rnn_pred, alpha=0.5, c="green", linewidth=3, label="RNN predictions")
plt.legend()
plt.xticks(rotation=45)
plt.title("Predictions vs actual data", fontsize=20)
plt.show()
如果我将它们一一绘制,线条将正确显示。
但是在一个图中显示所有线条,线条显示不正确。有人知道如何修复它吗?谢谢。
最佳答案
正如评论中提到的you need to create a second Y axis 。 然后你需要合并legend together
fig, ax1 = plt.subplots()
line1 = ax1.plot(y_test, c="orange", linewidth=3, label="Original values")
line2 = ax1.plot(rnn_pred, alpha=0.5, c="green", linewidth=3, label="RNN predictions")
plt.xticks(rotation=45)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
line3 = ax2.plot(lstm_pred, c="red", linewidth=3, label="LSTM predictions")
# added these three lines
lines = line1+line2+line3
labels = [l.get_label() for l in lines]
ax.legend(lns, labels)
plt.title("Predictions vs actual data", fontsize=20)
plt.show()
关于python - Matplotlib 绘制多条线不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59030621/