Esempio n. 1
0
        def transform(self, ll):
            longitude = ll[:, 0:1]
            latitude  = ll[:, 1:2]

            # Pre-compute some values
            half_long = longitude / 2.0
            cos_latitude = npy.cos(latitude)

            alpha = npy.arccos(cos_latitude * npy.cos(half_long))
            # Mask this array, or we'll get divide-by-zero errors
            alpha = ma.masked_where(alpha == 0.0, alpha)
            # We want unnormalized sinc.  numpy.sinc gives us normalized
            sinc_alpha = ma.sin(alpha) / alpha

            x = (cos_latitude * npy.sin(half_long)) / sinc_alpha
            y = (npy.sin(latitude) / sinc_alpha)
            x.set_fill_value(0.0)
            y.set_fill_value(0.0)
            return npy.concatenate((x.filled(), y.filled()), 1)
Esempio n. 2
0
        def transform(self, ll):
            longitude = ll[:, 0:1]
            latitude = ll[:, 1:2]

            # Pre-compute some values
            half_long = longitude / 2.0
            cos_latitude = npy.cos(latitude)

            alpha = npy.arccos(cos_latitude * npy.cos(half_long))
            # Mask this array, or we'll get divide-by-zero errors
            alpha = ma.masked_where(alpha == 0.0, alpha)
            # We want unnormalized sinc.  numpy.sinc gives us normalized
            sinc_alpha = ma.sin(alpha) / alpha

            x = (cos_latitude * npy.sin(half_long)) / sinc_alpha
            y = (npy.sin(latitude) / sinc_alpha)
            x.set_fill_value(0.0)
            y.set_fill_value(0.0)
            return npy.concatenate((x.filled(), y.filled()), 1)
Esempio n. 3
0
x = y = arange(-3.0, 3.0, delta)
X, Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z2-Z1)  # difference of Gaussians

# Set up a colormap:
palette = cm.gray
palette.set_over('r', 1.0)
palette.set_under('g', 1.0)
palette.set_bad('b', 1.0)
# Alternatively, we could use
# palette.set_bad(alpha = 0.0)
# to make the bad region transparent.  This is the default.
# If you comment out all the palette.set* lines, you will see
# all the defaults; under and over will be colored with the
# first and last colors in the palette, respectively.
Zm = ma.masked_where(Z > 1.2, Z)

# By setting vmin and vmax in the norm, we establish the
# range to which the regular palette color scale is applied.
# Anything above that range is colored based on palette.set_over, etc.

im = imshow(Zm, interpolation='bilinear',
    cmap=palette,
    norm = colors.Normalize(vmin = -1.0, vmax = 1.0, clip = False),
    origin='lower', extent=[-3,3,-3,3])
title('Green=low, Red=high, Blue=bad')
colorbar(im, extend='both', shrink=0.8)
show()
Esempio n. 4
0
import numpy as npy
from matplotlib.numerix import npyma as ma
from matplotlib.pyplot import step, legend, xlim, ylim, show

x = npy.arange(1, 7, 0.4)
y0 = npy.sin(x)
y = y0.copy() + 2.5

step(x, y, label='pre (default)')

y -= 0.5
step(x, y, where='mid', label='mid')

y -= 0.5
step(x, y, where='post', label='post')

y = ma.masked_where((y0>-0.15)&(y0<0.15), y - 0.5)
step(x,y, label='masked (pre)')

legend()

xlim(0, 7)
ylim(-0.5, 4)

show()

Esempio n. 5
0
from matplotlib.pyplot import figure, show, savefig
from matplotlib import cm, colors
from matplotlib.numerix import npyma as ma

n = 56
x = npy.linspace(-1.5, 1.5, n)
y = npy.linspace(-1.5, 1.5, n * 2)
X, Y = npy.meshgrid(x, y)
Qx = npy.cos(Y) - npy.cos(X)
Qz = npy.sin(Y) + npy.sin(X)
Qx = (Qx + 1.1)
Z = npy.sqrt(X**2 + Y**2) / 5
Z = (Z - Z.min()) / (Z.max() - Z.min())

# The color array can include masked values:
Zm = ma.masked_where(npy.fabs(Qz) < 0.5 * npy.amax(Qz), Z)

fig = figure()
ax = fig.add_subplot(121)
ax.set_axis_bgcolor("#bdb76b")
ax.pcolormesh(Qx, Qz, Z)
ax.set_title('Without masked values')

ax = fig.add_subplot(122)
ax.set_axis_bgcolor("#bdb76b")
#  You can control the color of the masked region:
#cmap = cm.jet
#cmap.set_bad('r', 1.0)
#ax.pcolormesh(Qx,Qz,Zm, cmap=cmap)
#  Or use the default, which is transparent:
col = ax.pcolormesh(Qx, Qz, Zm)
Esempio n. 6
0
#!/bin/env python
'''
Plot lines with points masked out.

This would typically be used with gappy data, to
break the line at the data gaps.
'''

