0

I have a function f(x,y) written in python 3 which takes integer input and outputs either 1 or 0.

My plan is to illustrate this function with black and white squares on a square grid, but i have no idea of how to do it. What could i do?

Ola
  • 121
  • 4

1 Answers1

0

To plot a grid of data with matplotlib use imshow:

import numpy as np
import matplotlib.pyplot as plt

def f(x,y):
    return (x+y)%2

x = np.arange(-5, 5, 1)
y = np.arange(-5, 5, 1)
xx, yy = np.meshgrid(x, y, sparse=True)

zz = f(xx,yy)

fig, ax = plt.subplots()
ax.imshow(zz,interpolation='none',cmap='gray')
plt.show()

If your f is more complex, you have to vectorize it first:

f = np.vectorize(f)
koalo
  • 2,113
  • 20
  • 31