python matplotlib 只会绘制整数

发布于 2024-09-11 13:07:28 字数 385 浏览 4 评论 0原文

我对 python 很陌生。两天。尝试使用 matplotlib 绘制绘图。我收到此错误:

无法使用灵活类型执行reduce

出现错误的行是:

ax.scatter(x,y,z,marker='o')

变量:

ax 定义为:ax = Axes3D(fig)

Fig 定义为:fig = plt.figure()

x, y, z 是列表

有Python专家告诉我为什么吗?我的情节可以工作,但值是整数。首选十进制值。

我读到 python 中的列表不太适合数字...有人可以澄清一下吗?我让它可以处理列表和 ndarray,但所有值都必须是整数。

谢谢!

I'm very new to python. two days. trying to get a plot working with matplotlib. I'm getting this error:

cannot perform reduce with flexible type

the line with the error is:

ax.scatter(x,y,z,marker='o')

Variables:

ax is defined as: ax = Axes3D(fig)

fig is defined as: fig = plt.figure()

x, y, z are lists

Any python experts tell me why? I got the plot to work but the values are integers. Decimal values are preferred.

I read that lists in python are not very good with numbers... can someone clarify? I got it to work with lists and ndarrays but all the values have to be integers.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛清羽墨安 2024-09-18 13:07:28

将您的代码与此基本散点图代码进行比较。请注意,xsyszs 是浮点数的 ndarray。该代码对您有用吗?也许您可以逐步将该代码转变为您自己的代码,以获得有效的代码(或了解您的代码在哪里出现问题)。

如果这没有帮助,也许可以发布足够的代码来让我们重现问题。至少,发布的输出

print(repr(x))
print(repr(y))
print(repr(z))

Compare your code with this basic scatter plot code. Notice that there xs,ys,zs are ndarrays of floats. Does that code work for you? Maybe you can incrementally morph that code into your own to get code that works (or learn where yours breaks).

If that doesn't help, perhaps post enough code to allow us to reproduce the problem. At the very least, post the output of

print(repr(x))
print(repr(y))
print(repr(z))
故事灯 2024-09-18 13:07:28

感谢您的回复。

我让它与浮动一起工作,但也许你们都可以告诉我为什么。我刚刚在这里创建了一个帐户,无法编辑我的访客问题...

我正在从 sqlite 数据库获取数据。我可以将 sql 中的值舍入或转换为浮点数,并且它可以工作。我猜测数据是一个数字对象,尽管它是数字的。这是正确的吗?

这是不起作用的代码的摘录。我通过将值转换为浮点数来使其工作。


import sqlite3
import re
import numpy as np
from numpy import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

conn = sqlite3.connect('./RES/resDB.dbl')

fig = plt.figure()
ax = Axes3D(fig)

sam = array([])
x = array([])
y = array([])
z = array([])

for sv in conn.execute('select sam,easting,northing,altitude from trx_all'):
    sam = np.append(sam, [sv[0]]) #this works: sam = np.append(sam, [float(sv[0])])
    x = np.append(x, [sv[1]]) #this works: x = np.append(x, [float(sv[1])])
    y = np.append(y, [sv[2]]) #this works: y = np.append(y, [float(sv[2])])
    z = np.append(z, [sv[3]]) #this works: z = np.append(z, [float(sv[3])])
if sam.__len__() > 0:
    print(repr(x))
    print(repr(y))
    print(repr(z))
    ax.scatter(x, y, z, marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title("3D plot")
plt.show()

unutbu 请求 print(repr(x)) 的输出:


x: array([u'227004.744896075', u'227029.694213685', u'227051.351657645', ...,
       u'226416.484484696', u'226436.436477995', u'226401.253669657'],
      dtype='U16')
y: array([u'3648736.75423911', u'3648737.3357031', u'3648746.34280013', ...,
       u'3645870.67565062', u'3645893.61216942', u'3645866.95833722'],
      dtype='U16')
z: array([u'1600.2589433603', u'1535.77224937826', u'1429.01016684435', ...,
       u'341.064685015939', u'343.452275622636', u'312.491347198375'],
      dtype='U16')

最后这是它在 ax.scatter(x,y,z,marker='o') 行上输出的错误:


Traceback (most recent call last):
  File "C:\stripped_3dplot.py", line 27, in 
    ax.scatter(x, y, z, marker='o')
  File "C:\Python27\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1019, in scatter
    patches = Axes.scatter(self, xs, ys, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 5813, in scatter
    minx = np.amin(temp_x)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 1846, in amin
    return amin(axis, out)
TypeError: cannot perform reduce with flexible type

就像我说的,我认为这只是错误的类型,但是如果你们都可以告诉我是什么类型导致了错误,我可以学到一些东西。

谢谢。

Thanks for the responses.

I got it working with floats but maybe you all can tell me why. I just created an account here and can't edit my guest question...

I'm getting my data from a sqlite database. I can round the values in the sql or cast to floats and it works. I'm guessing that the data was a numerical object even though it was numerical. Is this correct?

Here's an excerpt of the code that doesn't work. I got it to work by casting the values as floats.


import sqlite3
import re
import numpy as np
from numpy import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

conn = sqlite3.connect('./RES/resDB.dbl')

fig = plt.figure()
ax = Axes3D(fig)

sam = array([])
x = array([])
y = array([])
z = array([])

for sv in conn.execute('select sam,easting,northing,altitude from trx_all'):
    sam = np.append(sam, [sv[0]]) #this works: sam = np.append(sam, [float(sv[0])])
    x = np.append(x, [sv[1]]) #this works: x = np.append(x, [float(sv[1])])
    y = np.append(y, [sv[2]]) #this works: y = np.append(y, [float(sv[2])])
    z = np.append(z, [sv[3]]) #this works: z = np.append(z, [float(sv[3])])
if sam.__len__() > 0:
    print(repr(x))
    print(repr(y))
    print(repr(z))
    ax.scatter(x, y, z, marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title("3D plot")
plt.show()

unutbu requested the output of print(repr(x)):


x: array([u'227004.744896075', u'227029.694213685', u'227051.351657645', ...,
       u'226416.484484696', u'226436.436477995', u'226401.253669657'],
      dtype='U16')
y: array([u'3648736.75423911', u'3648737.3357031', u'3648746.34280013', ...,
       u'3645870.67565062', u'3645893.61216942', u'3645866.95833722'],
      dtype='U16')
z: array([u'1600.2589433603', u'1535.77224937826', u'1429.01016684435', ...,
       u'341.064685015939', u'343.452275622636', u'312.491347198375'],
      dtype='U16')

and finally here's the error that it outputs on the line ax.scatter(x,y,z,marker='o') :


Traceback (most recent call last):
  File "C:\stripped_3dplot.py", line 27, in 
    ax.scatter(x, y, z, marker='o')
  File "C:\Python27\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1019, in scatter
    patches = Axes.scatter(self, xs, ys, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 5813, in scatter
    minx = np.amin(temp_x)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 1846, in amin
    return amin(axis, out)
TypeError: cannot perform reduce with flexible type

Like i said i think it was just the wrong type, but if you all can tell me what type causes the error i could learn something.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文