I tried to bundle data using Pandas and draw it with maplotlib, but an error occurred.
How do I solve this error? TypeError: Cannot cast array data from dtype((numpy.record, [('x', '<i8'), ('y', '<i8')])) to dtype('float64')
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import PolyCollectiona = [ {'x': 1, 'y': 2}, {'x': 0, 'y': 1}, {'x': 1, 'y': 0}, {'x': 2, 'y': 1}, {'x': 3, 'y': 2}, {'x': 2, 'y': 1}, {'x': 3, 'y': 0}, {'x': 4, 'y': 1},]b = pd.DataFrame(a)c = np.array(b[['x', 'y']].to_records(index=False))fig, ax = plt.subplots(1, 1)ax.add_collection(PolyCollection(c))plt.show()
Traceback (most recent call last): File "..\test.py", line 80, in <module> ax.add_collection(PolyCollection(c)) ^^^^^^^^^^^^^^^^^ File "..\Python\Python311\site-packages\matplotlib\collections.py", line 1200, in __init__ self.set_verts(verts, closed) File "..\Python\Python311\site-packages\matplotlib\collections.py", line 1241, in set_verts self._paths.append(mpath.Path._create_closed(xy)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "..\Python\Python311\site-packages\matplotlib\path.py", line 199, in _create_closed v = _to_unmasked_float_array(vertices) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "..\Python\Python311\site-packages\matplotlib\cbook.py", line 1398, in _to_unmasked_float_array return np.asarray(x, float) ^^^^^^^^^^^^^^^^^^^^TypeError: Cannot cast array data from dtype((numpy.record, [('x', '<i8'), ('y', '<i8')])) to dtype('float64').