Exemplo n.º 1
0
class SinCos(object):
    funcNames = ('sin', 'cos')
    filePath = "sc.png"

    def __init__(self):
        self.X = np.linspace(0, 4 * np.pi, 200)
        self.pt = Plotter(1, 2, width=700, height=500, useAgg=True)
        self.pt.set_xlabel("X")
        self.pt.use_grid()
        self.pt.add_annotation(0, "First")
        self.pt.add_annotation(199, "Last")

    def __call__(self, frequency):
        self.pt.set_title("Sin and Cosine: Frequency = {:.2f}x", frequency)
        with self.pt as p:
            for funcName in self.funcNames:
                Y = getattr(np, funcName)(frequency * self.X)
                p.set_ylabel("{}(X)".format(funcName))
                p(self.X, Y)
        with open(self.filePath, "wb") as fh:
            self.pt.show(fh=fh)
Exemplo n.º 2
0
# express or implied. See the License for the specific language
# governing permissions and limitations under the License.
"""
A log plot, with several plots in one subplot, all done in one
call to the subplotting tool in context.
"""

import numpy as np
from yampex import Plotter

# Construct a Plotter object for a 1000x800 pixel (100 DPI) figure
# with a single subplot.
pt = Plotter(1, width=10.0, height=8.0)
# The plots will be of different bases raised to powers.
X = np.linspace(0, 10, 100)
pt.set_title("Different Bases Raised to Power 0-10")
# The x label will say "Power" and the subplot will have a grid.
pt.set_xlabel("Power")
pt.use_grid()

# Make a subplotting context and work with the single subplot via the
# subplot tool sp. It's actually just a reference to pt, but set up
# for subplotting.
with pt as sp:
    # Compute the vectors all at once
    Y2 = np.power(2, X)
    Y3 = np.power(3, X)
    Y10 = np.power(10, X)
    # Just call the subplotting tool with the X-axis vector and all
    # the Ys at once.
    sp.semilogy(X, Y2, Y3, Y10, legend=["Base 2", "Base 3", "Base 10"])
Exemplo n.º 3
0
"""
A sine and cosine plot in each of two subplots, with a call to the
L{SpecialAx} object.
"""

import numpy as np
from yampex import Plotter

# Construct a Plotter object for a 700x500 pixel (100 DPI) figure with
# two subplots.
pt = Plotter(1, 2, width=7.0, height=5.0)
# The plots, one in each subplot, will be of a sine and cosine with
# 200 points from 0 to 4*pi.
funcNames = ('sin', 'cos')
X = np.linspace(0, 4 * np.pi, 200)
pt.set_title("Sine and Cosine")
# Each subplot will have an x-axis label of "X" and a grid.
pt.set_xlabel("X")
pt.use_grid()
# Each plot will have an annotation labeled "Last" at its last
# point. Note that we can use negative indices referenced to the last
# element, just as with Python sequences.
pt.add_annotation(-1, "Last")

# Make a subplotting context and work with the two subplots via the
# subplot tool sp. It's actually just a reference to pt, but set up
# for subplotting, with a context for a new subplot each time it's
# called.
with pt as sp:
    # Do each plot, sin and then cos.
    for k, funcName in enumerate(funcNames):
Exemplo n.º 4
0
Illustrates the use of L{options.OptsBase.use_timex} and calls to a
subplot-context instance of L{plot.Plotter} with multiple y-axis
arguments. Also shows a couple of intelligently-positioned
annotations.
"""

import numpy as np
from yampex import Plotter

EQs = ("sin(10*t)*cos(10000*t)", "cos(10*t)*cos(10000*t)")

N = 6
t = np.linspace(0, 2E-6, 1000)
# This time, we specify the plot dimensions in pixels with a tuple
pt = Plotter(N, figSize=(1600, 1200))
pt.set_title("With {:d} time scales: {}, {}", N, *EQs)
pt.use_timex()
pt.use_grid()
for eq in EQs:
    pt.add_legend(eq)
with pt as sp:
    for mult in range(N):
        X = t * 10**mult
        Y1 = np.sin(10 * X) * np.sin(10000 * X)
        Y2 = np.cos(10 * X) * np.cos(10000 * X)
        sp.set_title("0 - {:.5g} seconds", X[-1])
        if mult < 5 and np.any(Y2 < 0):
            sp.add_annotation(0.0, "Zero Crossing", kVector=1, y=True)
        sp(X, Y1, Y2)
pt.show()
Exemplo n.º 5
0
Illustrates the use of L{options.OptsBase.use_timex} and calls to a
subplot-context instance of L{plot.Plotter} with multiple y-axis
arguments.

Also illustrates how simply a row can be made twice as high as the
others.
"""

import numpy as np
from yampex import Plotter

