1

Not able to understand why I am getting this error and how to fix it. I am trying to plot a graph for number of listings in the neighborhood. I have tried uninstalling and then reinstalling pandas but it doesn't seem to work

By running the following code :

feq=listings['neighbourhood'].value_counts().sort_values(ascending=True)
feq.plot.barh(figsize=(10, 8), color='b', width=1)
plt.title("Number of listings by neighbourhood", fontsize=20)
plt.xlabel('Number of listings', fontsize=12)
plt.show()

This error is encountered :

ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected.
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
C:\Users\DIKSHA~1\AppData\Local\Temp/ipykernel_2704/2039381319.py in <module>
      1 feq=listings['neighbourhood'].value_counts().sort_values(ascending=True)
----> 2 feq.plot.barh(figsize=(10, 8), color='b', width=1)
      3 plt.title("Number of listings by neighbourhood", fontsize=20)
      4 plt.xlabel('Number of listings', fontsize=12)
      5 plt.show()

~\anaconda3\lib\site-packages\pandas\plotting\_core.py in barh(self, x, y, **kwargs)
   1197             >>> index = ['snail', 'pig', 'elephant',
   1198             ...          'rabbit', 'giraffe', 'coyote', 'horse']
-> 1199             >>> df = pd.DataFrame({'speed': speed,
   1200             ...                    'lifespan': lifespan}, index=index)
   1201             >>> ax = df.plot.barh(x='lifespan')

~\anaconda3\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
    873                 "arguments, only keyword arguments. The order of "
    874                 "positional arguments will change in the future. "
--> 875                 f"Use `Series.plot({keyword_args})` instead of "
    876                 f"`Series.plot({positional_args})`."
    877             )

~\anaconda3\lib\site-packages\pandas\plotting\_core.py in _get_plot_backend(backend)
   1784         f"Could not find plotting backend '{backend}'. Ensure that you've "
   1785         f"installed the package providing the '{backend}' entrypoint, or that "
-> 1786         "the package has a top-level `.plot` method."
   1787     )
   1788 

ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected.
Diksha Nasa
  • 135
  • 3
  • 13
  • Does this answer your question? [ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected](https://stackoverflow.com/questions/60977789/importerror-matplotlib-is-required-for-plotting-when-the-default-backend-matpl) – phuycke Aug 16 '21 at 09:55
  • @phuycke Yes I did try that but it doesn't work – Diksha Nasa Aug 16 '21 at 09:57
  • Do you have ```matplotlib``` installed, and is your ```pandas``` version larger than 0.25? – phuycke Aug 16 '21 at 10:01
  • @phuycke yes, my version of pandas is 1.2.2 – Diksha Nasa Aug 16 '21 at 10:08
  • do you still have the error when you include ```import matplotlib``` at the start of your script? – phuycke Aug 16 '21 at 10:11
  • @phuycke yes, I have included it . – Diksha Nasa Aug 16 '21 at 10:13
  • Which version of python are you using, and do you have a virtual environment? Also, is numpy explicitly installed? It's a dependency of matplotlib, but did you install it separately? – waykiki Aug 16 '21 at 10:51
  • @waykiki my python version is 3.8.5, no I don't have a virtual environment , yes numpy is installed seperately. – Diksha Nasa Aug 16 '21 at 12:19

1 Answers1

1

I misspelled bar with "barh" (feq.plot.barh(figsize=(10, 8), color='b', width=1)) which led to this error .

Diksha Nasa
  • 135
  • 3
  • 13
  • 1
    `.plot.bar` and `.plot.barh` are both valid. Is this a comment, or supposed to be an [edit](https://stackoverflow.com/posts/68800750/edit) to the question, because it doesn't seem like an answer? – Trenton McKinney Aug 16 '21 at 16:24