更改使用seaborn.factorplot创建的条形图中的条形宽度 [英] Changing width of bars in bar chart created using seaborn.factorplot
本文介绍了更改使用seaborn.factorplot创建的条形图中的条形宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用seaborn.factorplot创建条形图.我的代码如下:
I'm trying to create bar chart using seaborn.factorplot. My code looks like this:
import seaborn
import matplotlib.pyplot as plt
df=pd.read_csv('data.csv')
fg = seaborn.factorplot(x='vesselID', y='dur_min', hue='route', size=6,aspect=2 ,kind='bar', data=df)
我的 data.csv 看起来像这样
,route,vesselID,dur_min
0,ANA-SJ,13,39.357894736842105
1,ANA-SJ,20,24.747663551401867
2,ANA-SJ,38,33.72142857142857
3,ANA-SJ,69,37.064516129032256
4,ED-KING,30,22.10062893081761
5,ED-KING,36,21.821428571428573
6,ED-KING,68,23.396551724137932
7,F-V-S,1,13.623239436619718
8,F-V-S,28,14.31294964028777
9,F-V-S,33,16.161616161616163
10,MUK-CL,18,13.953191489361702
11,MUK-CL,19,14.306513409961687
12,PD-TAL,65,12.477272727272727
13,PT-COU,52,27.48148148148148
14,PT-COU,66,28.24778761061947
15,SEA-BI,25,30.94267515923567
16,SEA-BI,32,31.0
17,SEA-BI,37,31.513513513513512
18,SEA-BR,2,55.8
19,SEA-BR,13,57.0
20,SEA-BR,15,54.05434782608695
21,SEA-BR,17,50.43859649122807
现在我的问题是如何更改条形的宽度,而我无法通过更改大小和长宽比来实现.
Now my question is how to change the width of the bar and I'm not able to achieve this by changing size and aspect.
推荐答案
实际上,您可以直接使用带有功能set_width
的patch属性来完成此操作.但是,如果仅执行此操作,则只修改补丁的宽度,而不修改轴上的位置,因此也必须更改x坐标.
In fact, you can do it using directly the patches attributes with the function set_width
. However if you only do that, you will just modify your patches width but not the position on the axe, so you have to change the x coordinates too.
import pylab as plt
import seaborn as sns
tips = sns.load_dataset("tips")
fig, ax = plt.subplots()
sns.barplot(data=tips, ax=ax, x="time", y="tip", hue="sex")
def change_width(ax, new_value) :
for patch in ax.patches :
current_width = patch.get_width()
diff = current_width - new_value
# we change the bar width
patch.set_width(new_value)
# we recenter the bar
patch.set_x(patch.get_x() + diff * .5)
change_width(ax, .35)
plt.show()
这是结果:
这篇关于更改使用seaborn.factorplot创建的条形图中的条形宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文