Can we have more than two side by side bars for the bar chart?


#1

Question

In the context of this exercise, can we have more than two side by side bars for the bar chart?

Answer

Yes, you can set any number of bars side by side for the bar chart. To do this, you can utilize the provided create_x() function, which will return the x values for each group of bars.

For the create_x() function, two parameters will be of importance. The t parameter determines how many sets of data there are, and the n parameter determines which set of bars the current data is for.

To add, for example, a third set of bars to this graph, we can do the following.

# First create the datasets
middle_school_a = [80, 85, 84, 83, 86]
middle_school_b = [73, 78, 77, 82, 86]
middle_school_c = [80, 85, 87, 92, 90]

# Create the x values for each using the create_x() function.
# Set t = 3 for the three sets of data.
school_a_x = create_x(3, 0.8, 1, 5)
school_b_x = create_x(3, 0.8, 2, 5)
school_c_x = create_x(3, 0.8, 3, 5)

# Plot the three sets of bars.
plt.bar(school_a_x, middle_school_a)
plt.bar(school_b_x, middle_school_b)
plt.bar(school_c_x, middle_school_c)

# You can also set the x positions for each x label,
# taking the average of the three x values.
middle_x = [ (a + b + c) / 3.0 for a, b, c in zip(school_a_x, school_b_x, school_c_x)]
ax.set_xticks(middle_x)