如何绘制分组条形图

时间:2021-05-24 10:26:19

标签: python pandas matplotlib bar-chart

我不知道如何解决图表中的拥挤问题,而且我的 'breakfast participation' 列未显示在图表中。如何使用 matplotlib 解决此问题?

对于拥挤,我尝试过

fig= plt.figure(figsize=(15,5))

axes= fig.add_axes([0.1,0.1,0.8,0.8])

n_groups = 15
total_free_eligible = (236305,234775,1014,885,822,755,226415,177846,59117,56718,55654,55714,54903,54349,36263)
average_free_breakfast_participation = (2242735,608168,22897,20845,17614,11811,251016,440541,341749,256595,216001,238032,219906,282472,217790)
average_free_lunch_participation = (3402485,914333,22764,20799,17510,11921,260736,468113,398018,296766,250137,282551,259162,347370,307164)

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.5
opacity = 0.8

rects1 = plt.bar(index, total_free_eligible, bar_width,
alpha=opacity,
color='c',
label='Total Eligible')

rects2 = plt.bar(index + bar_width, average_free_breakfast_participation, bar_width,
alpha=opacity,
color='m',
label='Breakfast Participation')

rects3 = plt.bar(index + bar_width, average_free_lunch_participation, bar_width,
alpha=opacity,
color='b',
label='Lunch Participation')

plt.xlabel('Month')
plt.ylabel('Total')
plt.title('Comparison of Free Eligibility to Free Meals Served Bexar County')
plt.xticks(index + bar_width, ('2/1/20','3/12/20','4/1/20','5/1/20','6/1/20','7/1/20','8/1/20','9/1/20','10/1/20','11/1/20', '12/1/20', '1/1/21','2/1/21','3/1/21','4/1/21'))
plt.legend()

plt.tight_layout()
plt.show()

结果图

enter image description here

1 个答案:

答案 0 :(得分:0)

数据和导入

  • 使用 pandas v1.2.4matplotlib v3.4.2 进行测试。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# sample data
total_free_eligible = (236305,234775,1014,885,822,755,226415,177846,59117,56718,55654,55714,54903,54349,36263)
average_free_breakfast_participation = (2242735,608168,22897,20845,17614,11811,251016,440541,341749,256595,216001, 238032,219906,282472,217790)
average_free_lunch_participation = (3402485,914333,22764,20799,17510,11921,260736,468113,398018,296766,250137, 282551,259162,347370,307164)
labels = ('2/1/20','3/12/20','4/1/20','5/1/20','6/1/20','7/1/20','8/1/20','9/1/20','10/1/20','11/1/20', '12/1/20', '1/1/21','2/1/21','3/1/21','4/1/21')

带有 pandas 的代码

  • 这在 Pandas 中会更容易,这会将代码从 14 行减少到 5 行。
  • 对于图中未显示的某些值,y 范围很大,因此小的值很小。这可以通过将 yscale 设置为 'log' 来解决。
  • 使用 pandas.DataFrame.plot 绘制 DataFrame,它使用 matplotlib 作为后端。
# create a dict of all the data
data = {'Month': labels, 'Total Eligible': total_free_eligible, 'Breakfast Participation': average_free_breakfast_participation, 'Lunch Participation': average_free_lunch_participation}

# create the DataFrame
df = pd.DataFrame(data)

# convert the dates to a correct datetime format and extract only the date component - optional if wanting a datetime format
# df.Month = pd.to_datetime(df.Month).dt.date

# set the dates as the index
df = df.set_index('Month')

# display(df.head())
         Total Eligible  Breakfast Participation  Lunch Participation
Month                                                                
2/1/20           236305                  2242735              3402485
3/12/20          234775                   608168               914333
4/1/20             1014                    22897                22764

# plot the data
ax = df.plot(kind='bar', figsize=(13, 6), ylabel='Total', rot=0,
             title='Comparison of Free Eligibility to Free Meals Served Bexar County')

# set the yscale
ax.set_yscale('log')

enter image description here

没有pandas的代码

x = np.arange(len(labels))
width = 0.25

# create the figure
fig, ax = plt.subplots(figsize=(13, 6))

# plot each bar
rects1 = ax.bar(x - width, total_free_eligible, width, label='total_free_eligible')
rects2 = ax.bar(x + width, average_free_breakfast_participation, width, label='average_free_breakfast_participation')
rects3 = ax.bar(x, average_free_lunch_participation, width, label='average_free_lunch_participation')

# scale the y-axis so small values will be visible
ax.set_yscale('log')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Total')
ax.set_xlabel('Month')
ax.set_title('Comparison of Free Eligibility to Free Meals Served Bexar County')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

fig.tight_layout()

enter image description here