matplotlib 将颜色数组传递给条带图时的ValueError

ar5n3qh5  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(267)

我有一个 Dataframe ,我试图使用Seaborn绘制为带状图。数据结构为:
| 集团|价值|
| --------------|--------------|
| “一”|3.1|
| “二”|1.2|
| ......这是什么?|......这是什么?|
其中“Group”是字符串,“Value”是浮点数。
我想为任何小于阈值的值分配一种颜色,我已经尝试为图中的每个点分配一个颜色数组,但是当我运行这个时,我得到了错误:“ValueError:“c”参数有37个元素,与大小为10的“x”和“y”不一致
我不知道如何解决这个问题,因为我不知道10的大小来自哪里。data[“Group”]和data[“Value”]的长度是37。唯一值的长度,即len(set(data["Group"]))是4,所以比我的颜色列表小,但不是10。
我目前拥有的代码是:

import pandas as pd
import numpy as np
import seaborn
from matplotlib import pyplot as plt

THRESHOLD = 0.5

data = pd.DataFrame({
    "Group": ["1","1","1","2","3","2","2","3","3"],
    "Value": [0.3, 1.2, 4.2, 5.1, 0.1, 0.2, 3.2, 4.1, 0.2]
})

colors = np.where(data["Value"] < THRESHOLD, "r", "#444444")

seaborn.stripplot(
    data = data,
    x = "Group",
    y = "Value",
    color = colors
)

编辑以提供一个完整的工作示例。本例中产生的错误为:“ValueError:“c”参数有9个元素,这与大小为3的“x”和“y”不一致。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[7], line 11
      4 data = pd.DataFrame({
      5     "Group": ["1","1","1","2","3","2","2","3","3"],
      6     "Value": [0.3, 1.2, 4.2, 5.1, 0.1, 0.2, 3.2, 4.1, 0.2]
      7 })
      9 colors = np.where(data["Value"] < THRESHOLD, "r", "#444444")
---> 11 sns.stripplot(
     12     data = data,
     13     x = "Group",
     14     y = "Value",
     15     color = colors
     16 )

File ~\anaconda3\envs\py11\Lib\site-packages\seaborn\categorical.py:2537, in stripplot(data, x, y, hue, order, hue_order, jitter, dodge, orient, color, palette, size, edgecolor, linewidth, hue_norm, native_scale, formatter, legend, ax, **kwargs)
   2529 size = kwargs.get("s", size)
   2531 kwargs.update(dict(
   2532     s=size ** 2,
   2533     edgecolor=edgecolor,
   2534     linewidth=linewidth)
   2535 )
-> 2537 p.plot_strips(
   2538     jitter=jitter,
   2539     dodge=dodge,
   2540     color=color,
   2541     edgecolor=edgecolor,
   2542     plot_kws=kwargs,
   2543 )
   2545 # XXX this happens inside a plotting method in the distribution plots
   2546 # but maybe it's better out here? Alternatively, we have an open issue
   2547 # suggesting that _attach could add default axes labels, which seems smart.
   2548 p._add_axis_labels(ax)

File ~\anaconda3\envs\py11\Lib\site-packages\seaborn\categorical.py:299, in _CategoricalPlotterNew.plot_strips(self, jitter, dodge, color, edgecolor, plot_kws)
    296         sub_data[var] = np.power(10, sub_data[var])
    298 ax = self._get_axes(sub_vars)
--> 299 points = ax.scatter(sub_data["x"], sub_data["y"], color=color, **plot_kws)
    301 if "hue" in self.variables:
    302     points.set_facecolors(self._hue_map(sub_data["hue"]))

File ~\anaconda3\envs\py11\Lib\site-packages\matplotlib\__init__.py:1442, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1439 @functools.wraps(func)
   1440 def inner(ax, *args, data=None, **kwargs):
   1441     if data is None:
-> 1442         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1444     bound = new_sig.bind(ax, *args, **kwargs)
   1445     auto_label = (bound.arguments.get(label_namer)
   1446                   or bound.kwargs.get(label_namer))

File ~\anaconda3\envs\py11\Lib\site-packages\matplotlib\axes\_axes.py:4602, in Axes.scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, **kwargs)
   4599 if edgecolors is None:
   4600     orig_edgecolor = kwargs.get('edgecolor', None)
   4601 c, colors, edgecolors = \
-> 4602     self._parse_scatter_color_args(
   4603         c, edgecolors, kwargs, x.size,
   4604         get_next_color_func=self._get_patches_for_fill.get_next_color)
   4606 if plotnonfinite and colors is None:
   4607     c = np.ma.masked_invalid(c)

File ~\anaconda3\envs\py11\Lib\site-packages\matplotlib\axes\_axes.py:4455, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
   4451     else:
   4452         if len(colors) not in (0, 1, xsize):
   4453             # NB: remember that a single color is also acceptable.
   4454             # Besides *colors* will be an empty array if c == 'none'.
-> 4455             raise invalid_shape_exception(len(colors), xsize)
   4456 else:
   4457     colors = None  # use cmap, norm after collection is created

ValueError: 'c' argument has 9 elements, which is inconsistent with 'x' and 'y' with size 3.
kgqe7b3p

kgqe7b3p1#

您应该使用hue而不是color来标记这两个组,并使用palette来指定所需的颜色。

import seaborn as sns
import pandas as pd
import numpy as np
df = {'Value': np.random.randn(10),
      'Group': np.random.randint(0, 10, size=(10,))}
data = pd.DataFrame(df)
THRESHOLD = 0.1
colors = np.where(data["Value"] < THRESHOLD, "less than threshold", "Bigger than threshold")
sns.stripplot(
    data = data,
    x = "Group",
    y = "Value",
    hue = colors,
    palette=['red', 'blue']
)

相关问题