熊猫散点图日志配色栏

发布于 2025-01-31 17:59:30 字数 3739 浏览 2 评论 0 原文

我正在绘制Pandas中使用另一列颜色的数据框架,我想使用日志归一化的colormap。

此帖子显示,在普通的matplotlib中,您可以使用log nog nogosalized通过简单地添加 norm = matplotlib.colors.lognorm()将其colormap到 .scatter()方法。当我为熊猫这样做时,尽管它会导致typeerror: typeError:matplotlib.axes._axes.axes.axes.scatter()获得了关键字参数'norm'norm'的多个值。

最小可再现示例

example_data = pd.DataFrame({'sample_no': [1, 2, 3, 4, 5, 6], 
                'data1': [10, 15, 12, 10, 13, 14], 
                'data2': [0.1, 1, 5, 10, 12, 5]})

这样的

example_data.plot.scatter('sample_no', 'data1', c='data2', cmap='viridis',)

如下:得出一个不错的图。

添加 norm 参数会产生上述错误。

example_data.plot.scatter('sample_no', 'data1', c='data2', cmap='viridis',
                          norm=matplotlib.colors.LogNorm())

完整的松弛痕迹

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/rwegener/repos/sarp_lessons/snippets/More mapping/flight_track.ipynb Cell 45' in <cell line: 1>()
----> 1 example_data.plot.scatter('sample_no', 'data1', c='data2', cmap='viridis',
      2                           norm=matplotlib.colors.LogNorm())

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_core.py:1669, in PlotAccessor.scatter(self, x, y, s, c, **kwargs)
   1586 def scatter(self, x, y, s=None, c=None, **kwargs):
   1587     """
   1588     Create a scatter plot with varying marker point size and color.
   1589 
   (...)
   1667         ...                       colormap='viridis')
   1668     """
-> 1669     return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_core.py:917, in PlotAccessor.__call__(self, *args, **kwargs)
    915 if kind in self._dataframe_kinds:
    916     if isinstance(data, ABCDataFrame):
--> 917         return plot_backend.plot(data, x=x, y=y, kind=kind, **kwargs)
    918     else:
    919         raise ValueError(f"plot kind {kind} can only be used for data frames")

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/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 ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py:329, in MPLPlot.generate(self)
    327 self._compute_plot_data()
    328 self._setup_subplots()
--> 329 self._make_plot()
    330 self._add_table()
    331 self._make_legend()

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py:1114, in ScatterPlot._make_plot(self)
   1112 else:
   1113     label = None
-> 1114 scatter = ax.scatter(
   1115     data[x].values,
   1116     data[y].values,
   1117     c=c_values,
   1118     label=label,
   1119     cmap=cmap,
   1120     norm=norm,
   1121     **self.kwds,
   1122 )
   1123 if cb:
   1124     cbar_label = c if c_is_column else ""

TypeError: matplotlib.axes._axes.Axes.scatter() got multiple values for keyword argument 'norm'

I am plotting a dataframe in pandas that uses another column for color and I would like to use a log normalized colormap.

This post shows that in plain matplotlib you can use a log-normalized colormap by simply adding norm=matplotlib.colors.LogNorm() to the .scatter() method. When I do that for pandas, though it causes a TypeError: TypeError: matplotlib.axes._axes.Axes.scatter() got multiple values for keyword argument 'norm'.

Minimum Reproducible Example

example_data = pd.DataFrame({'sample_no': [1, 2, 3, 4, 5, 6], 
                'data1': [10, 15, 12, 10, 13, 14], 
                'data2': [0.1, 1, 5, 10, 12, 5]})

Plotting like this:

example_data.plot.scatter('sample_no', 'data1', c='data2', cmap='viridis',)

Yields a nice graph.
enter image description here

Adding the norm argument yields the error above.

example_data.plot.scatter('sample_no', 'data1', c='data2', cmap='viridis',
                          norm=matplotlib.colors.LogNorm())

Full slack trace

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/rwegener/repos/sarp_lessons/snippets/More mapping/flight_track.ipynb Cell 45' in <cell line: 1>()
----> 1 example_data.plot.scatter('sample_no', 'data1', c='data2', cmap='viridis',
      2                           norm=matplotlib.colors.LogNorm())

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_core.py:1669, in PlotAccessor.scatter(self, x, y, s, c, **kwargs)
   1586 def scatter(self, x, y, s=None, c=None, **kwargs):
   1587     """
   1588     Create a scatter plot with varying marker point size and color.
   1589 
   (...)
   1667         ...                       colormap='viridis')
   1668     """
