Matplotlib "LinearSegmentedColormap"错误

标签 matplotlib data-science jupyter

有人可以帮我解决 matplotlib 的这个错误吗? 我正在将 jupyter 用于一本著名书籍中的一些数据科学项目(动手机器学习......),但我遇到了一个异常错误的问题。

这是代码:

%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")

这是错误:

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'

我只想使用 matplotlib 来绘制一个简单而正常的图形,但我找不到问题。

最佳答案

不是传入 cmap=plt.get_cmap("jet") 而是传入 "jet"

所以调用应该改为

%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")

关于Matplotlib "LinearSegmentedColormap"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74208216/

相关文章:

python - 在图表中扩展 matplotlib 线图

python - matplotlib 动画中数组的不同颜色点

matplotlib - Matplotlib 中相交透明层的正确图例颜色

python - fit_transform() 需要 2 个位置参数,但 3 个是通过 LabelBinarizer 给出的

python - 根据两列中的匹配值为日期差异创建条件列

python - 如何使用 matplotlib 在 python 中绘制时间戳?

python - Bokeh 图未在 nbviewer 中显示

python - 使用Python绘制Matplotlib回归散点图?

r - Jupyter 中 R 中的 Magic %% 命令

python - 创建一个函数来进行傅里叶逆变换