SciPy helps us do math and science calculations easily. Matplotlib lets us draw pictures of data so we can understand it better.
0
0
SciPy with Matplotlib for visualization
Introduction
You want to find the best curve that fits your data points.
You need to solve math problems like integration or optimization and show the results.
You want to compare two sets of data visually after doing calculations.
You have scientific data and want to plot graphs to explain it clearly.
Syntax
SciPy
import numpy as np from scipy import optimize import matplotlib.pyplot as plt # Define a function to fit def f(x, a, b): return a * x + b # Sample data xdata = np.array([1, 2, 3, 4, 5]) ydata = np.array([2, 4, 6, 8, 10]) # Use optimize.curve_fit to find best a, b params, covariance = optimize.curve_fit(f, xdata, ydata) # Plot original data and fitted curve plt.scatter(xdata, ydata) plt.plot(xdata, f(xdata, *params)) plt.show()
Use optimize.curve_fit to fit a function to data points.
Matplotlib's plt.scatter shows data points, plt.plot draws lines.
Examples
This example fits a straight line to points and shows both on a graph.
SciPy
import numpy as np from scipy import optimize import matplotlib.pyplot as plt # Sample data xdata = np.array([1, 2, 3, 4, 5]) ydata = np.array([2.1, 4.1, 6.0, 8.1, 10.2]) # Define linear function def linear(x, a, b): return a * x + b # Fit data params, _ = optimize.curve_fit(linear, xdata, ydata) # Plot plt.scatter(xdata, ydata, label='Data') plt.plot(xdata, linear(xdata, *params), label='Fit', color='red') plt.legend() plt.show()
This example calculates the area under sin(x) from 0 to π and shows it shaded on the plot.
SciPy
import numpy as np from scipy import integrate import matplotlib.pyplot as plt # Define function to integrate def f(x): return np.sin(x) # Calculate integral from 0 to pi result, error = integrate.quad(f, 0, np.pi) # Plot function x = np.linspace(0, np.pi, 100) y = f(x) plt.plot(x, y, label='sin(x)') plt.fill_between(x, 0, y, alpha=0.3) plt.title(f'Integral from 0 to pi = {result:.2f}') plt.legend() plt.show()
Sample Program
This program creates noisy data points that roughly follow a line. It uses SciPy to find the best line that fits the points. Then it draws the points and the fitted line on a graph.
SciPy
import numpy as np from scipy import optimize import matplotlib.pyplot as plt # Data points with some noise xdata = np.linspace(0, 10, 20) ydata = 3.5 * xdata + 2 + np.random.normal(0, 2, size=xdata.size) # Define linear function to fit def linear_func(x, a, b): return a * x + b # Fit the data params, covariance = optimize.curve_fit(linear_func, xdata, ydata) # Print fitted parameters a_fit, b_fit = params print(f'Fitted line: y = {a_fit:.2f} * x + {b_fit:.2f}') # Plot data and fitted line plt.scatter(xdata, ydata, label='Data points') plt.plot(xdata, linear_func(xdata, *params), color='red', label='Fitted line') plt.xlabel('x') plt.ylabel('y') plt.title('Linear Fit using SciPy and Matplotlib') plt.legend() plt.show()
OutputSuccess
Important Notes
Always check your data before fitting to avoid errors.
Matplotlib plots appear in a window or inline if using notebooks.
Fitting results depend on data quality and initial guesses.
Summary
SciPy helps with math tasks like fitting and integration.
Matplotlib shows data and results visually with plots.
Combining both makes data science easier and clearer.