Created
January 9, 2023 16:06
-
-
Save muhammadanas0716/57bab1f1b5ace67db96786ad79646c83 to your computer and use it in GitHub Desktop.
Scatter Plot Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import needed libraries | |
import numpy as np | |
import pandas as pd | |
import matplotlib as mpl | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
# Import dataset | |
midwest = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv") | |
# Prepare Data | |
# Create as many colors as there are unique midwest['category'] | |
categories = np.unique(midwest['category']) | |
colors = [plt.cm.tab10(i / float(len(categories)-1)) for i in range(len(categories))] | |
# Draw Plot for Each Category | |
plt.figure(figsize=(10, 7), dpi= 120, facecolor='w', edgecolor='k') | |
for i, category in enumerate(categories): | |
plt.scatter('area', 'poptotal', | |
data=midwest.loc[midwest.category==category, :], | |
s=20, c=colors[i], label=str(category)) | |
# Decorations | |
plt.gca().set(xlim=(0.0, 0.1), ylim=(0, 90000), | |
xlabel='Area', ylabel='Population') | |
plt.xticks(fontsize=12); plt.yticks(fontsize=12) | |
plt.title("Scatterplot of Midwest Area vs Population", fontsize=22) | |
plt.legend(fontsize=12) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment