简体   繁体   中英

How to plot discrete f(x,y) output

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM