gpt4 book ai didi

python - 没有关联数据时如何从 catplot 中删除 xticks

转载 作者:行者123 更新时间:2023-12-05 01:32:11 24 4
gpt4 key购买 nike

我有两个问题:

  1. 我想从条形图中删除空条(出现在第一列中)。
  2. 我必须在 PowerPoint 演示文稿中使用此图表。如何增加条形图的高度以固定幻灯片的高度?我试图增加高度,但它没有进一步增加。可能吗?如果不是,我可以尝试哪些其他选项?

enter image description here

plt.figure(figsize=(40,20))
g = sns.catplot(x = 'Subject', y = 'EE Score',data = df , hue = 'Session',col='Grade',sharey = True,sharex = True,
hue_order=["2017-18", "2018-19", "2019-20"], kind="bar");
#plt.legend(bbox_to_anchor=(1, 1), loc=2)
g.set(ylim=(0, 100))
g.set_axis_labels("Subject", "EE Score")

ax = g.facet_axis(0,0)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)
ax = g.facet_axis(0,1)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)
ax = g.facet_axis(0,2)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)
ax = g.facet_axis(0,3)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)

#g.set_ylabel('')
plt.savefig('2.png', bbox_inches = 'tight')

最佳答案

  • 与@JohanC 一样,我最初认为无法从 catplot() 中删除空类别。但是,Michael 的评论提供了解决方案:sharex=False
  • 如果用于 x= 的列是 category dtype,则此解决方案将不起作用,可以使用 pandas.DataFrame.info()
  • python 3.10pandas 1.4.2matplotlib 3.5.1seaborn 0.11.2< 中测试
    • seabornmatplotlib
    • 的高级 API

object dtype x 轴

import seaborn as sns

titanic = sns.load_dataset('titanic')

# remove one category
titanic.drop(titanic.loc[(titanic['class']=='First')&(titanic['who']=='child')].index, inplace=True)

g = sns.catplot(x="who", y="survived", col="class", data=titanic, kind="bar", ci=None, sharex=False, hue='embarked', estimator=sum)

enter image description here

分类 dtype x 轴

  • 看到 tips.daycategorical 并且 sharex=False 将不起作用
  • 可以使用 tips.day = tips.day.astype('str') 将列转换为 object dtype,在这种情况下,sharex= False 会起作用,星期几不会排序。
tips = sns.load_dataset('tips')

print(tips.info())

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 total_bill 244 non-null float64
1 tip 244 non-null float64
2 sex 244 non-null category
3 smoker 244 non-null category
4 day 244 non-null category
5 time 244 non-null category
6 size 244 non-null int64
dtypes: category(4), float64(2), int64(1)
memory usage: 7.4 KB

g = sns.catplot(x="day", y="total_bill", col="time", kind="bar", data=tips, ci=None, sharex=False, hue='smoker')

enter image description here

  • 将列转换为object dtype
  • 请注意,日期不再按顺序排列。
tips.day = tips.day.astype('str')

print(tips.info())

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 total_bill 244 non-null float64
1 tip 244 non-null float64
2 sex 244 non-null category
3 smoker 244 non-null category
4 day 244 non-null object
5 time 244 non-null category
6 size 244 non-null int64
dtypes: category(3), float64(2), int64(1), object(1)
memory usage: 8.8+ KB

g = sns.catplot(x="day", y="total_bill", col="time", kind="bar", data=tips, ci=None, sharex=False, hue='smoker')

enter image description here

关于python - 没有关联数据时如何从 catplot 中删除 xticks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65732126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com