【问题标题】:Line chart Of two Numpy arrays matplotlib两个 Numpy 数组 matplotlib 的折线图
【发布时间】:2022-01-26 01:24:01
【问题描述】:
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 轴从 31 到 53,然后回到 46,你会看到橙色线就是这样做的。由于您的数据没有顺序,我建议使用散点图。
  • 作为 Chris 建议的替代方案,如果您真的很喜欢折线图,您可以使用用于 x 值的数组中的键对所有数组进行排序。见这里:stackoverflow.com/a/67020841/4365003
  • 对非时间数据使用线图可能是一个糟糕的设计选择。

标签: python numpy matplotlib linear-regression


【解决方案1】:

由于你的 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2019-01-19
    • 2021-02-25
    相关资源
    最近更新 更多