gpt4 book ai didi

python - 使用 python 中的坐标列表和多数组错误绘制图形

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

我想使用这样的坐标列表来绘制图表:

a = [<x1>, <y1>, <x2>, <y2>, …]

这是我使用的算法:

import matplotlib.pyplot as plt
import numpy as np
a = [<x1>, <y1>, <x2>, <y2>, ….]
x,y = np.array(a).reshape((len(a)/2, 2)).transpose()
plt.plot(x,y)

我失败并出现一个奇怪的错误,根本错误是:

ImportError: cannot import name multiarray

那么这里有什么问题呢。我必须更改我的算法,或者是否有其他方法使用坐标列表绘制图形。

谢谢;

这就是整个堆栈跟踪:

/usr/bin/python3.3 /cs/usr/mohammadja/grph/graph.py
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 16, in <module>
from . import multiarray
ImportError: cannot import name multiarray

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/cs/usr/mohammadja/grph/graph.py", line 1, in <module>
import matplotlib.pyplot as plt
File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 122, in <module>
from matplotlib.cbook import is_string_like, mplDeprecation, dedent, get_label
File "/usr/lib/python3/dist-packages/matplotlib/cbook.py", line 32, in <module>
import numpy as np
File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/usr/lib/python3/dist-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python3/dist-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/usr/lib/python3/dist-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 24, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.


Process finished with exit code 1

最佳答案

无法帮助解决损坏的 numpy 但这适用于您的列表:

a = ['x1', 'y1', 'x2', 'y2', 'x3', 'y3']
x, y = a[::2], a[1::2]

x
Out[64]: ['x1', 'x2', 'x3']

y
Out[65]: ['y1', 'y2', 'y3']

切片索引是不可知的,也适用于 np.array

ar = np.array(a)

x, y = ar[::2], ar[1::2]

x
Out[72]:
array(['x1', 'x2', 'x3'],
dtype='<U2')

y
Out[73]:
array(['y1', 'y2', 'y3'],
dtype='<U2')

重新评论,你指的是情节吗?

import matplotlib.pyplot as plt
# import numpy as np # not used here but same code works with np.array too


a = [*range(10)]
x, y = x, y = a[::2], a[1::2]
plt.plot(x, y)
print(x, y)
[0, 2, 4, 6, 8] [1, 3, 5, 7, 9]

enter image description here

关于python - 使用 python 中的坐标列表和多数组错误绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717018/

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