Here are some of the most common and useful plots you can create with Pandas. Note that the plot methods on a Series or DataFrame are just simple wrappers around matplotlib functions. This is why you might see these them used interchangeably. Pandas is nice for quick insights, but you'll need to use matplotlib to really dive into details and customize your visualizations. We'll get into this more later on.
Let's first use census income data to practice plotting histograms, bar charts, and pie charts.
import pandas as pd
# This allows us to view visualizations in Jupyter notebook - useful!!
%matplotlib inline
# View summary of census income data
df_census = pd.read_csv('census_income_data.csv')
df_census.info()
df_census.head()
# This is quick way to view histograms for all numeric columns
df_census.hist()
# That was way too crowded, let's make our figure size bigger
# Also, we can use a semicolon to suppress unwanted output
df_census.hist(figsize=(8,8));
# We can also get a histogram for a single column like this
df_census['age'].hist();
# We can also plot a histogram using this more general function
df_census['age'].plot(kind='hist');
Next, let's plot a bar chart. For this, we need counts for each distinct value (or bar).
# This function aggregates counts for each unique value in a column
print(df_census['education'].value_counts());
# We can use value counts to plot our bar chart
df_census['education'].value_counts().plot(kind='bar');
# Value counts are also required for pie charts
df_census['workclass'].value_counts().plot(kind='pie', figsize=(8, 8));
Now, let's use cancer data to practice plotting scatter plots and box plots.
df_cancer = pd.read_csv('cancer_data_edited.csv')
df_cancer.info()
This next function is really cool for getting quick insight into the relationships among numeric variables with scatterplots. It also displays a histogram for each variable.
# Create scatter matrix, make figure size big enough to display clearly
pd.plotting.scatter_matrix(df_cancer, figsize=(15, 15));
# Create a single scatter plot like this
df_cancer.plot(x='compactness', y='concavity', kind='scatter');
# Create a box plot like this
df_cancer['concave_points'].plot(kind='box');