gpt4 book ai didi

python - 绘制 Matplotlib 中的缺陷

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:59 25 4
gpt4 key购买 nike

使用 x,y 坐标的散点图建议在 Matplotlib 中绘制不同于使用其他程序获得的图。例如,这里是一些 PCA 对两个拟合分数的结果。使用 R 的相同图形和相同的数据提供不同的显示……我还检查了 Excell 和 Libreoffice:它们提供了与 R 相同的显示。在反对 Matplotlib 或报告错误之前,我想听听其他意见并检查我是否做得好。我的缺点是什么?

我检查了 float 不是问题,检查了类似的坐标顺序,......所以用 R 绘图:

mydata = read.csv("C:/Users/Anon/Desktop/data.txt")  # read csv file
summary(mydata)
attach(mydata)
plot(mydata)

R 制作的散点图 enter image description here

使用 Matplotlib 绘制的相同数据:

import matplotlib.pyplot as mpl
import numpy as np
import os
# open the file with PCA results and convert it into float
file_data = os.getcwd() + "\\data.txt"
F = open(file_data, 'r')
DATA=F.readlines()
F.close()
for x in range(len(DATA)) :
a = DATA[x]
b = a.split(',')
DATA[x] = b
for i in xrange(len(DATA)):
for j in xrange(len(DATA[i])):
DATA[i][j] = float(DATA[i][j])
print DATA[0]
X_train = np.mat(DATA)
print "X_train\n",X_train

mpl.scatter(X_train[:, 0], X_train[:, 1], c='white')
mpl.show()

scatter plot made by Matplotlib和打印 X_train 的结果(因此您可以验证数据是否相同) enter image description here使用 Excell: enter image description here

数据:(我放不下所有数据,请告诉我如何加入*.txt文件~40.5 Ko)

0.02753547770433    -0.037999362802379
0.05179194064903 0.0257492713593311
-0.0272928319004863 0.0065143681863637
0.0891355504379135 -0.00801696955147688
0.0946809371499167 -0.00502202338807476
-0.0445799941736001 -0.0435759273767196
-0.333617999778119 -0.204222004815357
-0.127212025425053 -0.110264460064754
-0.0243459270896855 -0.0622273166478512
0.0497080821876597 0.0272080474151131
-0.181221703468915 -0.134945934382777
-0.0699503258694739 -0.0835239795690277

编辑:所以我还把 PCA 数据(从 scipy)导出到一个文本文件中,并用 python/matplotlib 和 R 打开这个通用文本文件,以避免一些与 PCA 相关的 prblms。绘图是在该处理之后绘制的(PCA 之前的图形看起来像一个圆顶)

edit2:使用 numpy.loadtxt(),它显示为 R,但我的自定义方法和 numpy.loadtxt() 提供了相同的数据形状、大小、类型和值,那么涉及的机制是什么?

X_train numpy.loadtxt()
[[ 0.02753548 -0.03799936]
[ 0.05179194 0.02574927]
[-0.02729283 0.00651437]
...,
[ 0.02670961 -0.00696177]
[ 0.09011859 -0.00661216]
[-0.04406559 0.09285291]]
shape and size
(1039L, 2L) 2078

X_train custom-method
[[ 0.02753548 -0.03799936]
[ 0.05179194 0.02574927]
[-0.02729283 0.00651437]
...,
[ 0.02670961 -0.00696177]
[ 0.09011859 -0.00661216]
[-0.04406559 0.09285291]]
shape and size
(1039L, 2L) 2078

最佳答案

问题是您将 X_train 表示为矩阵而不是二维数组。这意味着当您使用 X_train[:, 0] 对它进行子集化时,您得到的不是一维数组 - 您得到的是一个只有一列的矩阵(matplotlib 然后尝试分散)。您可以通过打印 X_train[:, 0] 来亲眼看到。*

您只需更改以下行即可解决问题:

X_train = np.mat(DATA)

X_train = np.array(DATA)

*例如,在您发布的数据上,X_train[:, 0] 是:

[[ 0.02753548]
[ 0.05179194]
[-0.02729283]
[ 0.08913555]
[ 0.09468094]
[-0.04457999]
[-0.333618 ]
[-0.12721203]
[-0.02434593]
[ 0.04970808]
[-0.1812217 ]
[-0.06995033]]

关于python - 绘制 Matplotlib 中的缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16085703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com