How I can get a Cartesian coordinate system in Matplotlib?

To create a Cartesian coordinate system in Matplotlib, you can plot data points on an X-Y plane with proper axes and grid lines. This involves setting up the coordinate system with axes, plotting data points, and customizing the appearance for better visualization.

Basic Steps

To plot a Cartesian coordinate system in matplotlib, follow these steps ?

  • Initialize variables and create data points for x and y coordinates
  • Plot the points using scatter() method with x and y data points
  • Add grid lines and axis labels for better visualization
  • Display the figure using show() method

Example

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Generate random data points
N = 50
x = np.random.rand(N)
y = np.random.rand(N)

# Create scatter plot
plt.scatter(x, y, alpha=0.7)

# Add grid and labels
plt.grid(True, alpha=0.3)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Cartesian Coordinate System')

# Display the plot
plt.show()

Enhanced Cartesian System with Axes

For a more traditional Cartesian coordinate system with visible axes through the origin ?

import numpy as np
import matplotlib.pyplot as plt

# Create data points
x = np.random.uniform(-2, 2, 30)
y = np.random.uniform(-2, 2, 30)

# Create the plot
fig, ax = plt.subplots(figsize=(8, 6))

# Plot points
ax.scatter(x, y, c='red', alpha=0.7, s=50)

# Add axes through origin
ax.axhline(y=0, color='black', linewidth=0.8)
ax.axvline(x=0, color='black', linewidth=0.8)

# Add grid
ax.grid(True, alpha=0.3)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Enhanced Cartesian Coordinate System')

# Set equal aspect ratio
ax.set_aspect('equal')

plt.show()

Customizing the Coordinate System

You can further customize the appearance with different markers, colors, and axis properties ?

import numpy as np
import matplotlib.pyplot as plt

# Create sample data
theta = np.linspace(0, 2*np.pi, 20)
x = np.cos(theta) + np.random.normal(0, 0.1, 20)
y = np.sin(theta) + np.random.normal(0, 0.1, 20)

plt.figure(figsize=(8, 8))

# Plot with custom styling
plt.scatter(x, y, c=theta, cmap='viridis', s=100, alpha=0.8)

# Customize axes
plt.axhline(y=0, color='black', linewidth=1.2)
plt.axvline(x=0, color='black', linewidth=1.2)
plt.grid(True, alpha=0.4)

# Set labels and limits
plt.xlabel('X-coordinate', fontsize=12)
plt.ylabel('Y-coordinate', fontsize=12)
plt.title('Customized Cartesian Coordinate System', fontsize=14)
plt.xlim(-2, 2)
plt.ylim(-2, 2)

# Add colorbar
plt.colorbar(label='Angle (radians)')

plt.show()

Conclusion

Creating a Cartesian coordinate system in Matplotlib involves plotting data points with scatter(), adding grid lines with grid(), and enhancing visualization with axes through the origin using axhline() and axvline(). This provides a clear framework for displaying data relationships in 2D space.

Updated on: 2026-03-25T20:11:17+05:30

915 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements