简体   繁体   中英

Graphing two large lists using python - "unhashable type: 'numpy.ndarray'"

I am trying to read from a log and graph some distance data against the image from which it is obtained like so:

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()

However on running the code I end up with the following error:

    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'

the distance1 and image_numbers lists are indeed quite large with over 16000 values in each, but I do not understand how to approach this error. What am I missing here?

I see plt.plot([distance1],[image_numbers]) . I think you can fix the problem by removing the brackets like so and using distances1 : plt.plot(distances1,image_numbers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM