Learn

For categorical variables, bar charts and pie charts are common options for visualizing the count (or proportion) of values in each category. They can also convey the relative frequencies of each category.

Python’s seaborn library offers several functions that can create bar charts. The simplest for plotting the counts is countplot():

# Bar chart for borough sns.countplot(x='borough', data=rentals) plt.show() plt.close()

Bar chart of borough

There are currently no functions in the seaborn library for creating a pie chart, but the pandas library provides a convenient wrapper function around matplotlib‘s pie() function that can generate a pie chart from any column in a DataFrame:

# Pie chart for borough rentals.borough.value_counts().plot.pie() plt.show() plt.close()

Pie chart of borough

In general, many data analysts avoid pie charts because people are better at visually comparing areas of rectangles than wedges of a pie. For a variable with a small number of categories (i.e., fewer than three), a pie chart is a reasonable choice; however, for more complex data, a bar chart is usually preferable.

Instructions

1.

Using the movies DataFrame, create a bar chart for genre using the countplot() function from seaborn. Don’t forget to display the plot using plt.show() and close the plot using plt.close().

2.

Create a pie chart for genre using the .pie() method from pandas.

From the plots, what do you notice about the relative frequencies of movie genres?

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?