使用python绘制两个大列表-“不可散列类型:'numpy.ndarray'”

时间:2018-11-06 18:17:27

标签: python numpy matplotlib

我正在尝试从日志中读取图像,并根据获得图像的距离绘制一些距离数据的图形,如下所示:

import time
import numpy as np
import re
import matplotlib.pyplot as plt
from scipy.spatial import distance


log_path = 'path/to/log/2018-10-31_log.txt'


log = open (log_path, "r")
lines = log.readlines()
i = 0
distances1 = []
distances2 = []
image_numbers = []
for line in lines:
  image_name = (line.split()[2])
  image_number = re.search('_image(.*).jpg',image_name)
  distance1 = float(line.split()[5])
  distance2 = float(line.split()[8])
  distances1.append(distance1)
  distances2.append(distance2)
  image_numbers.append(image_number.group(1))

plt.plot([distance1],[image_numbers])
plt.show()

但是,在运行代码时,我遇到以下错误:

    Traceback (most recent call last):
  File "read_ultrasonic_log.py", line 27, in <module>
    plt.plot([distance1],[image_numbers])
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3358, in plot
    ret = ax.plot(*args, **kwargs)
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.py", line 1855, in inner
    return func(ax, *args, **kwargs)
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 1527, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 406, in _grab_next_args
    for seg in self._plot_args(this, kwargs):
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 383, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 216, in _xy_from_xy
    by = self.axes.yaxis.update_units(y)
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/axis.py", line 1469, in update_units
    default = self.converter.default_units(data, self)
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/category.py", line 115, in default_units
    axis.set_units(UnitData(data))
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/category.py", line 182, in __init__
    self.update(data)
  File "/home/intuitive/anaconda2/lib/python2.7/site-packages/matplotlib/category.py", line 199, in update
    for val in OrderedDict.fromkeys(data):
  File "/home/intuitive/anaconda2/lib/python2.7/collections.py", line 216, in fromkeys
    self[key] = value
  File "/home/intuitive/anaconda2/lib/python2.7/collections.py", line 75, in __setitem__
    if key not in self:
TypeError: unhashable type: 'numpy.ndarray'

distance1和image_numbers列表确实很大,每个列表中都有超过16000个值,但是我不知道如何处理此错误。我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

我看到了plt.plot([distance1],[image_numbers])。我认为您可以通过删除括号并使用distances1plt.plot(distances1,image_numbers)

来解决此问题。