两个 Numpy 数组的折线图 matplotlib
Line chart Of two Numpy arrays matplotlib
import matplotlib.pyplot as plt
plt.plot(x_test,y_pred)
print(x_test)
print()
print(y_pred)
print()
print(y_test)
plt.plot(x_test,y_test)
plt.show()
我正在尝试使用以上 3 个数组绘制折线图,所有数组的形状都相同,但由于某种原因,折线图仍然很乱。我还是菜鸟,谁能告诉我错误吗?
输出:
[31.588117 53.66093226 46.68222865 43.10782022 70.34607562 44.49285588
57.5045333 36.93007661 55.80573336 38.95476907 56.9012147 56.86890066
34.3331247 59.04974121 57.78822399 54.28232871 51.0887199 50.28283635
44.21174175 38.00548801 32.94047994 53.69163957 68.76573427 46.2309665
68.31936082 50.03017434 49.23976534 50.03957594 48.14985889 25.12848465]
[ 48.07285783 77.96480957 68.51395026 63.67333329 100.56054527
65.5490079 83.16997871 55.30716832 80.86939107 58.04909356
82.35293882 82.30917777 51.79026483 85.26256544 83.55416479
78.80633125 74.48140943 73.39004741 65.1683111 56.76353649
49.90428565 78.00639472 98.42037934 67.90283173 97.8158813
73.0478817 71.97747598 73.06061375 70.50147799 39.32494706]
[ 50.39267014 63.64239878 72.24725107 57.81251298 104.25710159
86.64202032 91.486778 55.23166089 79.55043668 44.84712424
80.20752314 83.14274979 55.72348926 77.63418251 99.05141484
79.12064627 69.58889785 69.51050331 73.68756432 61.36690454
67.17065577 85.66820315 114.85387123 90.12357207 97.91982104
81.53699078 72.11183247 85.23200734 66.22495789 53.45439421]
由于你的x轴没有排序,线会很乱。它准确地绘制了你告诉它的情节。如果您想要折线图,则需要先对数据进行排序(同时确保对其他向量进行相应排序)。例如,您可以使用 Pandas 轻松做到这一点。
import pandas as pd
df = pd.DataFrame({'x_test': x_test,
'y_test': y_test,
'y_pred': y_pred})
df.sort_values('x_test', inplace=True)
plt.plot(df.x_test, df.y_pred)
plt.plot(df.x_test, df.y_test)
plt.show()
或者,如前所述,您最好使用散点图:
plt.figure()
plt.plot(df.x_test, df.y_pred)
plt.scatter(df.x_test, df.y_test)
plt.show()
import matplotlib.pyplot as plt
plt.plot(x_test,y_pred)
print(x_test)
print()
print(y_pred)
print()
print(y_test)
plt.plot(x_test,y_test)
plt.show()
我正在尝试使用以上 3 个数组绘制折线图,所有数组的形状都相同,但由于某种原因,折线图仍然很乱。我还是菜鸟,谁能告诉我错误吗?
输出:
[31.588117 53.66093226 46.68222865 43.10782022 70.34607562 44.49285588
57.5045333 36.93007661 55.80573336 38.95476907 56.9012147 56.86890066
34.3331247 59.04974121 57.78822399 54.28232871 51.0887199 50.28283635
44.21174175 38.00548801 32.94047994 53.69163957 68.76573427 46.2309665
68.31936082 50.03017434 49.23976534 50.03957594 48.14985889 25.12848465]
[ 48.07285783 77.96480957 68.51395026 63.67333329 100.56054527
65.5490079 83.16997871 55.30716832 80.86939107 58.04909356
82.35293882 82.30917777 51.79026483 85.26256544 83.55416479
78.80633125 74.48140943 73.39004741 65.1683111 56.76353649
49.90428565 78.00639472 98.42037934 67.90283173 97.8158813
73.0478817 71.97747598 73.06061375 70.50147799 39.32494706]
[ 50.39267014 63.64239878 72.24725107 57.81251298 104.25710159
86.64202032 91.486778 55.23166089 79.55043668 44.84712424
80.20752314 83.14274979 55.72348926 77.63418251 99.05141484
79.12064627 69.58889785 69.51050331 73.68756432 61.36690454
67.17065577 85.66820315 114.85387123 90.12357207 97.91982104
81.53699078 72.11183247 85.23200734 66.22495789 53.45439421]
由于你的x轴没有排序,线会很乱。它准确地绘制了你告诉它的情节。如果您想要折线图,则需要先对数据进行排序(同时确保对其他向量进行相应排序)。例如,您可以使用 Pandas 轻松做到这一点。
import pandas as pd
df = pd.DataFrame({'x_test': x_test,
'y_test': y_test,
'y_pred': y_pred})
df.sort_values('x_test', inplace=True)
plt.plot(df.x_test, df.y_pred)
plt.plot(df.x_test, df.y_test)
plt.show()
或者,如前所述,您最好使用散点图:
plt.figure()
plt.plot(df.x_test, df.y_pred)
plt.scatter(df.x_test, df.y_test)
plt.show()