python - sns.catplot,缩小条形之间的差距
问题描述
这是我运行此代码时得到的情节有什么办法可以减少这两个条之间的间隙但不能完全相互接触?
sns.catplot(x = "case", kind = "count", data = df, alpha=0.8, palette = my_pal, hue="class")
plt.ylabel("Count", size=12)
plt.tight_layout()
解决方案
问题似乎是“case”和“class”列包含相同的信息,只是名称不同。在 case 为 1 的任何地方,class 都是 Negative,反之亦然。
如果您同时使用x
and hue
,seaborn 将放置 4 列:
- 案例 1,“负面”类
- 案例 1,“绿色”类
- 案例 2,“负面”类
- 案例 2,“绿色”类
四列中的两列保持空白:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame({'case': np.concatenate([np.repeat([1], 3700), np.repeat([2], 1200)]),
'class': np.concatenate([np.repeat(['Negative'], 3700), np.repeat(['green'], 1200)])})
g = sns.catplot(x="case",
hue='class',
palette='Blues',
data=df,
kind="count")
plt.show()
在这种情况下,更合适的情节是省略hue
并直接使用该类x
:
g = sns.catplot(x='class',
palette='Blues',
data=df,
kind='count')
plt.show()
PS:要获得与第一个情节相似的图例,可以使用 xticks 和 xlabel。请注意,seaborncatplot
旨在创建完整的子图网格。g.axes[0][0]
抓取ax
第一个子图的。
plt.legend(g.axes[0][0].patches,
[l.get_text() for l in g.axes[0][0].get_xticklabels()],
title= g.axes[0][0].get_xlabel())
g.axes[0][0].set_xticks([]) # remove the xticks (now in legend)
g.axes[0][0].set_xlabel('') # remove the xlabel (now title of legend)
推荐阅读
- c - 为什么两个 dirrerent 函数中的两个不同变量在 C 中共享相同的内存位置?
- android - 尝试优化重组的问题
- python - 如何存储在kivy中列出?
- javascript - JS 吸气剂性能
- java - 将 Java 对象返回给适当的 Kafka 生产者
- java - 带有Java的String的排序数组的问题
- angular - ngx-extended-pdf-viewer 从 Angular 8 中的 url 中删除哈希
- rust - 我如何 fork/exec 一个在 Rust 中转发所有参数的进程?
- reactjs - 基于变体将嵌入式样式应用于 MUI 组件
- python - Gurobi - 解决容量定位问题