Let’s check out another chart: the bar chart.
Bar charts use the length of bars to compare categorical data. For example, a bar chart could show the heights of different trial groups of pea plants, the GDPs of five countries, or the number of flu vaccines administered at a pharmacy each day in October.
The bar chart graph function is plt.bar()
. It takes the following parameters:
x
: categorical data for each barheight
: numeric data to determine the height of each barwidth
: a number we can pass in to set the width of each baralign
: set to'center'
or'edge'
to align each bar on the x-axis
For example, for a bar chart comparing the yearly profits of 5 local businesses, our code might read:
plt.bar(x = data.business_name, height = data.profit, width = 0.8, align = 'center')
We’ll use general functions to add all the information that will make the graph readable. The code below demonstrates how we would add title and axis labels to the same graph.
plt.bar(x = data.business_name, height = data.profit, width = 0.8, align = 'center') plt.title("Profits at 5 Businesses, 2021") plt.xlabel("Business Name") plt.ylabel("Profit ($)")
We’ll practice making a bar chart in the Jupyter notebook.
Instructions
Run the Setup cells to load in the necessary packages and datasets. Run the cell below to inspect the bar_data
dataset.
Above the plt.title()
code, write the code to make a bar chart of pH and average leaf width. Set the width
equal to 0.8
and align
to center
. The code for the title and axis labels is written for you already.