Trikonturfa highway with a hole in the middle.

I have some data defined on regular Cartesian grids. I would like to show only some of them with a condition based on the radius from the center. This will effectively create an annular structure with a hole in the center. As a result, I cannot use imshow. tricontourfor tripcolor- this is what I found for this. My code looks something like this:

R = np.sqrt(x**2+y**2)
flag = (R<150)*(R>10)
plt.tricontourf(x[flag], y[flag], data[flag], 100)

where xand yare the grids of the grid, where it datadetermines. The problem here is that, as tricontourfwell as tripcolortrying to fill in the middle of the ring, where, I hope, can be left blank.

To be more specific, the one on the left is similar to what I want, but I can only get the one on the right with this code snippet shown above.

Chart Examples

+4
source share
2 answers

The following shows how to mask some parts of a graph based on a condition. Use imshowis quite possible, and what the script does below.

The idea is to set all the unwanted parts of the charts to nan. To reduce the values nan, we can set their alpha to 0, basically making a transparent graph at these points.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-150, 150, 300)
y = np.linspace(-150, 150, 300)
X,Y = np.meshgrid(x,y)
data = np.exp(-(X/80.)**2-(Y/80.)**2)

R = np.sqrt(X**2+Y**2)
flag =np.logical_not( (R<110) * (R>10) )
data[flag] = np.nan

palette = plt.cm.jet
palette.set_bad(alpha = 0.0)

im = plt.imshow(data)

plt.colorbar(im)
plt.savefig(__file__+".png")
plt.show()

enter image description here


, tricontourf , . matplotlib , , SO .
+2

np.nan np.inf. ( , 1), , .

0

Source: https://habr.com/ru/post/1667101/


All Articles