Automate matplotlib for Scatter Plots
A Simple Python Example: Real-Time Scatter Plot with Linear Regression
Are you a beginner in Python and interested in data visualization and simple machine learning models? Then this post is for you! We will use Python to create a real-time scatter plot, and implement a linear regression model that updates in real-time using scikit-learn.
Prerequisites
- Python installed
- Matplotlib library installed
- NumPy library installed
- scikit-learn library installed
Code Overview
The code does the following:
- Imports necessary libraries
- Initializes an empty list for x and y values
- Sets up the plot
- Runs a loop to generate random points and update the plot
- Performs linear regression on the points every 20 iterations
Let's Dive into the Code
Step 1: Importing Libraries
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
import random
Step 2: Initialize Lists and the Linear Regression Model
reg = LinearRegression()
x_values = []
y_values = []
Step 3: Set up the Plot
plt.xlim(0, 100)
plt.ylim(0, 100)
Step 4: The Main Loop
for i in range(1000):
plt.clf()
x_values.append(random.randint(0, 100))
y_values.append(random.randint(0, 100))
x = np.array(x_values)
x = x.reshape(-1,1)
y = np.array(y_values)
y = y.reshape(-1,1)
Step 5: Perform Linear Regression
if i % 20 == 0:
reg.fit(x, y)
plt.xlim(0,100)
plt.ylim(0,100)
plt.scatter(x_values, y_values, color='black')
plt.plot(list(range(100)), reg.predict(np.array([x for x in range(100)]).reshape(-1,1)))
plt.pause(0.001)
plt.show()
Understanding the Code
The loop runs 1000 times, each time generating a random point and adding it to our lists of x and y values. These points are then plotted as a scatter plot. Every 20 iterations, the code performs linear regression on the accumulated points and updates the regression line on the plot.
Conclusion
This example provides an introduction to real-time data visualization and machine learning using Python. With just a few lines of code, we were able to generate a dynamic scatter plot and a linear regression model that updates in real-time.
This post is made possible by: NeuralNine
Comments
Post a Comment