import matplotlib.numerix.npyma as ma
from pylab import *

x = ma.arange(0, 2*pi, 0.02)
y = ma.sin(x)
y1 = sin(2*x)
y2 = sin(3*x)
ym1 = ma.masked_where(y1 > 0.5, y1)
ym2 = ma.masked_where(y2 < -0.5, y2)

lines = plot(x, y, 'r', x, ym1, 'g', x, ym2, 'bo')
setp(lines[0], linewidth = 4)
setp(lines[1], linewidth = 2)
setp(lines[2], markersize = 10)

legend( ('No mask', 'Masked if > 0.5', 'Masked if < -0.5') ,
        loc = 'upper right')
title('Masked line demo')
show()
Esempio n. 7
0
# use masked arrays to plot a line with different colors by y-value
import matplotlib.numerix.npyma as ma
from numpy import logical_or, arange, sin, pi
from matplotlib.pyplot import plot, show

t = arange(0.0, 2.0, 0.01)
s = sin(2 * pi * t)

upper = 0.77
lower = -0.77

supper = ma.masked_where(s < upper, s)
slower = ma.masked_where(s > lower, s)
smiddle = ma.masked_where(logical_or(s < lower, s > upper), s)

plot(t, slower, 'r', t, smiddle, 'b', t, supper, 'g')
show()
Esempio n. 8
0
import numpy as npy
from matplotlib.numerix import npyma as ma
from matplotlib.pyplot import step, legend, xlim, ylim, show

x = npy.arange(1, 7, 0.4)
y0 = npy.sin(x)
y = y0.copy() + 2.5

step(x, y, label='pre (default)')

y -= 0.5
step(x, y, where='mid', label='mid')

y -= 0.5
step(x, y, where='post', label='post')

y = ma.masked_where((y0 > -0.15) & (y0 < 0.15), y - 0.5)
step(x, y, label='masked (pre)')

legend()

xlim(0, 7)
ylim(-0.5, 4)

show()
Esempio n. 9
0
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z2 - Z1)  # difference of Gaussians

# Set up a colormap:
palette = cm.gray
palette.set_over('r', 1.0)
palette.set_under('g', 1.0)
palette.set_bad('b', 1.0)
# Alternatively, we could use
# palette.set_bad(alpha = 0.0)
# to make the bad region transparent.  This is the default.
# If you comment out all the palette.set* lines, you will see
# all the defaults; under and over will be colored with the
# first and last colors in the palette, respectively.
Zm = ma.masked_where(Z > 1.2, Z)

# By setting vmin and vmax in the norm, we establish the
# range to which the regular palette color scale is applied.
# Anything above that range is colored based on palette.set_over, etc.

im = imshow(Zm,
            interpolation='bilinear',
            cmap=palette,
            norm=colors.Normalize(vmin=-1.0, vmax=1.0, clip=False),
            origin='lower',
            extent=[-3, 3, -3, 3])
title('Green=low, Red=high, Blue=bad')
colorbar(im, extend='both', shrink=0.8)
show()
Esempio n. 10
0
#!/bin/env python
'''
Plot lines with points masked out.

This would typically be used with gappy data, to
break the line at the data gaps.
'''

import matplotlib.numerix.npyma as ma
from pylab import *

x = ma.arange(0, 2 * pi, 0.02)
y = ma.sin(x)
y1 = sin(2 * x)
y2 = sin(3 * x)
ym1 = ma.masked_where(y1 > 0.5, y1)
ym2 = ma.masked_where(y2 < -0.5, y2)

lines = plot(x, y, 'r', x, ym1, 'g', x, ym2, 'bo')
setp(lines[0], linewidth=4)
setp(lines[1], linewidth=2)
setp(lines[2], markersize=10)

legend(('No mask', 'Masked if > 0.5', 'Masked if < -0.5'), loc='upper right')
title('Masked line demo')
show()
Esempio n. 11
0
# use masked arrays to plot a line with different colors by y-value
import matplotlib.numerix.npyma as ma
from numpy import logical_or, arange, sin, pi
from matplotlib.pyplot import plot, show

t = arange(0.0, 2.0, 0.01)
s = sin(2 * pi * t)

upper = 0.77
lower = -0.77


supper = ma.masked_where(s < upper, s)
slower = ma.masked_where(s > lower, s)
smiddle = ma.masked_where(logical_or(s < lower, s > upper), s)

plot(t, slower, "r", t, smiddle, "b", t, supper, "g")
show()
Esempio n. 12
0
from matplotlib.pyplot import figure, show, savefig
from matplotlib import cm, colors
from matplotlib.numerix import npyma as ma

