重塑两个列表以供Matplotlib绘制。

huangapple go评论65阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年6月13日 05:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定