2

I want to plot multiple squares of size 1x1 using meshgrid. The current and expected outputs are presented.

import numpy as np
import matplotlib.pyplot as plt
X = np.array([0,1,2])
Y = np.array([0, -1, -2])
xx, yy = np.meshgrid(X,Y)
plt.plot(xx, yy,"s")
plt.show()

The current output is

enter image description here

The expected output is

enter image description here

2
  • you should be looking for line collection, not plot. Commented Jul 2, 2022 at 14:36
  • The style "s" is for square bullet not for tracing grid. If you aim to draw squares you need to provide coordinate of them in right order using Patches or tracing vertical and horizontal lines using axhline and axvline. Commented Jul 2, 2022 at 14:55

1 Answer 1

1

I try to suggest the following solution. Given the assumption that we have 1x1 squares, meshgrid is not necessary:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

X = np.array([0, 1, 2])
Y = np.array([0, -1, -2])

x_sorted = np.sort(X)
y_sorted = np.sort(Y)

ax.set_xticks(x_sorted)
ax.set_yticks(y_sorted)

ax.set_xlim(x_sorted[0], x_sorted[-1])
ax.set_ylim(y_sorted[0], y_sorted[-1])

ax.grid()

ax.set_aspect('equal', 'box')

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.