示例#1
0
def tornado(ax):
    y = np.arange(8)
    x1 = y + np.random.random(8) + 1
    x2 = y + 3 * np.random.random(8) + 1
    ax.barh(y, x1, color='lightblue')
    ax.barh(y, -x2, color='salmon')
    ax.margins(0.15)
    example_utils.label(ax, 'barh(x, y)')
示例#2
0
def basic_bar(ax):
    y = [1, 3, 4, 5.5, 3, 2]
    err = [0.2, 1, 2.5, 1, 1, 0.5]
    x = np.arange(len(y))
    ax.bar(x, y, yerr=err, color='lightblue', ecolor='black')
    ax.margins(0.05)
    ax.set_ylim(bottom=0)
    example_utils.label(ax, 'bar(x, y, yerr=e)')
示例#3
0
def general(ax):
    num = 10
    left = np.random.randint(0, 10, num)
    bottom = np.random.randint(0, 10, num)
    width = np.random.random(num) + 0.5
    height = np.random.random(num) + 0.5
    ax.bar(left, height, width, bottom, color='salmon')
    ax.margins(0.15)
    example_utils.label(ax, 'bar(l, h, w, b)')
示例#4
0
def hist(ax, dists, colors):
    # We could call "ax.hist(dists, ...)" and skip the loop, but we'll plot
    # each distribution separately so they'll overlap and turn on transparency
    ax.set_color_cycle(colors)
    for dist in dists:
        ax.hist(dist, bins=20, density=True, edgecolor='none', alpha=0.5)

    ax.margins(y=0.05)
    ax.set_ylim(bottom=0)

    example_utils.label(ax, 'ax.hist(dists)')
示例#5
0
def violinplot(ax, dists, colors):
    result = ax.violinplot(dists, vert=False, showmedians=True)
    for body, color in zip(result['bodies'], colors):
        body.set(facecolor=color, alpha=0.5)
    for item in ['cbars', 'cmaxes', 'cmins', 'cmedians']:
        plt.setp(result[item], edgecolor='gray', linewidth=1.5)
    plt.setp(result['cmedians'], edgecolor='black')

    ax.margins(0.05)
    ax.set(ylim=[0, 6])

    example_utils.label(ax, 'ax.violinplot(dists)')
示例#6
0
def boxplot(ax, dists, colors):
    result = ax.boxplot(dists, patch_artist=True, notch=True, vert=False)

    for box, color in zip(result['boxes'], colors):
        box.set(facecolor=color, alpha=0.5)
    for item in ['whiskers', 'caps', 'medians']:
        plt.setp(result[item], color='gray', linewidth=1.5)
    plt.setp(result['fliers'], markeredgecolor='gray', markeredgewidth=1.5)
    plt.setp(result['medians'], color='black')

    ax.margins(0.05)
    ax.set(yticks=[], ylim=[0, 6])

    example_utils.label(ax, 'ax.boxplot(dists)')
def fill_between_example(ax):
    # Fill between fills between two curves or a curve and a constant value
    # It can be used in several ways. We'll illustrate a few below.
    x, y1, y2 = sin_data()

    # The most basic (and common) use of fill_between
    err = np.random.rand(x.size)**2 + 0.1
    y = 0.7 * x + 2
    ax.fill_between(x, y + err, y - err, color='orange')

    # Filling between two curves with different colors when they cross in
    # different directions
    ax.fill_between(x, y1, y2, where=y1>y2, color='lightblue')
    ax.fill_between(x, y1, y2, where=y1<y2, color='forestgreen')

    # Note that this is fillbetween*x*!
    ax.fill_betweenx(x, -y1, where=y1>0, color='red', alpha=0.5)
    ax.fill_betweenx(x, -y1, where=y1<0, color='blue', alpha=0.5)

    ax.margins(0.15)
    example_utils.label(ax, 'fill_between/x')
示例#8
0
def fill_between_example(ax):
    # Fill between fills between two curves or a curve and a constant value
    # It can be used in several ways. We'll illustrate a few below.
    x, y1, y2 = sin_data()

    # The most basic (and common) use of fill_between
    err = np.random.rand(x.size)**2 + 0.1
    y = 0.7 * x + 2
    ax.fill_between(x, y + err, y - err, color='orange')

    # Filling between two curves with different colors when they cross in
    # different directions
    ax.fill_between(x, y1, y2, where=y1 > y2, color='lightblue')
    ax.fill_between(x, y1, y2, where=y1 < y2, color='forestgreen')

    # Note that this is fillbetween*x*!
    ax.fill_betweenx(x, -y1, where=y1 > 0, color='red', alpha=0.5)
    ax.fill_betweenx(x, -y1, where=y1 < 0, color='blue', alpha=0.5)

    ax.margins(0.15)
    example_utils.label(ax, 'fill_between/x')
