Seaborn 在水平条形图中绘制注释
Seaborn plotting annotations in bar charts horizontal
我有一个这样的数据框:
switch_summary duration count
McDonalds -> Arbys -> McDonalds 0.067 1
Wendys -> Popeyes -> McDonalds -> KFC 0.293 1
Arbys -> Wendys -> Popeyes -> McDonalds 0.542 2
Arbys -> McDonalds -> KFC 1.075 1
KFC -> Arbys -> Wendys -> Popeyes 2.123 3
KFC -> Wendys -> Popeyes -> Arbys 2.297 1
我想创建一个 seaborn 图来可视化持续时间和计数。
我有以下代码:
plt.figure(figsize = [15,7])
ax = (sns.barplot(x = 'duration',
y = 'switch_summary',
palette = sns.color_palette('winter', 10),
data = df))
for p in ax.patches:
width = p.get_width()
plt.text( 0.1 + p.get_width(), p.get_y() + 0.55 * p.get_height(),
'~{:1.2f}\n days'.format(width),
ha = 'center', va = 'center', color = 'black', weight = 'bold')
ax = ax.set(title = 'Top 10 Fastest Trends',
xlabel = 'Total Duration in Trend',
ylabel = 'Restaurant Trend')
此代码将显示持续时间,但我还想显示计数。
如何在代码的 plt.text()
部分显示计数?
一个快速的解决方法是枚举补丁,使用索引获取计数。
import pandas as pd
data = {'switch_summary': ['McDonalds -> Arbys -> McDonalds ', 'Wendys -> Popeyes -> McDonalds -> KFC', 'Arbys -> Wendys -> Popeyes -> McDonalds ', 'Arbys -> McDonalds -> KFC', 'KFC -> Arbys -> Wendys -> Popeyes', 'KFC -> Wendys -> Popeyes -> Arbys '],
'duration': [0.067, 0.293, 0.542, 1.075, 2.123, 2.297],
'count': [1, 1, 2,1,3,1]
}
df = pd.DataFrame(data)
plt.figure(figsize = [15,7])
ax = (sns.barplot(x = 'duration',
y = 'switch_summary',
palette = sns.color_palette('winter', 10),
data = df))
for i,p in enumerate(ax.patches):
width = p.get_width()
plt.text( 0.1 + p.get_width(), p.get_y() + 0.55 * p.get_height(),
'~{:1.2f}\n days \n count - {}'.format(width,df['count'][i]),
ha = 'center', va = 'center', color = 'black', weight = 'bold')
ax = ax.set(title = 'Top 10 Fastest Trends',
xlabel = 'Total Duration in Trend',
ylabel = 'Restaurant Trend')
我有一个这样的数据框:
switch_summary duration count
McDonalds -> Arbys -> McDonalds 0.067 1
Wendys -> Popeyes -> McDonalds -> KFC 0.293 1
Arbys -> Wendys -> Popeyes -> McDonalds 0.542 2
Arbys -> McDonalds -> KFC 1.075 1
KFC -> Arbys -> Wendys -> Popeyes 2.123 3
KFC -> Wendys -> Popeyes -> Arbys 2.297 1
我想创建一个 seaborn 图来可视化持续时间和计数。
我有以下代码:
plt.figure(figsize = [15,7])
ax = (sns.barplot(x = 'duration',
y = 'switch_summary',
palette = sns.color_palette('winter', 10),
data = df))
for p in ax.patches:
width = p.get_width()
plt.text( 0.1 + p.get_width(), p.get_y() + 0.55 * p.get_height(),
'~{:1.2f}\n days'.format(width),
ha = 'center', va = 'center', color = 'black', weight = 'bold')
ax = ax.set(title = 'Top 10 Fastest Trends',
xlabel = 'Total Duration in Trend',
ylabel = 'Restaurant Trend')
此代码将显示持续时间,但我还想显示计数。
如何在代码的 plt.text()
部分显示计数?
一个快速的解决方法是枚举补丁,使用索引获取计数。
import pandas as pd
data = {'switch_summary': ['McDonalds -> Arbys -> McDonalds ', 'Wendys -> Popeyes -> McDonalds -> KFC', 'Arbys -> Wendys -> Popeyes -> McDonalds ', 'Arbys -> McDonalds -> KFC', 'KFC -> Arbys -> Wendys -> Popeyes', 'KFC -> Wendys -> Popeyes -> Arbys '],
'duration': [0.067, 0.293, 0.542, 1.075, 2.123, 2.297],
'count': [1, 1, 2,1,3,1]
}
df = pd.DataFrame(data)
plt.figure(figsize = [15,7])
ax = (sns.barplot(x = 'duration',
y = 'switch_summary',
palette = sns.color_palette('winter', 10),
data = df))
for i,p in enumerate(ax.patches):
width = p.get_width()
plt.text( 0.1 + p.get_width(), p.get_y() + 0.55 * p.get_height(),
'~{:1.2f}\n days \n count - {}'.format(width,df['count'][i]),
ha = 'center', va = 'center', color = 'black', weight = 'bold')
ax = ax.set(title = 'Top 10 Fastest Trends',
xlabel = 'Total Duration in Trend',
ylabel = 'Restaurant Trend')