数据集:
绘制代码:
import numpy as np
import matplotlib.pyplot as plt
# data to plot
n_groups = 6
GreenTaxi = pivot_df['GreenTaxi'].tolist()
YellowTaxi = pivot_df['YellowTaxi'].tolist()
Uber = pivot_df['Uber'].tolist()
# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
rects1 = plt.bar(index, GreenTaxi, bar_width,
alpha=opacity,
color='b',
label='GreenTaxi')
rects2 = plt.bar(index + bar_width, YellowTaxi, bar_width,
alpha=opacity,
color='g',
label='YellowTaxi')
rects3 = plt.bar(index + bar_width, Uber, bar_width,
alpha=opacity,
color='r',
label='Uber')
plt.xlabel('Month')
plt.ylabel('Ride Counts')
plt.title('Rides By Month')
plt.figure(figsize=(5,5))
plt.xticks(index + bar_width,pivot_df['MonthName'].tolist() )
plt.legend()
plt.show()
输出:
绿色和红色条形图重叠,而且更多的MonthName没有出现在x标签中,只有数字才会出现。你能不能就这个问题提出建议。
答案 0 :(得分:0)
我认为您可以使用DataFrame.plot.bar
,也可以使用列Names
的{{3}},然后按子集[]
更改列的顺序:
bar_width = 0.35
opacity = 0.8
colors = ['b','g','r']
cols = ['GreenTaxi','YellowTaxi','Uber']
title = 'Rides By Month'
ylabel = "Ride Counts"
ax = pivot_df.set_index('MonthName')[cols].plot.bar(width=bar_width,
alpha=opacity,
figsize=(5,5),
color=colors,
title=title)
ax.set_ylabel(ylabel)
ax.legend(loc='best',prop={'size':10})
答案 1 :(得分:0)