def fill_example(ax):
    # Use fill when you want a simple filled polygon between vertices
    x, y = fill_data()
    ax.fill(x, y, color='lightblue')
    ax.margins(0.1)
    example_utils.label(ax, 'fill')
示例#10
0
# Set up our data...
z = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))
ny, nx = z.shape
y, x = np.mgrid[:ny, :nx]
y = (y - y.mean()) * (x + 10)**2

mask = (z > -0.1) & (z < 0.1)
z2 = np.ma.masked_where(mask, z)

fig, axes = example_utils.setup_axes()

# Either pcolor or pcolormesh would produce the same result here.
# pcolormesh is faster, however.
axes[0].pcolor(x, y, z, cmap='gist_earth')
example_utils.label(axes[0], 'either')

# The difference between the two will become clear as we turn on edges:

# pcolor will completely avoid drawing masked cells...
axes[1].pcolor(x, y, z2, cmap='gist_earth', edgecolor='black')
example_utils.label(axes[1], 'pcolor(x,y,z)')

# While pcolormesh will draw them as empty (but still present) cells.
axes[2].pcolormesh(x, y, z2, cmap='gist_earth', edgecolor='black', lw=0.5,
                   antialiased=True)
example_utils.label(axes[2], 'pcolormesh(x,y,z)')

example_utils.title(fig, 'pcolor/pcolormesh: Colormapped 2D arrays')
fig.savefig('pcolor_example.png', facecolor='none')
示例#11
0
import matplotlib.pyplot as plt
import numpy as np
import example_utils
# 随机生成数据
np.random.seed(1874)
x, y, z = np.random.normal(0, 1, (3, 100))
t = np.arctan2(y, x)
size = 50 * np.cos(2 * t)**2 + 10

fig, axes = example_utils.setup_axes()

# 子图1
axes[0].scatter(x, y, marker='o',  color='darkblue', facecolor='white', s=80)
example_utils.label(axes[0], 'scatter(x, y)')

# 子图2
axes[1].scatter(x, y, marker='s', color='darkblue', s=size)
example_utils.label(axes[1], 'scatter(x, y, s)')

# 子图3
axes[2].scatter(x, y, s=size, c=z,  cmap='gist_ncar')
example_utils.label(axes[2], 'scatter(x, y, s, c)')

# example_utils.title(fig, '"ax.scatter(...)": Colored/scaled markers',
#                     y=0.95)
fig.savefig('scatter_example.png', facecolor='none')

plt.show()
# Set up our data...
z = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))
ny, nx = z.shape
y, x = np.mgrid[:ny, :nx]
y = (y - y.mean()) * (x + 10)**2

mask = (z > -0.1) & (z < 0.1)
z2 = np.ma.masked_where(mask, z)

fig, axes = example_utils.setup_axes()

# Either pcolor or pcolormesh would produce the same result here.
# pcolormesh is faster, however.
axes[0].pcolor(x, y, z, cmap='gist_earth')
example_utils.label(axes[0], 'either')

# The difference between the two will become clear as we turn on edges:

# pcolor will completely avoid drawing masked cells...
axes[1].pcolor(x, y, z2, cmap='gist_earth', edgecolor='black')
example_utils.label(axes[1], 'pcolor(x,y,z)')

# While pcolormesh will draw them as empty (but still present) cells.
axes[2].pcolormesh(x, y, z2, cmap='gist_earth', edgecolor='black', lw=0.5,
                   antialiased=True)
example_utils.label(axes[2], 'pcolormesh(x,y,z)')

example_utils.title(fig, 'pcolor/pcolormesh: Colormapped 2D arrays')
fig.savefig('pcolor_example.png', facecolor='none')
示例#13
0
# Generate data
n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
xi, yi = np.meshgrid(x, y)
z = (1 - xi / 2 + xi**5 + yi**3) * np.exp(-xi**2 - yi**2)
dy, dx = np.gradient(z)
mag = np.hypot(dx, dy)

fig, axes = example_utils.setup_axes()

# Use ax.arrow to plot a single arrow on the axes.
axes[0].arrow(0, 0, -0.5, 0.5, width=0.005, color='black')
axes[0].axis([-1, 1, -1, 1])
example_utils.label(axes[0], 'arrow(x, y, dx, dy)')

# Plot a regularly-sampled vector field with ax.quiver
ds = np.s_[::16, ::16] # Downsample our array a bit...
axes[1].quiver(xi[ds], yi[ds], dx[ds], dy[ds], z[ds], cmap='gist_earth',
               width=0.01, scale=0.25, pivot='middle')
axes[1].axis('tight')
example_utils.label(axes[1], 'quiver(x, y, dx, dy)')

# Use ax.streamplot to show flowlines through our vector field
# We'll get fancy and vary their width and color
lw = 2 * (mag - mag.min()) / mag.ptp() + 0.2
axes[2].streamplot(xi, yi, dx, dy, color=z, density=1.5, linewidth=lw,
                   cmap='gist_earth')
