gpt4 book ai didi

Matplotlib "LinearSegmentedColormap"错误

转载 作者:行者123 更新时间:2023-12-05 01:49:21 26 4
gpt4 key购买 nike

有人可以帮我解决 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com