-> 1669     return self(kind="scatter", x=x, y=y, s=s, c=c, **kwargs)

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_core.py:917, in PlotAccessor.__call__(self, *args, **kwargs)
    915 if kind in self._dataframe_kinds:
    916     if isinstance(data, ABCDataFrame):
--> 917         return plot_backend.plot(data, x=x, y=y, kind=kind, **kwargs)
    918     else:
    919         raise ValueError(f"plot kind {kind} can only be used for data frames")

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/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 ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py:329, in MPLPlot.generate(self)
    327 self._compute_plot_data()
    328 self._setup_subplots()
--> 329 self._make_plot()
    330 self._add_table()
    331 self._make_legend()

File ~/.pyenv/versions/miniconda3-4.7.12/envs/sarp/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py:1114, in ScatterPlot._make_plot(self)
   1112 else:
   1113     label = None
-> 1114 scatter = ax.scatter(
   1115     data[x].values,
   1116     data[y].values,
   1117     c=c_values,
   1118     label=label,
   1119     cmap=cmap,
   1120     norm=norm,
   1121     **self.kwds,
   1122 )
   1123 if cb:
   1124     cbar_label = c if c_is_column else ""

TypeError: matplotlib.axes._axes.Axes.scatter() got multiple values for keyword argument 'norm'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

潦草背影 2025-02-07 17:59:30

您可以将日志规范应用于data1和data2,然后创建散点图。它将产生预期的效果。

You could apply log norm to data1 and data2 and then create the scatter plot. It would produce the intended effect.

愛放△進行李 2025-02-07 17:59:30

它看起来像类SctactPlot 中的一个错误=“ nofollow noreferrer”> _ matplotlib/core.py.py

if color_by_categorical:
    from matplotlib import colors

    n_cats = len(self.data[c].cat.categories)
    cmap = colors.ListedColormap([cmap(i) for i in range(cmap.N)])
    bounds = np.linspace(0, n_cats, n_cats + 1)
    norm = colors.BoundaryNorm(bounds, cmap.N)
else:
    norm = self.kwds.pop("norm", None)

sets norm local variable,然后在后来使用它,

scatter = ax.scatter(
    data[x].values,
    data[y].values,
    c=c_values,
    label=label,
    cmap=cmap,
    norm=norm,
    **self.kwds,
)

但是,最后一个参数 self.kwds 已经包含 norm 命名参数,您已发送,因此您最终会得到两个 norm ,而Python无法处理。

似乎有试图在,但是提交没有成功。

It looks like a bug in class ScatterPlot in _matplotlib/core.py

if color_by_categorical:
    from matplotlib import colors

    n_cats = len(self.data[c].cat.categories)
    cmap = colors.ListedColormap([cmap(i) for i in range(cmap.N)])
    bounds = np.linspace(0, n_cats, n_cats + 1)
    norm = colors.BoundaryNorm(bounds, cmap.N)
else:
    norm = self.kwds.pop("norm", None)

Sets norm local variable and later uses it in

scatter = ax.scatter(
    data[x].values,
    data[y].values,
    c=c_values,
    label=label,
    cmap=cmap,
    norm=norm,
    **self.kwds,
)

However, the last parameter self.kwds already contains norm named parameter, which you sent, so you end up with two norm, and Python can't handle this.

It seems like there was an attempt to fix it at commit 1cad74e1ee3b5144f6ef774aeed6ae3152d28d6a, but the commit wasn't successful.

ι不睡觉的鱼゛ 2025-02-07 17:59:30

如果熊猫不允许您输入标准:

fig, ax = plt.subplots()
ax.scatter(sample_data[‘sample_no’], sample_data[‘data1’], norm=matplotlib.colors.LogNorm())

Use matplotlib’ s scatter rather than pandas if the pandas one won’t let you input a norm:

fig, ax = plt.subplots()
ax.scatter(sample_data[‘sample_no’], sample_data[‘data1’], norm=matplotlib.colors.LogNorm())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文