To create plots and data visualization in Pandas, we can add the .plot() method to any DataFrame or Series object that has appropriate numeric data.
We can specify the title with the title= parameter and the kind of plot by altering the kind= parameter:
- ‘bar’ or ‘barh’ for bar plots (h is for horizontal)
- ‘hist’ for histogram
- ‘box’ for boxplot
- ‘kde’ or ‘density’ for density plots
- ‘area’ for area plots
- ‘scatter’ for scatter plots
- ‘hexbin’ for hexagonal bin plots
- ‘pie’ for pie plots
For example, we can visualize the data we got from our Groupby command looking at the total number of refugees by country of arrival as a bar chart:
refugee_df.groupby('origin')['arrivals'].count().sort_values(ascending=False)[:20].plot(kind='bar', title='Total number of refugee arrivals in the U.S. \n by country of origin')
Let’s unpack the command to better understand these results:
- refugee_df.groupby(‘origin’)[‘arrivals’].count().sort_values(ascending=False)[:20]: This is the same command we used in lesson 7 to count the number of refugee arrivals by country of origin, with the output showing the top twenty rows sorted by descending order:
- We have three stacked methods here: .groupby(), .count(), and .sort_values().
- groupby(‘origin’)[‘arrivals’]: For the Groupby object we defined in lesson 7, groupby(‘origin’), we are isolating the “arrivals” column. Basically, we are asking to view the number of refugee arrivals by country of origin.
- .count(): This method counts non-blank cells for each column or row. The results we see in the output show the total number of refugee arrivals by country of origin.
- .sort_values(ascending=False): This method specifies how we want our output to be sorted. We include the ascending=False parameter in order to request that the data be displayed with the highest percentage first.
- [:20]: This Python slide specifies that we just want to see the top 20 rows.
- .plot(kind=’bar’, title=’Total number of refugee arrivals in the U.S. \n by country of origin’):
- Here we are using the .plot() method to create a visualization, and we are specifying that we want a bar chart with the “kind=’bar’” parameter. We are also giving the chart a title with the “title=’Total number of refugee arrivals in the U.S. \n by country of origin’” parameter.
- Note: By adding “\n” in the title text, we signify that the text that follows should be on a new line.
- Here we are using the .plot() method to create a visualization, and we are specifying that we want a bar chart with the “kind=’bar’” parameter. We are also giving the chart a title with the “title=’Total number of refugee arrivals in the U.S. \n by country of origin’” parameter.
We can also visualize the data as a pie chart:
refugee_df.groupby('origin')['arrivals'].count().sort_values(ascending=False)[:20].plot(kind='pie', title='Total number of refugee arrivals in the U.S. \n by country of origin')
In this case, the parameter within the .plot() method specifying the kind of chart we want changed from “bar” in the previous command to “pie”.
We can also create time series using the Groupby method. For example, if we wanted to visualize the total number of refugees resettled in the U.S. across the 2005-2015 period, we would first create a Groupby object based on the “year” column (refer back to lesson 7 for more on Groupby objects).
refugee_df.groupby('year')
Next, we can create a new variable calculating the average number of refugees being resettled over time.
total_arrivals_by_year = refugee_df.groupby('year')['arrivals'].sum()
Let’s break this command down:
- We have two stacked methods here: .groupby() and .sum()
- groupby(‘year’)[‘arrivals’]: For the Groupby object, groupby(year), we are isolating the “arrivals” column. Basically, we are asking to view the number of refugee arrivals by year.
- .sum(): This method returns the sum of the values over the requested axis. In our case, it will calculate the total number of refugee arrivals per year.
Finally, we can add the .plot() method to create a line chart.
total_arrivals_by_year.plot(kind='line', title="Total Number of Refugee Arrivals by Year")
In this command, we are adding the .plot() method to request a chart, and specifying that we want a line graph with the “kind=line” parameter. We are also giving the chart a title with the “title=’Total Number of Refugee Arrivals by Year’” parameter.


