Matplotlib is one of the most widely used data visualization libraries in Python. From simple to complex visualizations, it’s the go-to library for most.
In this tutorial, we’ll take a look at how to plot a scatter plot in Matplotlib.
We’ll be using the Ames Housing dataset and visualizing correlations between features from it.
Let’s import Pandas and load in the dataset:
import pandas as pd
df = pd.read_csv('AmesHousing.csv')
Now, with the dataset loaded, let’s import Matplotlib, decide on the features we want to visualize, and construct a scatter plot:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('AmesHousing.csv')
fig, ax = plt.subplots(figsize=(10, 6))
ax.scatter(x = df['Gr Liv Area'], y = df['SalePrice'])
plt.xlabel("Living Area Above Ground")
plt.ylabel("House Price")
plt.show()
Here, we’ve created a plot, using the PyPlot instance, and set the figure size. Using the returned Axes
object, which is returned from the subplots()
function, we’ve called the scatter()
function.
#python #matplotlib #data science