熊猫散点图日志配色栏
我正在绘制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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将日志规范应用于data1和data2,然后创建散点图。它将产生预期的效果。
You could apply log norm to data1 and data2 and then create the scatter plot. It would produce the intended effect.
它看起来像
类SctactPlot
中的一个错误=“ nofollow noreferrer”> _ matplotlib/core.py.pysets
norm
local variable,然后在后来使用它,但是,最后一个参数
self.kwds
已经包含norm
命名参数,您已发送,因此您最终会得到两个norm
,而Python无法处理。似乎有试图在,但是提交没有成功。
It looks like a bug in
class ScatterPlot
in _matplotlib/core.pySets
norm
local variable and later uses it inHowever, the last parameter
self.kwds
already containsnorm
named parameter, which you sent, so you end up with twonorm
, 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.
如果熊猫不允许您输入标准:
Use matplotlib’ s scatter rather than pandas if the pandas one won’t let you input a norm: