示例#1
0
import matplotlib.pyplot as plt

from mpltools import style
from mpltools import layout


def color_cycle_example(ax):
    L = 6
    x = np.linspace(0, L)
    ncolors = len(plt.rcParams['axes.color_cycle'])
    shift = np.linspace(0, L, ncolors, endpoint=False)
    for s in shift:
        ax.plot(x, np.sin(x + s), 'o-')


def image_and_patch_example(ax):
    ax.imshow(np.random.random(size=(20, 20)), interpolation='none')
    c = plt.Circle((5, 5), radius=5, label='patch')
    ax.add_patch(c)


style.use('grayscale')

figsize = layout.figaspect(0.5)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=figsize)

color_cycle_example(ax1)
image_and_patch_example(ax2)

plt.show()
示例#2
0
.. [1] http://www.huyng.com/posts/sane-color-scheme-for-matplotlib/

.. _ggplot: http://had.co.nz/ggplot/
.. _R: http://www.r-project.org/

"""
import numpy as np
import matplotlib.pyplot as plt

from mpltools import style
from mpltools import layout

style.use('ggplot')

figsize = layout.figaspect(scale=1.2)
fig, axes = plt.subplots(ncols=2, nrows=2, figsize=figsize)
ax1, ax2, ax3, ax4 = axes.ravel()

# scatter plot (Note: `plt.scatter` doesn't use default colors)
x, y = np.random.normal(size=(2, 200))
ax1.plot(x, y, 'o')

# sinusoidal lines with colors from default color cycle
L = 2 * np.pi
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.color_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)
for s in shift:
    ax2.plot(x, np.sin(x + s), '-')
ax2.margins(0)
示例#3
0
class Draw(object):
    def __init__(self):
        return

    fig = plt.figure(figsize=layout.figaspect(1))
    ax = fig.add_subplot(111, projection='3d')

    def plot_earth(self, radius):
        '''This function plots a celestial body at the origin of the plot.
        :param radius: Takes the radius of a celestial body
        :returns: angular coordinates
        '''

        coefs = (1, 1, 1)
        rx, ry, rz = [radius / np.sqrt(coef) for coef in coefs]

        # Azimuth Angle & Altitude in Spherical Coordinates
        phi = np.linspace(0, 2 * np.pi, 100)
        theta = np.linspace(0, np.pi, 100)

        # Spherical Angles
        x = rx * np.outer(np.cos(phi), np.sin(theta))
        y = ry * np.outer(np.sin(phi), np.sin(theta))
        z = rz * np.outer(np.ones_like(phi), np.cos(theta))
        return x, y, z

    def orientation(self,
                    inclination=0,
                    right_ascension=0,
                    argument_periapsis=0):
        '''This function defines the rotational matricies used to orient the ellipse.'''
        i = Orbital().degree_to_radian(inclination)
        R = np.matrix([[1, 0, 0], [0, np.cos(i), -np.sin(i)],
                       [0, np.sin(i), np.cos(i)]])

        w_omega = Orbital().degree_to_radian(right_ascension)
        R2 = np.matrix([[np.cos(w_omega), -np.sin(w_omega), 0],
                        [np.sin(w_omega), np.cos(w_omega), 0], [0, 0, 1]])

        omega = Orbital().degree_to_radian(argument_periapsis)
        R3 = np.matrix([[np.cos(omega), -np.sin(omega), 0],
                        [np.sin(omega), np.cos(omega), 0], [0, 0, 1]])
        return R, R2, R3

    def polar_equation_of_ellipse(self, semi_major_axis, eccentricity, theta):
        '''This function defines the polar equation of an ellipse, and returns the radius, and polar coordinates.'''
        r = (semi_major_axis *
             (1 - eccentricity**2)) / (1 + eccentricity * np.cos(theta))
        polar_x = r * np.cos(theta)
        polar_y = r * np.sin(theta)
        polar_z = 0 * theta
        return r, polar_x, polar_y, polar_z

    def define_orbit(self,
                     semi_major_axis=0,
                     eccentricity=0,
                     inclination=0,
                     right_ascension=0,
                     argument_periapsis=0,
                     theta=0,
                     define_orbit=True):
        '''This function takes the orbital elements and uses them to define the Elliptical orbit in 3-Dimensions'''
        R, R2, R3 = self.orientation(inclination, right_ascension,
                                     argument_periapsis)
        r, polar_x, polar_y, polar_z = self.polar_equation_of_ellipse(
            semi_major_axis, eccentricity, theta)
        if define_orbit is True:
            points = np.matrix(list(zip(polar_x, polar_y, polar_z)))
        else:
            points = np.matrix([polar_x, polar_y, polar_z])
        pts = (R * R2 * R3 * points.T)
        return pts

    def plot_orbit(self,
                   semi_major_axis=0,
                   eccentricity=0,
                   inclination=0,
                   right_ascension=0,
                   argument_periapsis=0,
                   true_anomaly=0,
                   label=None,
                   object=None):
        "Draws orbit around an earth in units of kilometers."

        #Plot Earth
        x, y, z = self.plot_earth(radius=a.objects[str(object)]['radius'])
        self.ax.plot_surface(x,
                             y,
                             z,
                             rstride=4,
                             cstride=4,
                             alpha=0.4,
                             color=a.objects[str(object)]['color'])
        self.ax.set_axis_off()

        #Plot Orbit
        theta = np.linspace(0, 2 * np.pi, 360)
        pts = self.define_orbit(semi_major_axis, eccentricity, inclination,
                                right_ascension, argument_periapsis, theta)
        orbit_pts = pts.T
        xr, yr, zr = orbit_pts[:, 0].A.flatten(), orbit_pts[:, 1].A.flatten(
        ), orbit_pts[:, 2].A.flatten()
        self.ax.plot(xr, yr, zr, color='g', linestyle='-')

        # Plot Satellite
        sat_angle = Orbital().degree_to_radian(true_anomaly)
        sat = self.define_orbit(semi_major_axis,
                                eccentricity,
                                inclination,
                                right_ascension,
                                argument_periapsis,
                                sat_angle,
                                define_orbit=False)
        sat = sat.flatten()
        satx, saty, satz = sat[0, 0], sat[0, 1], sat[0, 2]
        self.ax.plot([0, satx], [0, saty], [0, satz], 'b-')
        self.ax.plot([satx], [saty], [satz], 'bo')

        #Create X-axis Marker
        self.ax.plot([0, 7500], [0, 0], [0, 0], 'r:')
        self.ax.plot([7500], [0], [0], 'r<')
        self.ax.text(7510, 0, 0, s='X', fontsize=10, color='w')

        #Create Y-axis Marker
        self.ax.plot([0, 0], [0, 7500], [0, 0], 'r:')
        self.ax.plot([0], [7500], [0], 'r<')
        self.ax.text(0, 7510, 0, s='Y', fontsize=10, color='w')

        #Create Z-axis Marker
        self.ax.plot([0, 0], [0, 0], [0, 7500], 'r:')
        self.ax.plot([0], [0], [7500], 'r^')
        self.ax.text(0, 0, 7510, s='Z', fontsize=10, color='w')

        #Create Z-axis Marker
        self.ax.plot([0, 0], [0, 0], [0, 7500], 'm-')
        self.ax.plot([0], [0], [7500], 'm<')
        self.ax.text(0, 0, 7510, s='axis', fontsize=10, color='w')

        # Write satellite name next to it
        if label is not None:
            self.ax.text(satx, saty, satz, label, fontsize=11)

        #radius = np.sqrt(satx**2 + saty**2 + satz**2)
        #polar = np.arccos(satz/radius)
        #lon = o.degree_to_radian(polar-90)
        #lat = o.degree_to_radian(np.arctan2(saty, satx))

        #Lat = o.radian_to_degree(lat)
        #Lon = o.radian_to_degree(lon)
        #print("----------------------------------------------------------------------------------------")
        #print("{} : Projected Lat: {}° Long: {}°".format(label, Lat, Lon))

    def draw_orbit(self, *argv, object):
        '''This function calls the plot orbit function using the TLE elements defined in orbit.py'''
        o = Orbital()
        semi_major_axes = []
        for arg in argv:
            o.import_tle(tle=arg)
            semi_major_axis = o.semi_major_axis_calc()
            semi_major_axes.append(semi_major_axis)
            true_anomaly = o.anomoly_calc()
            self.plot_orbit(semi_major_axis,
                            o.eccentricity,
                            o.inclination,
                            o.right_ascension,
                            o.argument_periapsis,
                            true_anomaly,
                            o.title,
                            object=object)
        max_axis = max(semi_major_axes)
        self.ax.auto_scale_xyz([-max_axis, max_axis], [-max_axis, max_axis],
                               [-max_axis, max_axis])
        plt.show()
示例#4
0
===========================

``cycle_cmap`` provides a simple way to set the color cycle to evenly-spaced
intervals of a given colormap. By default, it alters the default color cycle,
but if you pass it a plot axes, only the color cycle for the axes is altered.

"""
import numpy as np
import matplotlib.pyplot as plt

from mpltools import layout
from mpltools import color

n_lines = 10

# Change default color cycle for all new axes
color.cycle_cmap(n_lines)

figsize = layout.figaspect(aspect_ratio=0.5)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=figsize)

# Change color cycle specifically for `ax2`
color.cycle_cmap(n_lines, cmap='pink', ax=ax2)

x = np.linspace(0, 10)
for shift in np.linspace(0, np.pi, n_lines):
    ax1.plot(x, np.sin(x - shift), linewidth=2)
    ax2.plot(x, np.sin(x - shift), linewidth=2)

plt.show()
示例#5
0
"""
Simple demo of a scatter plot.
"""
import numpy as np
import matplotlib.pyplot as plt
from mpltools import style
from mpltools import layout
import brewer2mpl

colors = brewer2mpl.get_map('Set2', 'qualitative', 3).mpl_colors
style.use('ggplot')

alpha = 0.9

figsize = layout.figaspect(scale=0.8)
fig, axes = plt.subplots(figsize=figsize)

N = 50
data1 = [np.random.rand(N), np.random.rand(N)]
data2 = [np.random.rand(N), np.random.rand(N)]
data3 = [np.random.rand(N), np.random.rand(N)]
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radiuses

plt.scatter(data1[0], data1[1], s=area, alpha=alpha, color=colors[0])
plt.scatter(data2[0], data2[1], s=area, alpha=alpha, color=colors[1])
plt.scatter(data3[0], data3[1], s=area, alpha=alpha, color=colors[2])
plt.title('Tutorial Matplotlib')

fig.tight_layout()

plt.show()