0

I am new to Python and trying to create a plot graph from a DataFrame.

I used the following piece of code:

predictions= list() 
for the in range (10):
    predicted= StartARIMAForecasting(RegistrationRates, 5,1,0) 
    predictions.append(predicted)
    RegistrationRates.append(predicted)

data = {'Year':['2016','2017','2018','2019','2020','2021','2022','2023','2024','2025'], 'Registration Rate':predictions}
resultdf = pd.DataFrame(data)
print(resultdf)

plt.xlabel('Year')
plt.ylabel('Registration Rate')
plt.plot(resultdf)

Following output is seen:

0  2016   [50.68501406476124]
1  2017   [52.41297372600995]
2  2018    [54.0703599343735]
3  2019   [53.58327982434545]
4  2020  [55.647237533704754]
5  2021  [54.398197822219714]
6  2022   [55.06459335430334]
7  2023   [56.00171430250292]
8  2024   [55.70449088032122]
9  2025    [57.7127557392168]

but blank graph is plotted with following error: TypeError: unhashable type: 'numpy.ndarray'

Full stack-trace is provided below:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-1d843e3f6a23> in <module>
     58 plt.xlabel('Year')
     59 plt.ylabel('Registration Rate')
---> 60 plt.plot(resultdf)
     61 
     62 root.mainloop()

~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args, **kwargs)
   3356                       mplDeprecation)
   3357     try:
-> 3358         ret = ax.plot(*args, **kwargs)
   3359     finally:
   3360         ax._hold = washold

~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1854                         RuntimeWarning, stacklevel=2)
-> 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs)
   1525         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
   1526 
-> 1527         for line in self._get_lines(*args, **kwargs):
   1528             self.add_line(line)
   1529             lines.append(line)

~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs)
    404                 this += args[0],
    405                 args = args[1:]
--> 406             for seg in self._plot_args(this, kwargs):
    407                 yield seg
    408 

~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    381             x, y = index_of(tup[-1])
    382 
--> 383         x, y = self._xy_from_xy(x, y)
    384 
    385         if self.command == 'plot':

~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
    214         if self.axes.xaxis is not None and self.axes.yaxis is not None:
    215             bx = self.axes.xaxis.update_units(x)
--> 216             by = self.axes.yaxis.update_units(y)
    217 
    218             if self.command != 'plot':

~\Anaconda3\lib\site-packages\matplotlib\axis.py in update_units(self, data)
   1467         neednew = self.converter != converter
   1468         self.converter = converter
-> 1469         default = self.converter.default_units(data, self)
   1470         if default is not None and self.units is None:
   1471             self.set_units(default)

~\Anaconda3\lib\site-packages\matplotlib\category.py in default_units(data, axis)
    113         # default_units->axis_info->convert
    114         if axis.units is None:
--> 115             axis.set_units(UnitData(data))
    116         else:
    117             axis.units.update(data)

~\Anaconda3\lib\site-packages\matplotlib\category.py in __init__(self, data)
    180         self._counter = itertools.count(start=0)
    181         if data is not None:
--> 182             self.update(data)
    183 
    184     def update(self, data):

~\Anaconda3\lib\site-packages\matplotlib\category.py in update(self, data)
    197         data = np.atleast_1d(np.array(data, dtype=object))
    198 
--> 199         for val in OrderedDict.fromkeys(data):
    200             if not isinstance(val, VALID_TYPES):
    201                 raise TypeError("{val!r} is not a string".format(val=val))

TypeError: unhashable type: 'numpy.ndarray'

1 Answer 1

3

If you check the type of your column 'Registration Rate', you will see that it's type of numpy.ndarray as shown in the error.

type(resultdf['Registration Rate'][0])

So, maybe modify your predictions creation to make it a single element?

predictions= list() 
for the in range (10):
    predicted= StartARIMAForecasting(RegistrationRates, 5,1,0) 
    # predicted is a numpy.ndarray, len = 1
    p = predicted[0]
    predictions.append(p)

Then run your code a gain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.