在 matplotlib 中更改条形图第二部分的值
Change values of second part of bar chart in matplotlib
df
channel ratio_customer_to_lead_owner ratio_customer_to_agent
0 Facebook 2 5
1 Bing 1 1
2 Google 6 13
下面的代码 returns 两个条形图具有相同的结果。
如何更改条形图的第二部分,使其值来自另一个名为 'ratio_customer_to_agent'?
的列
# create plot
fig, ax = plt.subplots()
n_groups = 3
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
X = np.arange(2)
plt.bar(X + 0.00, channel_ratios['ratio_customer_to_lead_owner'][0], color = 'b', width = 0.25,
label='Facebook')
plt.bar(X + 0.25, channel_ratios['ratio_customer_to_lead_owner'][1], color = 'g', width = 0.25,
label='Bing')
plt.bar(X + 0.50, channel_ratios['ratio_customer_to_lead_owner'][2], color = 'r', width = 0.25,
label='Google')
plt.ylabel('Ratio')
plt.title('Ratio of Customer to Lead Owner & Customer to Sales Agent')
plt.xticks(index + bar_width, ('Customer/Lead Owner', 'Customer/Sales Agent'))
plt.legend()
plt.tight_layout()
plt.show()
结果:
预期结果:
第二个蓝色条显示值 5,第二个绿色条显示 1,第二个红色条显示 13。
df = pd.DataFrame({'channel': {0: 'Facebook', 1: 'Bing', 2: 'Google'},
'ratio_customer_to_lead_owner': {0: 2, 1: 1, 2: 6},
'ratio_customer_to_agent': {0: 5, 1: 1, 2: 13}})
df.set_index('channel').T.plot(kind='bar')
输出:
df
channel ratio_customer_to_lead_owner ratio_customer_to_agent
0 Facebook 2 5
1 Bing 1 1
2 Google 6 13
下面的代码 returns 两个条形图具有相同的结果。 如何更改条形图的第二部分,使其值来自另一个名为 'ratio_customer_to_agent'?
的列# create plot
fig, ax = plt.subplots()
n_groups = 3
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
X = np.arange(2)
plt.bar(X + 0.00, channel_ratios['ratio_customer_to_lead_owner'][0], color = 'b', width = 0.25,
label='Facebook')
plt.bar(X + 0.25, channel_ratios['ratio_customer_to_lead_owner'][1], color = 'g', width = 0.25,
label='Bing')
plt.bar(X + 0.50, channel_ratios['ratio_customer_to_lead_owner'][2], color = 'r', width = 0.25,
label='Google')
plt.ylabel('Ratio')
plt.title('Ratio of Customer to Lead Owner & Customer to Sales Agent')
plt.xticks(index + bar_width, ('Customer/Lead Owner', 'Customer/Sales Agent'))
plt.legend()
plt.tight_layout()
plt.show()
结果:
预期结果: 第二个蓝色条显示值 5,第二个绿色条显示 1,第二个红色条显示 13。
df = pd.DataFrame({'channel': {0: 'Facebook', 1: 'Bing', 2: 'Google'},
'ratio_customer_to_lead_owner': {0: 2, 1: 1, 2: 6},
'ratio_customer_to_agent': {0: 5, 1: 1, 2: 13}})
df.set_index('channel').T.plot(kind='bar')
输出: