(matplotlib.pyplot) 散点图轴顺序错误 [英] wrong order in (matplotlib.pyplot) scatter plot axis

查看:80
本文介绍了(matplotlib.pyplot) 散点图轴顺序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注

我的结果:

缩放以查看轴:

代码:

将pandas导入为pd从 sklearn 导入 linear_model导入matplotlib.pyplot作为plt#读取数据dataframe = pd.read_fwf('brain_body.txt')x_values = dataframe [['Brain']]y_values = dataframe [['Body']]#在数据上训练模型body_reg = linear_model.LinearRegression()body_reg.fit(x_values,y_values)#可视化结果plt.scatter(x_values, y_values)plt.plot(x_values, body_reg.predict(x_values))plt.show()

brain_body.txt

 脑部3.385 44.5000.480 15.5001.350 8.100465.000 423.00036.330 119.50027.660 115.00014.830 98.2001.040 5.5004.190 58.0000.425 6.4000.101 4.0000.920 5.7001.000 6.6000.005 0.1400.060 1.0003.500 10.8002.000 12.3001.700 6.3002547.000 4603.0000.023 0.300187.100 419.000521.000 655.0000.785 3.50010.000 115.0003.300 25.6000.200 5.0001.410 17.500529.000 680.000207.000 406.00085.000 325.0000.750 12.30062.000 1320.0006654.000 5712.0003.500 3.9006.800 179.00035.000 56.0004.050 17.0000.120 1.0000.023 0.4000.010 0.2501.400 12.500250.000 490.0002.500 12.10055.500 175.000100.000 157.00052.160 440.00010.550 179.5000.550 2.40060.000 81.0003.600 21.0004.288 39.2000.280 1.9000.075 1.2000.122 3.0000.048 0.330192.000 180.0003.000 25.000160.000 169.0000.900 2.6001.620 11.4000.104 2.5004.235 50.400

这是我第一次使用python,我认为某些模块的安装存在问题,但是我没有任何线索.

解决方案

您要绘制值 plt.scatter(x_values.values,y_values.values).对数据进行排序也很有意义,以获得平滑的线条.

将 numpy 导入为 np将熊猫作为pd导入从sklearn导入linear_model导入matplotlib.pyplot作为plt#读取数据数据框= pd.read_fwf('data/brainbody.txt')x_values = 数据框[['Brain']]y_values = dataframe [['Body']]#在数据上训练模型body_reg = linear_model.LinearRegression()body_reg.fit(x_values,y_values)#可视化结果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()

I'm following this linear regression example but my result differs from what should be. The problem is in the plot axis, they are not in order.

expected:

my result:

zoom to see the axis:

The Code:

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

It is my first time in python and I think there is a problem with the installation of some module but I don't have a clue.

解决方案

You want to plot the values, plt.scatter(x_values.values, y_values.values). It would also make sense to sort the data, to get a smooth line.

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

这篇关于(matplotlib.pyplot) 散点图轴顺序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