简体   繁体   English

如何 plot 分组条形图

[英]How to plot grouped bar plots

I can't figure out how to fix the crowding in my graph and my 'breakfast participation' column isn't showing in the graph.我不知道如何解决图表中的拥挤问题,并且图表中没有显示我'breakfast participation'列。 How do I fix this with matplotlib?如何使用 matplotlib 解决此问题?

For the crowding, I tried对于拥挤,我尝试了

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()

Resulting Plot结果 Plot

在此处输入图像描述

Data and Imports数据和导入

  • Tested with pandas v1.2.4 and matplotlib v3.4.2 .使用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')

Code with pandas编码为pandas

  • This will be easier in pandas, which will reduce the code from 14 lines, to 5 lines.这在 pandas 中会更容易,这会将代码从 14 行减少到 5 行。
  • In regards to some values not showing in the plot, the y-range is large, so small values are small.对于plot中没有显示的一些值,y范围大,所以小值小。 This can be resolved by setting the yscale to 'log' .这可以通过将yscale设置为'log'来解决。
  • Plot the DataFrame with pandas.DataFrame.plot , which uses matplotlib as the backend. Plot the DataFrame with pandas.DataFrame.plot , which uses matplotlib as the backend.
# 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')

在此处输入图像描述

Code without pandaspandas代码

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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM