You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python为函数内外的点绘制像素而非仅进行常规绘图?

Got it, let's figure out how to plot your points as actual pixels in Python instead of the usual markers. I'll walk you through two reliable approaches that get the job done:

Approach 1: Create a Pixel Grid with NumPy & Matplotlib.imshow

This method builds a grid where each cell represents a pixel, then colors the cells corresponding to your points. It's great for large datasets since it's efficient.

import numpy as np
import matplotlib.pyplot as plt

# Example data: replace these with your actual points
inner_points = [(10, 20), (15, 25), (20, 20)]  # Points inside the function region
outer_points = [(5, 5), (30, 30), (10, 5)]     # Points outside the function region

# Calculate the bounds of our pixel grid
max_x = max(p[0] for p in inner_points + outer_points) + 1
max_y = max(p[1] for p in inner_points + outer_points) + 1

# Create a blank black grid (0 = black)
pixel_grid = np.zeros((max_y, max_x), dtype=np.uint8)

# Color inner points white (255) and outer points gray (127)
for x, y in inner_points:
    pixel_grid[y, x] = 255  # Note: imshow uses (row, column), so y comes first
for x, y in outer_points:
    pixel_grid[y, x] = 127

# Plot the pixel grid
plt.figure(figsize=(8, 8))
plt.imshow(pixel_grid, cmap='gray', interpolation='none', origin='lower')
plt.title('Pixel Grid: Inner vs Outer Points')
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.show()

Key Notes for This Method:

  • origin='lower' ensures the coordinate system matches standard plotting (origin at bottom-left) instead of imshow's default top-left.
  • interpolation='none' keeps pixels sharp—no blurring between adjacent points.
  • If your points have float coordinates, round and convert to integers first with int(round(x)) and int(round(y)).
Approach 2: Use Scatter Plot with Pixel-Sized Markers

If you prefer a simpler setup without building a grid, you can use plt.scatter with tiny, square markers to mimic pixels.

import matplotlib.pyplot as plt

# Example data (same as above)
inner_points = [(10, 20), (15, 25), (20, 20)]
outer_points = [(5, 5), (30, 30), (10, 5)]

# Split points into x and y lists
inner_x, inner_y = zip(*inner_points)
outer_x, outer_y = zip(*outer_points)

plt.figure(figsize=(8, 8))
# Plot outer points as gray pixels
plt.scatter(outer_x, outer_y, color='gray', s=1, marker='s')
# Plot inner points as white pixels
plt.scatter(inner_x, inner_y, color='white', s=1, marker='s')

# Customize the plot for better visibility
plt.gca().set_facecolor('black')  # Black background makes pixels pop
plt.title('Pixel-Like Scatter Plot')
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
# Adjust axes to fit all points with a small buffer
plt.xlim(-1, max(max(inner_x), max(outer_x)) + 1)
plt.ylim(-1, max(max(inner_y), max(outer_y)) + 1)
plt.show()

Key Notes for This Method:

  • s=1 sets the marker size to 1 pixel, and marker='s' uses square markers to look like actual pixels.
  • This is easier for small datasets, but can get slow if you have thousands of points (the grid method is better for large data).
Quick Tips for Better Results
  • For color instead of grayscale, use a 3-channel RGB grid (shape (max_y, max_x, 3)) and set values like [255,0,0] for red, [0,255,0] for green.
  • If your points are spread out over a huge range, consider normalizing coordinates to fit a reasonable grid size (e.g., scale x and y to 0-500 pixels).

内容的提问来源于stack exchange,提问作者random name

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:17:07