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

时间:2017-10-12 18:57:52

标签: python list plot graph

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

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

1 个答案:

答案 0 :(得分: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