n = 56
x = npy.linspace(-1.5,1.5,n)
y = npy.linspace(-1.5,1.5,n*2)
X,Y = npy.meshgrid(x,y);
Qx = npy.cos(Y) - npy.cos(X)
Qz = npy.sin(Y) + npy.sin(X)
Qx = (Qx + 1.1)
Z = npy.sqrt(X**2 + Y**2)/5;
Z = (Z - Z.min()) / (Z.max() - Z.min())

# The color array can include masked values:
Zm = ma.masked_where(npy.fabs(Qz) < 0.5*npy.amax(Qz), Z)


fig = figure()
ax = fig.add_subplot(121)
ax.set_axis_bgcolor("#bdb76b")
ax.pcolormesh(Qx,Qz,Z)
ax.set_title('Without masked values')

ax = fig.add_subplot(122)
ax.set_axis_bgcolor("#bdb76b")
#  You can control the color of the masked region:
#cmap = cm.jet
#cmap.set_bad('r', 1.0)
#ax.pcolormesh(Qx,Qz,Zm, cmap=cmap)
#  Or use the default, which is transparent:
Esempio n. 13
0
    def _contour_args(self, *args):
        if self.filled: fn = 'contourf'
        else:           fn = 'contour'
        Nargs = len(args)
        if Nargs <= 2:
            z = ma.asarray(args[0], dtype=npy.float64)
            x, y = self._initialize_x_y(z)
        elif Nargs <=4:
            x,y,z = self._check_xyz(args[:3])
        else:
            raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn))
        self.zmax = ma.maximum(z)
        self.zmin = ma.minimum(z)
        if self.logscale and self.zmin <= 0:
            z = ma.masked_where(z <= 0, z)
            warnings.warn('Log scale: values of z <=0 have been masked')
            self.zmin = z.min()
        self._auto = False
        if self.levels is None:
            if Nargs == 1 or Nargs == 3:
                lev = self._autolev(z, 7)
            else:   # 2 or 4 args
                level_arg = args[-1]
                try:
                    if type(level_arg) == int:
                        lev = self._autolev(z, level_arg)
                    else:
                        lev = npy.asarray(level_arg).astype(npy.float64)
                except:
                    raise TypeError(
                        "Last %s arg must give levels; see help(%s)" % (fn,fn))
            if self.filled and len(lev) < 2:
                raise ValueError("Filled contours require at least 2 levels.")
            # Workaround for cntr.c bug wrt masked interior regions:
            #if filled:
            #    z = ma.masked_array(z.filled(-1e38))
            # It's not clear this is any better than the original bug.
            self.levels = lev
        #if self._auto and self.extend in ('both', 'min', 'max'):
        #    raise TypeError("Auto level selection is inconsistent "
        #                             + "with use of 'extend' kwarg")
        self._levels = list(self.levels)
        if self.extend in ('both', 'min'):
            self._levels.insert(0, min(self.levels[0],self.zmin) - 1)
        if self.extend in ('both', 'max'):
            self._levels.append(max(self.levels[-1],self.zmax) + 1)
        self._levels = npy.asarray(self._levels)
        self.vmin = npy.amin(self.levels)  # alternative would be self.layers
        self.vmax = npy.amax(self.levels)
        if self.extend in ('both', 'min'):
            self.vmin = 2 * self.levels[0] - self.levels[1]
        if self.extend in ('both', 'max'):
            self.vmax = 2 * self.levels[-1] - self.levels[-2]
        self.layers = self._levels # contour: a line is a thin layer
        if self.filled:
            self.layers = 0.5 * (self._levels[:-1] + self._levels[1:])
            if self.extend in ('both', 'min'):
                self.layers[0] = 0.5 * (self.vmin + self._levels[1])
            if self.extend in ('both', 'max'):
                self.layers[-1] = 0.5 * (self.vmax + self._levels[-2])

        return (x, y, z)
Esempio n. 14
0
#!/usr/bin/env python
from pylab import *
import matplotlib.numerix.npyma as ma

N = 100
r0 = 0.6
x = 0.9*rand(N)
y = 0.9*rand(N)
area = pi*(10 * rand(N))**2 # 0 to 10 point radiuses
c = sqrt(area)
r = sqrt(x*x+y*y)
area1 = ma.masked_where(r < r0, area)
area2 = ma.masked_where(r >= r0, area)
scatter(x,y,s=area1, marker='^', c=c, hold='on')
scatter(x,y,s=area2, marker='o', c=c)
# Show the boundary between the regions:
theta = arange(0, pi/2, 0.01)
plot(r0*cos(theta), r0*sin(theta))

show()