Shading Using Patterns in VCS

This notebook shows how to use patterns in vcs.

Pattern can be used with isofill, boxfill, meshfill and fillarea object.

In this notebook we are using primirarly boxfill

Prepare Notebook Elements

(top)

In [1]:
import requests
import cdms2

r = requests.get("https://uvcdat.llnl.gov/cdat/sample_data/clt.nc",stream=True)
with open("clt.nc","wb") as f:
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:  # filter local_filename keep-alive new chunks
            f.write(chunk)

# and load data
f = cdms2.open("clt.nc")
clt = f("clt",time=slice(0,1),squeeze=1) # Get first month

Create default Graphic Method

(top)

In [2]:
import vcs
import cdms2
x=vcs.init(bg=True)
gm = vcs.createboxfill()
gm.boxfill_type = "custom"
In [3]:
# Let's look at the data w/o pattern
x.plot(clt,gm)
Out[3]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_6_0.png

Mask some data

Now let’s assume we are only interested in areas where clt is greater than 60% let’s shade out areas where clt is < 60%

(top)

In [4]:
import MV2
bad = MV2.less(clt,60.).astype("f")

Method 1: Regular Masking

(top)

In [5]:
# let's create a second boxfill method
gm2 = vcs.createboxfill()
gm2.boxfill_type = "custom"
# and a template for it
tmpl2 = vcs.createtemplate()
tmpl2.legend.priority=0
gm2.levels = [[0.5,1.]]
gm2.fillareacolors = ["black",]
x.plot(bad,gm2,tmpl2)
Out[5]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_10_0.png

Method 2: Using Opacity

Let’s use some opacity to “see” what’s bellow (top)

In [6]:
gm2.fillareaopacity = [50]
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[6]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_12_0.png

Method 3: Using Patterns

Rather than opacity, we can use patterns, that let us see better what’s “underneath” (top)

In [7]:
gm2.fillareastyle = "pattern"
gm2.fillareaindices = [10]
gm2.fillareaopacity = [100]
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[7]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_14_0.png
In [8]:
# we can control the size of patterns
gm2.fillareapixelscale = 2.
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[8]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_15_0.png

Controling Patterns Size

We can make the patterns bigger or smaller, using spacing (top)

In [9]:
# Bigger
gm2.fillareapixelspacing = [20,20]
gm2.fillareapixelscale=None
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[9]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_17_0.png
In [10]:
# or smaller
gm2.fillareapixelspacing = [5,5]
gm2.fillareapixelscale=None
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[10]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_18_0.png

Size and Opacity

We can still add opacity (top)

In [11]:
gm2.fillareaopacity = [25.]
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[11]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_20_0.png

Pattern Color can also be controled

Using hatch rather than pattern we can control the shading color (top)

In [12]:
gm2.fillareaopacity = [100.]
gm2.fillareastyle = "hatch"
gm2.fillareacolors = ["red"]
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[12]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_22_0.png

Patterns legend

(top)

In [13]:
# could even have a legend
tmpl2.legend.x1 = .54
tmpl2.legend.x2 = .62
tmpl2.legend.y1 = .885
tmpl2.legend.y2 = .985
tmpl2.legend.priority=1
gm2.legend = {.5:" Bad"}
x.clear()
x.plot(clt,gm)
x.plot(bad,gm2,tmpl2)
Out[13]:
../_images/Jupyter_Shading_With_Patterns_in_VCS_24_0.png