(matplotlib.pyplot)散点图坐标轴顺序错误

5
我正在按照这个线性回归示例进行操作,但我的结果与预期不符。问题出在绘图坐标轴上,它们没有按顺序排列。
预期结果: enter image description here 我的结果: enter image description here 放大图片以查看坐标轴: enter image description here 代码如下:
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt

#read data
dataframe = pd.read_fwf('brain_body.txt')
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]

#train model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)

#visualize results
plt.scatter(x_values, y_values)
plt.plot(x_values, body_reg.predict(x_values))
plt.show()

brain_body.txt

Brain        Body
    3.385    44.500
    0.480    15.500
    1.350     8.100
  465.000   423.000
   36.330   119.500
   27.660   115.000
   14.830    98.200
    1.040     5.500
    4.190    58.000
    0.425     6.400
    0.101     4.000
    0.920     5.700
    1.000     6.600
    0.005     0.140
    0.060     1.000
    3.500    10.800
    2.000    12.300
    1.700     6.300
 2547.000  4603.000
    0.023     0.300
  187.100   419.000
  521.000   655.000
    0.785     3.500
   10.000   115.000
    3.300    25.600
    0.200     5.000
    1.410    17.500
  529.000   680.000
  207.000   406.000
   85.000   325.000
    0.750    12.300
   62.000  1320.000
 6654.000  5712.000
    3.500     3.900
    6.800   179.000
   35.000    56.000
    4.050    17.000
    0.120     1.000
    0.023     0.400
    0.010     0.250
    1.400    12.500
  250.000   490.000
    2.500    12.100
   55.500   175.000
  100.000   157.000
   52.160   440.000
   10.550   179.500
    0.550     2.400
   60.000    81.000
    3.600    21.000
    4.288    39.200
    0.280     1.900
    0.075     1.200
    0.122     3.000
    0.048     0.330
  192.000   180.000
    3.000    25.000
  160.000   169.000
    0.900     2.600
    1.620    11.400
    0.104     2.500
    4.235    50.400

我第一次使用Python,但似乎遇到了模块安装的问题,具体原因不得而知。


2
请发布您的代码。 - James
如果问题只在使用 brain_body.txt 时出现,您需要提供此文件。如果问题不依赖于使用 brain_body.txt,则提供一个没有它的 [mcve]。 - ImportanceOfBeingErnest
1
我在代码中放了一个文件链接,但我知道问题不在代码中,因为它在老师的电脑(OSX)上可以运行,但在我的电脑(Windows)上却不能。我觉得可能是某个模块的问题,但我不知道如何检查它们。我得到了一个退出码0,没有堆栈跟踪。 - Pablo
为什么这个问题被关闭为重复?它有一个重要的区别。Y轴上的数字没有按顺序排列 - 这正是我今天遇到的问题。所谓的重复问题并没有显示这种奇怪的情况。 - DarenW
1
我也遇到了相同的问题,即Y轴上出现了奇怪的、无序的数值。但是我没有使用Pandas或进行线性拟合,只是绘制x、y值。在追踪了几个小时的野鹅之后,我终于发现了罪魁祸首:从文本文件中读取的值从未转换为浮点数,即x = float(linefromfile.split[0])。一旦我加入了float()转换,图形就正常显示了。这是避免使用弱类型语言的另一个原因! - DarenW
DarenW肯定有正确的解决方案。当使用plt.scatter时,如果传递的值是字符串而不是浮点数,则会出现此行为。 - PaulG
1个回答

5

您想要绘制值,可以使用plt.scatter(x_values.values,y_values.values)。此外,排序数据也是有意义的,以获得平滑的线条。

import numpy as np
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt

#read data
dataframe = pd.read_fwf('data/brainbody.txt')
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]

#train model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)

#visualize results
plt.scatter(x_values.values, y_values.values)

x = np.sort(x_values.values.flatten())
plt.plot(x, body_reg.predict(x[:,np.newaxis]))

plt.show()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接