legend=sns.stripplot 中的 False 出现错误

问题描述 投票:0回答:1

我正在使用seaborn的stripplot和pointplot来绘制我的数据(参考

我的代码是:

sns.set_palette("Purples")

sns.stripplot(
    data=df_d_pt, x="feat", y="acc", hue="alpha",
    dodge=True, alpha=.2, legend=False,
)

sns.pointplot(
    data=df_d_pt, x="feat", y="acc", hue="alpha",
    dodge=.4, linestyle="none", errorbar=None,
    marker="_", markersize=10, markeredgewidth=3,
)

但是,当我收到错误时, legend=False 似乎不起作用:

---------------------------------------------------------------------------
AttributeError
Traceback (most recent call last) <ipython-input-156-18efd4be2baa> in <module>
      3 sns.stripplot(
      4     data=df_d_pt, x="feat", y="acc", hue="alpha",
----> 5     dodge=True, alpha=.2, legend=False,
      6 )
      7 

~/.local/lib/python3.6/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

~/.local/lib/python3.6/site-packages/seaborn/categorical.py in stripplot(x, y, hue, data, order, hue_order, jitter, dodge, orient, color, palette, size, edgecolor, linewidth, ax, **kwargs)    2820      linewidth=linewidth))    2821 
-> 2822     plotter.plot(ax, kwargs)    2823     return ax    2824 

~/.local/lib/python3.6/site-packages/seaborn/categorical.py in plot(self, ax, kws)    1158     def plot(self, ax, kws):    1159       """Make the plot."""
-> 1160         self.draw_stripplot(ax, kws)    1161         self.add_legend_data(ax)    1162         self.annotate_axes(ax)

~/.local/lib/python3.6/site-packages/seaborn/categorical.py in draw_stripplot(self, ax, kws)    1152                     kws.update(c=palette[point_colors])    1153                     if self.orient == "v":
-> 1154                         ax.scatter(cat_pos, strip_data, **kws)    1155                     else:    1156                         ax.scatter(strip_data, cat_pos, **kws)

/opt/conda/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)    1445     def inner(ax, *args, data=None, **kwargs):    1446         if data is None:
-> 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)    1448     1449         bound = new_sig.bind(ax, *args, **kwargs)

/opt/conda/lib/python3.6/site-packages/matplotlib/cbook/deprecation.py in wrapper(*inner_args, **inner_kwargs)
    409                          else deprecation_addendum,
    410                 **kwargs)
--> 411         return func(*inner_args, **inner_kwargs)
    412 
    413     return wrapper

/opt/conda/lib/python3.6/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)    4496        )    4497         collection.set_transform(mtransforms.IdentityTransform())
-> 4498         collection.update(kwargs)    4499     4500         if colors is None:

/opt/conda/lib/python3.6/site-packages/matplotlib/artist.py in update(self, props)
    994                     func = getattr(self, f"set_{k}", None)
    995                     if not callable(func):
--> 996                         raise AttributeError(f"{type(self).__name__!r} object "
    997                                              f"has no property {k!r}")
    998                     ret.append(func(v))

AttributeError: 'PathCollection' object has no property 'legend'

如果我从代码中删除“

legend=False
”,结果将如下所示:

如何解决这个问题?要么删除图例,要么把它放在我图的右侧

python python-3.x matplotlib seaborn figure
1个回答
0
投票

您可以使用 stripplot.legend().set_visible(False) 删除图例。

© www.soinside.com 2019 - 2024. All rights reserved.