example_utils.label(axes[2], 'streamplot(x, y, dx, dy)')
示例#14
0
import matplotlib
matplotlib.use('nbAgg')

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.cbook import get_sample_data

import example_utils

z = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))

fig, axes = example_utils.setup_axes()

axes[0].contour(z, cmap='gist_earth')
example_utils.label(axes[0], 'contour')

axes[1].contourf(z, cmap='gist_earth')
example_utils.label(axes[1], 'contourf')

axes[2].contourf(z, cmap='gist_earth')
cont = axes[2].contour(z, colors='black')
axes[2].clabel(cont, fontsize=6)
example_utils.label(axes[2], 'contourf + contour\n + clabel')

example_utils.title(fig, '"contour, contourf, clabel": Contour/label 2D data',
                    y=0.96)
fig.savefig('contour_example.png', facecolor='none')

plt.show()
def stackplot_example(ax):
    # Stackplot is equivalent to a series of ax.fill_between calls
    x, y = stackplot_data()
    ax.stackplot(x, y.cumsum(axis=0), alpha=0.5)
    example_utils.label(ax, 'stackplot')
示例#16
0
def stackplot_example(ax):
    # Stackplot is equivalent to a series of ax.fill_between calls
    x, y = stackplot_data()
    ax.stackplot(x, y.cumsum(axis=0), alpha=0.5)
    example_utils.label(ax, 'stackplot')
"""
Illustrates the basics of using "scatter".
"""
import numpy as np
import matplotlib.pyplot as plt

import example_utils

# Generate some random data...
np.random.seed(1874)
x, y, z = np.random.normal(0, 1, (3, 100))
t = np.arctan2(y, x)
size = 50 * np.cos(2 * t)**2 + 10

fig, axes = example_utils.setup_axes()

axes[0].scatter(x, y, marker='o', facecolor='white', s=80)
example_utils.label(axes[0], 'scatter(x, y)')

axes[1].scatter(x, y, s=size, marker='s', color='darkblue')
example_utils.label(axes[1], 'scatter(x, y, s)')

axes[2].scatter(x, y, c=z, s=size, cmap='gist_ncar')
example_utils.label(axes[2], 'scatter(x, y, s, c)')

example_utils.title(fig,'"ax.scatter(...)": Colored/scaled markers',
                    y=0.95)
fig.savefig('scatter_example.png', facecolor='none')

plt.show()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.cbook import get_sample_data

import example_utils

z = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))

fig, axes = example_utils.setup_axes()

axes[0].contour(z, cmap='gist_earth')
example_utils.label(axes[0], 'contour')

axes[1].contourf(z, cmap='gist_earth')
example_utils.label(axes[1], 'contourf')

axes[2].contourf(z, cmap='gist_earth')
cont = axes[2].contour(z, colors='black')
axes[2].clabel(cont, fontsize=6)
example_utils.label(axes[2], 'contourf + contour\n + clabel')

example_utils.title(fig, '"contour, contourf, clabel": Contour/label 2D data',
                    y=0.96)
fig.savefig('contour_example.png', facecolor='none')

plt.show()
# Generate data
n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
xi, yi = np.meshgrid(x, y)
z = (1 - xi / 2 + xi**5 + yi**3) * np.exp(-xi**2 - yi**2)
dy, dx = np.gradient(z)
mag = np.hypot(dx, dy)

fig, axes = example_utils.setup_axes()

# Use ax.arrow to plot a single arrow on the axes.
axes[0].arrow(0, 0, -0.5, 0.5, width=0.005, color='black')
axes[0].axis([-1, 1, -1, 1])
example_utils.label(axes[0], 'arrow(x, y, dx, dy)')

# Plot a regularly-sampled vector field with ax.quiver
ds = np.s_[::16, ::16] # Downsample our array a bit...
axes[1].quiver(xi[ds], yi[ds], dx[ds], dy[ds], z[ds], cmap='gist_earth',
               width=0.01, scale=0.25, pivot='middle')
axes[1].axis('tight')
example_utils.label(axes[1], 'quiver(x, y, dx, dy)')

# Use ax.streamplot to show flowlines through our vector field
# We'll get fancy and vary their width and color
lw = 2 * (mag - mag.min()) / mag.ptp() + 0.2
axes[2].streamplot(xi, yi, dx, dy, color=z, density=1.5, linewidth=lw,
                   cmap='gist_earth')
example_utils.label(axes[2], 'streamplot(x, y, dx, dy)')
示例#20
0
def fill_example(ax):
    # Use fill when you want a simple filled polygon between vertices
    x, y = fill_data()
    ax.fill(x, y, color='lightblue')
    ax.margins(0.1)
    example_utils.label(ax, 'fill')