python - 如何使用汇总统计注释seaborn PairGrid 对角线

标签 python pandas matplotlib seaborn plot-annotations

我尝试通过以下代码将有关每个参数的统计数据放置在绘图的对角线上:

import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')

def summary(x, **kwargs):
    x = pd.Series(x)
    
    label = x.describe()[['mean', 'std', 'min', '50%', 'max']]
       
    label = label.round()
    ax = plt.gca()
    ax.set_axis_off()
    
    ax.annotate(pd.DataFrame(label),
               xy = (0.1, 0.2), size = 20, xycoords = ax.transAxes)

grd = sns.PairGrid(data=iris, size = 4)
grd = grd.map_upper(plt.scatter, color = 'k')

grd = grd.map_lower(sns.kdeplot, cmap = 'PRGn_r')
grd = grd.map_upper(sns.kdeplot, cmap = 'PRGn_r')
grd = grd.map_lower(plt.scatter, color = 'k')

grd = grd.map_diag(summary);

但由于某种原因,我遇到了 ValueError: DataFrame 的真值不明确。在尝试执行 summary 函数时使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().精确 - 将数据集 pd.DataFrame(label) 放入 ax.annotate 中。

回溯

-------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-88744ab705f0> in <module>
      6 grd = grd.map_lower(plt.scatter, color = 'k')
      7 
----> 8 grd = grd.map_diag(summary)
      9 plt.show()

e:\Anaconda3\lib\site-packages\seaborn\axisgrid.py in map_diag(self, func, **kwargs)
   1448 
   1449         if "hue" not in signature(func).parameters:
-> 1450             return self._map_diag_iter_hue(func, **kwargs)
   1451 
   1452         # Loop over diagonal variables and axes, making one plot in each

e:\Anaconda3\lib\site-packages\seaborn\axisgrid.py in _map_diag_iter_hue(self, func, **kwargs)
   1515                     func(x=data_k, label=label_k, color=color, **plot_kwargs)
   1516                 else:
-> 1517                     func(data_k, label=label_k, color=color, **plot_kwargs)
   1518 
   1519         self._add_axis_labels()

<ipython-input-25-d3b35ec81d97> in summary(x, **kwargs)
      8     ax.set_axis_off()
      9 
---> 10     ax.annotate(pd.DataFrame(label),
     11                xy = (0.1, 0.2), size = 20, xycoords = ax.transAxes)

e:\Anaconda3\lib\site-packages\matplotlib\_api\deprecation.py in wrapper(*args, **kwargs)
    333                 f"for the old name will be dropped %(removal)s.")
    334             kwargs[new] = kwargs.pop(old)
--> 335         return func(*args, **kwargs)
    336 
    337     # wrapper() must keep the same documented signature as func(): if we

e:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in annotate(self, text, xy, *args, **kwargs)
    650     @docstring.dedent_interpd
    651     def annotate(self, text, xy, *args, **kwargs):
--> 652         a = mtext.Annotation(text, xy, *args, **kwargs)
    653         a.set_transform(mtransforms.IdentityTransform())
    654         if 'clip_on' in kwargs:

e:\Anaconda3\lib\site-packages\matplotlib\text.py in __init__(self, text, xy, xytext, xycoords, textcoords, arrowprops, annotation_clip, **kwargs)
   1804 
   1805         # Must come last, as some kwargs may be propagated to arrow_patch.
-> 1806         Text.__init__(self, x, y, text, **kwargs)
   1807 
   1808     def contains(self, event):

e:\Anaconda3\lib\site-packages\matplotlib\text.py in __init__(self, x, y, text, color, verticalalignment, horizontalalignment, multialignment, fontproperties, rotation, linespacing, rotation_mode, usetex, wrap, transform_rotates_text, **kwargs)
    152         self._x, self._y = x, y
    153         self._text = ''
--> 154         self.set_text(text)
    155         self.set_color(
    156             color if color is not None else mpl.rcParams["text.color"])

e:\Anaconda3\lib\site-packages\matplotlib\text.py in set_text(self, s)
   1213         if s is None:
   1214             s = ''
-> 1215         if s != self._text:
   1216             self._text = str(s)
   1217             self.stale = True

e:\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1532     @final
   1533     def __nonzero__(self):
-> 1534         raise ValueError(
   1535             f"The truth value of a {type(self).__name__} is ambiguous. "
   1536             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

最佳答案

  • .annotate需要一个 string 而不是 DataFrameSeries。使用 .to_string()系列标签转换为字符串
  • x 已经是 pandas.Series,因此不需要 x = pd.Series(x)
  • 可重现并使用 seaborn 0.11.2matplotlib 3.4.2pandas 1.3.1 进行测试
import seaborn as sns
import matplotlib.pyplot as plt

# change your function; ax.annotate expects a string
def summary(x, **kwargs):
    
    label = x.describe()[['mean', 'std', 'min', '50%', 'max']].round()
       
    ax = plt.gca()
    ax.set_axis_off()
    
    # use .to_string()   
    ax.annotate(label.to_string(), xy=(0.1, 0.2), size=20, xycoords=ax.transAxes)

enter image description here

关于python - 如何使用汇总统计注释seaborn PairGrid 对角线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68909446/

相关文章:

python - 如何在 Pandas 中添加堆积条形图孵化? (...或者如何在 Pandas 情节与 matplotlib 中获得 BarContainer 与 AxisSubplot?)

python - 如何更改 Pandas 数据框中的日期格式?

python - 从许多网页中获取数据的最佳方式(线程/事件驱动)

python - 从 Django 模板中的 response.POST.get() 访问 QueryDict 元素

python - 将 Pandas 数据框转换为字典,其中一列作为键,另一列作为多个值

python - 使用 Python 在 matplotlib 中不剪裁错误栏

python - 想要从用于将文件从一个服务器传输到另一个服务器的 scp 命令的 shell 脚本传递密码

python - 通过查找基于 if-else 条件创建新的 pandas 数据框列

python - 在没有真实索引的情况下 reshape Pandas 数据框

python - 在不更改 rcParams 全局字典的情况下设置 axes.linewidth