Scatter plots are a common kind of statistical graph. In this blog, we’ll show you how to use Python to generate several different styles of scatter plots. Note: If you don’t have these libraries installed, begin with: pip install matplotlib seaborn numpy pandas.
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
# Random data
np.random.seed(0)
data1 = np.random.rand(100)
data2 = data1 + np.random.rand(100) * 0.1
# Create a DataFrame for seaborn usage
df = pd.DataFrame({'Data1': data1, 'Data2': data2})
# Set the styles for seaborn
sns.set_style("whitegrid")
# Classic matplotlib scatter plot
plt.figure(figsize=(6, 6))
plt.scatter(data1, data2, color='blue')
plt.title('Classic Matplotlib Scatter plot')
plt.xlabel('Data1')
plt.ylabel('Data2')
plt.show()
# Scatter plot with seaborn
plt.figure(figsize=(6, 6))
sns.scatterplot(data=df, x='Data1', y='Data2', color='green')
plt.title('Seaborn Scatter plot')
plt.xlabel('Data1')
plt.ylabel('Data2')
plt.show()
# Scatter plot with seaborn - With regression line
plt.figure(figsize=(6, 6))
sns.regplot(data=df, x='Data1', y='Data2', color='purple')
plt.title('Seaborn Scatter plot with Regression Line')
plt.xlabel('Data1')
plt.ylabel('Data2')
plt.show()
# Scatter plot with seaborn - With density contours
plt.figure(figsize=(6, 6))
sns.kdeplot(data=df, x='Data1', y='Data2', color='red', fill=True)
plt.title('Seaborn Scatter plot with Density Contours')
plt.xlabel('Data1')
plt.ylabel('Data2')
plt.show()
# Scatter plot with seaborn - With histogram
plt.figure(figsize=(6, 6))
sns.jointplot(data=df, x='Data1', y='Data2', color='orange')
plt.title('Seaborn Scatter plot with Histogram')
plt.show()
This gives you an idea of Python’s flexibility in scatter plot generation.
BridgeText can help you with all of your statistical analysis needs.