leftaroundabout - 2 years ago 113

Python Question

Say I have a function like

`def coulomb(x,y):`

r = sqrt(x**2 + y**2)

return 1/r if r > 1 else None

How could I best plot this in a colour plot so every

`None`

`fig = plt.figure()`

xs, ys = meshgrid(linspace(-5, 5, n), linspace(-5, 5, n))

vs = 1/sqrt(xs**2 + ys**2)

ax = fig.add_subplot(1, 1, 1, aspect='equal')

fig.colorbar(ax.pcolor(xs,ys,vs, vmin=0, vmax=1))

but with the center area blank instead of deep-red.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Just use masked arrays:

```
from numpy import ma
vs_ma = ma.masked_where(vs > 1, vs)
plt.colorbar(plt.pcolor(xs, ys, vs_ma, vmin=0, vmax=1))
```

matplotlib has a more complicated example image_masked.py where you can select the color for masked zones. To convert between an ordinary array and a masked array you can use one of the numpy.ma.masked_* functions

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**