How to plot a multivariate function in Python Matplotlib?

A multivariate function involves multiple input variables that produce an output. In Python, we can visualize such functions using Matplotlib with scatter plots and color mapping to represent the third dimension.

Basic Multivariate Function Plot

Let's create a scatter plot where colors represent the function values ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True

# Define a multivariate function
def func(x, y):
    return 3 * x + 4 * y - 2

# Generate sample data
x = np.random.randn(50) * 2
y = np.random.randn(50) * 3
z = func(x, y)

# Create the plot
fig, ax = plt.subplots()
scatter = ax.scatter(x, y, c=z, s=100, marker='o', cmap='viridis', alpha=0.7)

# Add colorbar and labels
fig.colorbar(scatter, label='Function Value (z)')
ax.set_xlabel('X values')
ax.set_ylabel('Y values')
ax.set_title('Multivariate Function: z = 3x + 4y - 2')

plt.show()

3D Surface Plot

For better visualization, we can create a 3D surface plot ?

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Create a grid of x and y values
x = np.linspace(-3, 3, 30)
y = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, y)

# Define the multivariate function
Z = 3 * X + 4 * Y - 2

# Create 3D surface plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

surface = ax.plot_surface(X, Y, Z, cmap='coolwarm', alpha=0.8)

# Add labels and colorbar
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z = 3x + 4y - 2')
ax.set_title('3D Surface Plot of Multivariate Function')
fig.colorbar(surface)

plt.show()

Contour Plot

Contour plots show level curves of the function ?

import numpy as np
import matplotlib.pyplot as plt

# Create grid
x = np.linspace(-4, 4, 100)
y = np.linspace(-4, 4, 100)
X, Y = np.meshgrid(x, y)

# Define a more complex multivariate function
Z = X**2 + Y**2 - 2*X*Y + 3*X - Y + 5

# Create contour plot
fig, ax = plt.subplots(figsize=(8, 6))
contour = ax.contour(X, Y, Z, levels=15, colors='black', alpha=0.6)
contourf = ax.contourf(X, Y, Z, levels=15, cmap='RdYlBu')

# Add labels and colorbar
ax.clabel(contour, inline=True, fontsize=8)
fig.colorbar(contourf)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Contour Plot: z = x² + y² - 2xy + 3x - y + 5')

plt.show()

Comparison of Visualization Methods

Method Best For Dimensions Shown
Scatter Plot Discrete data points 2D with color mapping
Surface Plot Continuous functions True 3D visualization
Contour Plot Level curves analysis 2D with iso-lines

Conclusion

Use scatter plots for discrete multivariate data, surface plots for 3D visualization of continuous functions, and contour plots to analyze level curves. Choose the method based on your data type and visualization needs.

Updated on: 2026-03-26T19:02:06+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements