英文:
Reshape two lists to be plotted by matplotlib
问题
我有这两个列表:
Predict = [50.47426986694336, 50.587158203125, 50.58975601196289, 50.911197662353516]
Target = [[51.339996337890625, 51.79999923706055, 51.09000015258789, 64.86000061035156]]
我如何将它们中的一个重新调整为与另一个相同的形状,以便我可以绘制这两个列表?
英文:
I have this two lists:
Predict =
[50.47426986694336, 50.587158203125, 50.58975601196289, 50.911197662353516]
Target =
[[51.339996337890625, 51.79999923706055, 51.09000015258789, 64.86000061035156]]
How can I reshape one of them to be the same shape to the other so that I can plot the two lists please?
答案1
得分: 0
import numpy as np
np.reshape(Predict, (1, 4))
这将得到:
[[50.47426987, 50.5871582 , 50.58975601, 50.91119766]]
英文:
Assuming, that the doubled colon in Target is a typo, you could do:
import numpy as np
np.reshape(Predict, (1, 4))
This results in:
[[50.47426987, 50.5871582 , 50.58975601, 50.91119766]]
答案2
得分: 0
以下是您提供的代码的翻译部分:
这是我在这个问题上测试过的代码。
import numpy as np
import matplotlib.pyplot as plt
Predict = [50.47426986694336, 50.587158203125, 50.58975601196289, 50.911197662353516]
Target = [[51.339996337890625, 51.79999923706055, 51.09000015258789, 64.86000061035156]]
# 将Predict转换为NumPy数组并进行转置
predict_array = np.array(Predict).T
# 重塑Predict,使其具有与Target相同的形状
predict_reshaped = predict_array.reshape(Target.shape)
# 绘制两个列表
plt.plot(predict_reshaped.flatten(), label='Predict')
plt.plot(Target.flatten(), label='Target')
plt.legend()
plt.show()
英文:
Here is my code tested on my side for this question.
import numpy as np
import matplotlib.pyplot as plt
Predict = [50.47426986694336, 50.587158203125, 50.58975601196289, 50.911197662353516]
Target = [[51.339996337890625, 51.79999923706055, 51.09000015258789, 64.86000061035156]]
# Convert Predict to a NumPy array and transpose it
predict_array = np.array(Predict).T
# Reshape Predict to have the same shape as Target
predict_reshaped = predict_array.reshape(Target.shape)
# Plot both lists
plt.plot(predict_reshaped.flatten(), label='Predict')
plt.plot(Target.flatten(), label='Target')
plt.legend()
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论