Statement of Completion#8b843cce
Visualizations with Matplotlib
easy
Exploring relationships with US Census data
Resolution
Activities
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
In [2]:
df = pd.read_csv('hs_rate_poverty.csv')
df.head()
Out[2]:
Unnamed: 0 | State | normalized_hs_rate | normalized_poverty_rate | |
---|---|---|---|---|
0 | 0 | AK | 0.846274 | 0.198530 |
1 | 1 | AL | 0.803007 | 0.206471 |
2 | 2 | AR | 0.799495 | 0.229632 |
3 | 3 | AZ | 0.804671 | 0.256664 |
4 | 4 | CA | 0.819554 | 0.171247 |
Activities¶
1) Plotting and Comparing Socio-Economic Indicators with Matplotlib¶
In [3]:
# Creating a figure and an axis
fig, ax = plt.subplots(figsize=(14, 7))
# Plotting the data using scatter plots
ax.plot(df['State'], df['normalized_hs_rate'], label='High School Graduation Rates', marker='o', color='b')
ax.plot(df['State'], df['normalized_poverty_rate'], label='Poverty Rates', marker='x', color='r')
# Adding labels and title
ax.set_xlabel('States')
ax.set_ylabel('Normalized Rates')
ax.set_title('Comparison of High School Graduation and Poverty Rates by State')
ax.set_xlabel('States', fontsize=10, color='blue')
# Rotating x-axis labels by 90 degrees
ax.set_xticklabels(df['State'], fontsize=8,rotation=90)
# Adding a legend
ax.legend()
# Display the plot
plt.show()
/tmp/ipykernel_14/2783202139.py:15: UserWarning: FixedFormatter should only be used together with FixedLocator ax.set_xticklabels(df['State'], fontsize=8,rotation=90)
2) Scatter Plotting Socio-Economic Indicators¶
In [5]:
# Plotting the scatter plot
fig, ax = plt.subplots(figsize=(14, 7))
ax.scatter(df['normalized_hs_rate'], df['normalized_poverty_rate'])
# Adding labels and title
ax.set_xlabel('Normalized High School Graduation Rate')
ax.set_ylabel('Normalized Poverty Rate')
ax.set_title('Statewise Comparison of High School Graduation and Poverty Rates')
# Display the plot
plt.show()