N_sp = 4
N_pts = 200
X = np.linspace(0, 4 * np.pi, N_pts)
pt = Plotter(1, 4, width=10, height=10, h2=0)
pt.set_title("Sin(X) with increasingly noisy versions")
pt.set_zeroLine()
with pt as sp:
    Y = None
    for mult in np.logspace(-1.5, +0.5, N_sp):
        if Y is None:
            sp.add_textBox("SW",
                           "This subplot is twice as high as the others.")
        Y = np.sin(X)
        Y_noisy = Y + mult * np.random.randn(N_pts)
        k = np.argmax(np.abs(Y - Y_noisy))
        sp.add_textBox("NE", "Worst: {:+.3g} vs. {:+.3g}", Y_noisy[k], Y[k])
        ax = sp(X, Y)
        ax.plot(X, Y_noisy, 'r.')
pt.show()
Exemplo n.º 6
0
    """
    Returns a list with X and C{k*sin(k*X)} for I{k} from 1 to I{N},
    where X is a 1-D Numpy vector having 200 points from 0 to M{4*pi}.
    """
    X = np.linspace(0, 4 * np.pi, 200)
    return [X] + [k * np.sin(k * X) for k in range(1, N + 1)]


# The number of waveforms
N = 3
# Construct a Plotter object for a 800x500 pixel (specified directly
# in pixels) figure with a single subplot.
pt = Plotter(1, width=800, height=500)
# As with many methods of opts.OptsBase, you can specify the title
# with a string formatting prototype and its argument(s).
pt.set_title("Sine Waves with {:d} frequency & amplitude multipliers", N)
# The x label will say "X" and the subplot will have a grid.
pt.set_xlabel("X")
pt.use_grid()

# Make a subplotting context and work with the single subplot via the
# subplot tool sp. It's actually just a reference to pt, but set up
# for subplotting.
with pt as sp:
    # Add legends for the plots. Again, a string formatting prototype
    # and its argument are used for convenience.
    for mult in range(1, N + 1):
        sp.add_legend("x{:d}", mult)
    # Plot all the vectors in a single call to the subplot tool.
    sp(*vectors(N))
# Show the single subplot.
Exemplo n.º 7
0
class CurvePlotter(object):
    """
    I do the plotting.
    """
    width = 1400
    height = 1200
    xMin, xMax = 5.0, 8.0
    N = 100

    def __init__(self):
        """
        Constructs a Yampex Plotter object for a figure with two subplots.
        """
        self.pt = Plotter(2, width=self.width, height=self.height)
        self.pt.use_grid()
        self.pt.set_title("Exponentials plotted from {:.1f} to {:.1f}",
                          self.xMin, self.xMax)
        self.pt.set_xlabel("X")
        self.pt.set_ylabel("a*exp(-b*X)")

    def func(self, X, a, b):
        """
        The exponential function M{a*exp(-b*X)}
        """
        return a * np.exp(-b * X)

    def leastDiff(self, Ys, logspace=False):
        """
        Returns the index of the vectors of I{Ys} where there is the least
        difference between their values.

        Set I{logspace} C{True} to have the difference calculated in
        logspace, for a semilog plot.
        """
        Z = np.row_stack(Ys)
        if logspace: Z = np.log(Z)
        V = np.var(Z, axis=0)
        return np.argmin(V)

    def subplot(self, sp, X, aVals, bVals, semilog=False):
        """
        Given the subplotting tool I{sp} and the supplied 1-D Numpy array
        of I{X} values, plots the curves for each combination of I{a}
        in I{aVals} and I{b} in I{bVals}.

        Returns the value of I{X} where there is the least difference
        between the curves.
        """
        Ys = []
        for a, b in zip(aVals, bVals):
            Ys.append(self.func(X, a, b))
            sp.add_legend("a={:.2f}, b={:.2f}", a, b)
        k = self.leastDiff(Ys, semilog)
        sp.add_annotation(k, X[k])
        if semilog:
            sp.semilogy(X, *Ys)
        else:
            sp(X, *Ys)

    def plot(self, aVals, bVals):
        """
        Plots the curves for each combination of I{a} in I{aVals} and its
        corresponding I{b} in I{bVals}, from my I{xMin} to my I{xMax}
        and from zero to double my I{xMax}.
        """
        with self.pt as sp:
            # Top subplot: The range of interest
            X = np.linspace(self.xMin, self.xMax, self.N)
            self.subplot(sp, X, aVals, bVals)
            # Bottom subplot: Positive X surrounding the range of
            # interest
            X = np.linspace(0, 2 * self.xMax, self.N)
            sp.add_axvline(self.xMin)
            sp.add_axvline(self.xMax)
            self.subplot(sp, X, aVals, bVals, semilog=True)
        self.pt.show()