numpy 通过matplotlib重新塑造两个列表[duplicate]

jmp7cifd  于 2023-06-23  发布在  其他
关注(0)|答案(2)|浏览(107)

此问题已在此处有答案

Python reshape list to ndim array(4个答案)
9天前关闭
我有两个清单:

Predict = 
 [50.47426986694336, 50.587158203125, 50.58975601196289, 50.911197662353516]

Target =
 [[51.339996337890625, 51.79999923706055, 51.09000015258789, 64.86000061035156]]

我怎样才能重塑其中一个是相同的形状,以其他,以便我可以绘制两个名单,请?

r8xiu3jd

r8xiu3jd1#

假设 Target 中的双冒号是一个错字,你可以这样做:

import numpy as np

   np.reshape(Predict, (1, 4))

这导致:

[[50.47426987, 50.5871582 , 50.58975601, 50.91119766]]
bgtovc5b

bgtovc5b2#

下面是我的代码测试我这边这个问题。

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

相关问题