1

I am generating rectangular patches as shown below but some rectangles have different sizes even though I am using a constant dimension. I want all rectangles to be of the same size. How do I fix this?

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.colors import Normalize
from matplotlib import cm

fig,ax = plt.subplots(1)

n=67
i=1
j=5
Factor=1000
for i in range(0, n):
    rect = mpl.patches.Rectangle((Factor+Factor*i, Factor+Factor*j), 10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
    ax.add_patch(rect)
ax.set_xlim(left = 0, right = Factor*1.1*n)
ax.set_ylim(bottom = 0, top = Factor*1.1*n)
plt.show()

enter image description here

2
  • 2
    Tried your code and didn't see different size of rectangles. Should be display issue. Try to enlarge your figure by using figsize in subplots, fig,ax = plt.subpots(1, figsize=(30,10)). If still have issue, please let us know your python/matplotlib version. Commented Feb 25, 2023 at 10:38
  • 1
    You can use Rectangle(..., snap=False) to prevent matplotlib from snapping lines to pixel position. For very short distances, this won't be 100% perfect, but it will look more evenly. Commented Feb 25, 2023 at 20:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.