Can someone help me with this error of matplotlib? I'm using jupyter for some data science project from a famous book (hands-on machine learning...) but I have a problem with an unusual error.
This is the code:
%matplotlib inline
import matplotlib.pyplot as plt
housing.plot(kind="scatter", x="longitude", y="latitude", alpha=0.4,
s=housing["population"]/100, label="population", figsize=(10,7),
c="median_house_value", cmap=plt.get_cmap("jet"), colorbar=True,
sharex=False)
plt.legend()
save_fig("housing_prices_scatterplot")
And this is the error:
TypeError Traceback (most recent call last)
Cell In [85], line 3
1 get_ipython().run_line_magic('matplotlib', 'inline')
2 import matplotlib.pyplot as plt
----> 3 housing.plot(kind="scatter", x="longitude", y="latitude", alpha=0.4,
4 s=housing["population"]/100, label="population", figsize=(10,7),
5 c="median_house_value", cmap=plt.get_cmap("jet"), colorbar=True,
6 sharex=False)
7 plt.legend()
8 save_fig("housing_prices_scatterplot")
File ~/my_env/lib/python3.9/site-packages/pandas/plotting/_core.py:945, in PlotAccessor.__call__(self, *args, **kwargs)
943 if kind in self._dataframe_kinds:
944 if isinstance(data, ABCDataFrame):
--> 945 return plot_backend.plot(data, x=x, y=y, kind=kind, **kwargs)
946 else:
947 raise ValueError(f"plot kind {kind} can only be used for data frames")
File ~/my_env/lib/python3.9/site-packages/pandas/plotting/_matplotlib/__init__.py:71, in plot(data, kind, **kwargs)
69 kwargs["ax"] = getattr(ax, "left_ax", ax)
70 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 71 plot_obj.generate()
72 plot_obj.draw()
73 return plot_obj.result
File ~/my_env/lib/python3.9/site-packages/pandas/plotting/_matplotlib/core.py:452, in MPLPlot.generate(self)
450 self._compute_plot_data()
451 self._setup_subplots()
--> 452 self._make_plot()
453 self._add_table()
454 self._make_legend()
File ~/my_env/lib/python3.9/site-packages/pandas/plotting/_matplotlib/core.py:1225, in ScatterPlot._make_plot(self)
1223 if self.colormap is not None:
1224 if mpl_ge_3_6_0():
-> 1225 cmap = mpl.colormaps[self.colormap]
1226 else:
1227 cmap = self.plt.cm.get_cmap(self.colormap)
File ~/my_env/lib/python3.9/site-packages/matplotlib/cm.py:87, in ColormapRegistry.__getitem__(self, item)
85 def __getitem__(self, item):
86 try:
---> 87 return self._cmaps[item].copy()
88 except KeyError:
89 raise KeyError(f"{item!r} is not a known colormap name") from None
TypeError: unhashable type: 'LinearSegmentedColormap'
I just want to use matplotlib for a simple and normal graph but I can't find the problem.
CodePudding user response:
A plot was made without error after I deleted this: cmap=plt.get_camp("jet"). The color range was default, not from blue to red.
CodePudding user response:
Instead of passing in cmap=plt.get_cmap("jet")
just pass in "jet"
.
So the call should instead be
%matplotlib inline
import matplotlib.pyplot as plt
housing.plot(kind="scatter", x="longitude", y="latitude", alpha=0.4,
s=housing["population"]/100, label="population", figsize=(10,7),
c="median_house_value", cmap="jet", colorbar=True,
sharex=False)
plt.legend()
save_fig("housing_prices_scatterplot")