python - 在 seaborn 图下方添加一个表格
问题描述
我已经看到了很多关于这个的东西,但我似乎无法让它发挥作用,所以我想我会问。
我想生成这样的图,图下方的表格显示每个类别的计数。谁能帮我吗?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = {'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
'var1': [1, 2, 2, 4, 5, 3, 4, 5, 4, 7],
'var2': [2, 4, 8, 9, 4, 2, 3, 8, 3, 7]}
df = pd.DataFrame(data)
print(df.head(15))
sns.jointplot(data=df, x='var1', y='var2', hue='Category')
plt.suptitle('Example Data')
plt.show()
理想情况下,我希望它看起来像这样:
Plot Stuff
_______________________
| Category | a | b | c |
------------------------
| Count | 5 | 3 | 2 |
------------------------
编辑:
我已经更新了脚本,如下所示。我不能让表格居中,我只能让它出现在情节的右侧,我需要它在情节下方,但上面也可以。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = {'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
'var1': [1, 2, 2, 4, 5, 3, 4, 5, 4, 7],
'var2': [2, 4, 8, 9, 4, 2, 3, 8, 3, 7]}
df = pd.DataFrame(data)
print(df.head(15))
sns.jointplot(data=df, x='var1', y='var2', hue='Category')
plt.suptitle('Example Data')
plt.table(cellText=df.values,
rowLabels=df.index,
colLabels=df.columns,
cellLoc = 'center', rowLoc = 'center',
loc='bottom')
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.show()
解决方案
您可以使用bbox
手动将表格居中,这是您的代码bbox
并调整了字体大小:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = {'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
'var1': [1, 2, 2, 4, 5, 3, 4, 5, 4, 7],
'var2': [2, 4, 8, 9, 4, 2, 3, 8, 3, 7]}
df = pd.DataFrame(data)
print(df.head(15))
sns.jointplot(data=df, x='var1', y='var2', hue='Category')
plt.suptitle('Example Data')
table = plt.table(cellText=df.values,
rowLabels=df.index,
colLabels=df.columns,
bbox=(-6, -0.65, 6, 0.5))
table.auto_set_font_size(False)
table.set_fontsize(12)
plt.show()
推荐阅读
- http - 支持 HSTS 的 Http 客户端实现?
- mongodb - 合并来自多个 MongoDB 文档的数组
- python - 无法打开 Spyder
- javascript - 如何使用原生 HTML5 约束 API 验证单选组是否有选择
- android - 尝试从 Room 数据库获取对象后,订阅 LiveData 导致 null
- angular-material2 - 在 Angular Material 2 版本 6 的下拉列表中动态选择一些选项
- r - 将因子日期 1/1/2017 转换为 2017-01
- python - 将多个列表合并为一个嵌套列表
- swift - Swift 中的类类型工厂
- html - 升级到 5 pro 版后,FontAwesome 图标变大了