Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/animation/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def update_line(num, data, line):
line.set_data(data[..., :num])
return line,

###############################################################################

fig1 = plt.figure()

# Fixing random state for reproducibility
Expand All @@ -32,6 +34,8 @@ def update_line(num, data, line):

# To save the animation, use the command: line_ani.save('lines.mp4')

###############################################################################

fig2 = plt.figure()

x = np.arange(-9, 10)
Expand Down
7 changes: 5 additions & 2 deletions examples/api/filled_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ def stack_hist(ax, stacked_data, sty_cycle, bottoms=None,
stack_data = np.random.randn(4, 12250)
dict_data = OrderedDict(zip((c['label'] for c in label_cycle), stack_data))

# work with plain arrays
###############################################################################
# Work with plain arrays

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4.5), tight_layout=True)
arts = stack_hist(ax1, stack_data, color_cycle + label_cycle + hatch_cycle,
hist_func=hist_func)
Expand All @@ -204,7 +206,8 @@ def stack_hist(ax, stacked_data, sty_cycle, bottoms=None,
ax2.set_xlabel('counts')
ax2.set_ylabel('x')

# work with labeled data
###############################################################################
# Work with labeled data

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4.5),
tight_layout=True, sharey=True)
Expand Down
65 changes: 43 additions & 22 deletions examples/api/sankey_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,42 @@
from matplotlib.sankey import Sankey


###############################################################################
# Example 1 -- Mostly defaults
#
# This demonstrates how to create a simple diagram by implicitly calling the
# Sankey.add() method and by appending finish() to the call to the class.

Sankey(flows=[0.25, 0.15, 0.60, -0.20, -0.15, -0.05, -0.50, -0.10],
labels=['', '', '', 'First', 'Second', 'Third', 'Fourth', 'Fifth'],
orientations=[-1, 1, 0, 1, 1, 1, 0, -1]).finish()
plt.title("The default settings produce a diagram like this.")

###############################################################################
# Notice:
# 1. Axes weren't provided when Sankey() was instantiated, so they were
# created automatically.
# 2. The scale argument wasn't necessary since the data was already
# normalized.
# 3. By default, the lengths of the paths are justified.
#
# 1. Axes weren't provided when Sankey() was instantiated, so they were
# created automatically.
# 2. The scale argument wasn't necessary since the data was already
# normalized.
# 3. By default, the lengths of the paths are justified.


###############################################################################
# Example 2
#
# This demonstrates:
# 1. Setting one path longer than the others
# 2. Placing a label in the middle of the diagram
# 3. Using the scale argument to normalize the flows
# 4. Implicitly passing keyword arguments to PathPatch()
# 5. Changing the angle of the arrow heads
# 6. Changing the offset between the tips of the paths and their labels
# 7. Formatting the numbers in the path labels and the associated unit
# 8. Changing the appearance of the patch and the labels after the figure is
# created
#
# 1. Setting one path longer than the others
# 2. Placing a label in the middle of the diagram
# 3. Using the scale argument to normalize the flows
# 4. Implicitly passing keyword arguments to PathPatch()
# 5. Changing the angle of the arrow heads
# 6. Changing the offset between the tips of the paths and their labels
# 7. Formatting the numbers in the path labels and the associated unit
# 8. Changing the appearance of the patch and the labels after the figure is
# created

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
title="Flow Diagram of a Widget")
Expand All @@ -51,18 +62,26 @@
diagrams = sankey.finish()
diagrams[0].texts[-1].set_color('r')
diagrams[0].text.set_fontweight('bold')

###############################################################################
# Notice:
# 1. Since the sum of the flows is nonzero, the width of the trunk isn't
# uniform. If verbose.level is helpful (in matplotlibrc), a message is
# given in the terminal window.
# 2. The second flow doesn't appear because its value is zero. Again, if
# verbose.level is helpful, a message is given in the terminal window.
#
# 1. Since the sum of the flows is nonzero, the width of the trunk isn't
# uniform. If verbose.level is helpful (in matplotlibrc), a message is
# given in the terminal window.
# 2. The second flow doesn't appear because its value is zero. Again, if
# verbose.level is helpful, a message is given in the terminal window.


###############################################################################
# Example 3
#
# This demonstrates:
# 1. Connecting two systems
# 2. Turning off the labels of the quantities
# 3. Adding a legend
#
# 1. Connecting two systems
# 2. Turning off the labels of the quantities
# 3. Adding a legend

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
flows = [0.25, 0.15, 0.60, -0.10, -0.05, -0.25, -0.15, -0.10, -0.35]
Expand All @@ -74,6 +93,8 @@
diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='best')

###############################################################################
# Notice that only one connection is specified, but the systems form a
# circuit since: (1) the lengths of the paths are justified and (2) the
# orientation and ordering of the flows is mirrored.
Expand Down
4 changes: 4 additions & 0 deletions examples/event_handling/figure_axes_enter_leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def leave_figure(event):
event.canvas.figure.patch.set_facecolor('grey')
event.canvas.draw()

###############################################################################

fig1, (ax, ax2) = plt.subplots(2, 1)
fig1.suptitle('mouse hover over figure or axes to trigger events')

Expand All @@ -41,6 +43,8 @@ def leave_figure(event):
fig1.canvas.mpl_connect('axes_enter_event', enter_axes)
fig1.canvas.mpl_connect('axes_leave_event', leave_axes)

###############################################################################

fig2, (ax, ax2) = plt.subplots(2, 1)
fig2.suptitle('mouse hover over figure or axes to trigger events')

Expand Down
13 changes: 12 additions & 1 deletion examples/images_contours_and_fields/contour_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,43 @@
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)


###############################################################################
# Create a simple contour plot with labels using default colors. The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label

plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')


###############################################################################
# contour labels can be placed manually by providing list of positions
# (in data coordinate). See ginput_manual_clabel.py for interactive
# placement.

plt.figure()
CS = plt.contour(X, Y, Z)
manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)]
plt.clabel(CS, inline=1, fontsize=10, manual=manual_locations)
plt.title('labels at selected locations')


###############################################################################
# You can force all the contours to be the same color.

plt.figure()
CS = plt.contour(X, Y, Z, 6,
colors='k', # negative contours will be dashed by default
)
plt.clabel(CS, fontsize=9, inline=1)
plt.title('Single color - negative contours dashed')

###############################################################################
# You can set negative contours to be solid instead of dashed:

matplotlib.rcParams['contour.negative_linestyle'] = 'solid'
plt.figure()
CS = plt.contour(X, Y, Z, 6,
Expand All @@ -65,7 +72,9 @@
plt.title('Single color - negative contours solid')


###############################################################################
# And you can manually specify the colors of the contour

plt.figure()
CS = plt.contour(X, Y, Z, 6,
linewidths=np.arange(.5, 4, .5),
Expand All @@ -75,8 +84,10 @@
plt.title('Crazy lines')


###############################################################################
# Or you can use a colormap to specify the colors; the default
# colormap will be used for the contour lines

plt.figure()
im = plt.imshow(Z, interpolation='bilinear', origin='lower',
cmap=cm.gray, extent=(-3, 3, -2, 2))
Expand Down
13 changes: 8 additions & 5 deletions examples/images_contours_and_fields/contour_label_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

##################################################
###############################################################################
# Define our surface

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
Expand All @@ -29,9 +30,10 @@
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

##################################################
###############################################################################
# Make contour labels using creative float classes
# Follows suggestion of Manuel Metz

plt.figure()

# Basic contour plot
Expand Down Expand Up @@ -59,9 +61,9 @@ def __repr__(self):
fmt = '%r %%'
plt.clabel(CS, CS.levels, inline=True, fmt=fmt, fontsize=10)

##################################################
# Label contours with arbitrary strings using a
# dictionary
###############################################################################
# Label contours with arbitrary strings using a dictionary

plt.figure()

# Basic contour plot
Expand All @@ -75,6 +77,7 @@ def __repr__(self):
# Label every other level using strings
plt.clabel(CS, CS.levels[::2], inline=True, fmt=fmt, fontsize=10)

###############################################################################
# Use a Formatter

plt.figure()
Expand Down
14 changes: 5 additions & 9 deletions examples/images_contours_and_fields/contourf_hatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import numpy as np


# invent some numbers, turning the x and y arrays into simple
# 2d arrays, which make combining them together easier.
x = np.linspace(-3, 5, 150).reshape(1, -1)
Expand All @@ -18,22 +17,19 @@
# we no longer need x and y to be 2 dimensional, so flatten them.
x, y = x.flatten(), y.flatten()

###############################################################################
# Plot 1: the simplest hatched plot with a colorbar

# ---------------------------------------------
# | Plot #1 |
# ---------------------------------------------
# the simplest hatched plot with a colorbar
fig = plt.figure()
cs = plt.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
cmap=plt.get_cmap('gray'),
extend='both', alpha=0.5
)
plt.colorbar()

# ---------------------------------------------
# | Plot #2 |
# ---------------------------------------------
# a plot of hatches without color with a legend
###############################################################################
# Plot 2: a plot of hatches without color with a legend

plt.figure()
n_levels = 6
plt.contour(x, y, z, n_levels, colors='black', linestyles='-')
Expand Down
6 changes: 6 additions & 0 deletions examples/images_contours_and_fields/quiver_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
U = np.cos(X)
V = np.sin(Y)

###############################################################################

plt.figure()
plt.title('Arrows scale with plot width, not view')
Q = plt.quiver(X, Y, U, V, units='width')
qk = plt.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E',
coordinates='figure')

###############################################################################

plt.figure()
plt.title("pivot='mid'; every third arrow; units='inches'")
Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
Expand All @@ -30,6 +34,8 @@
coordinates='figure')
plt.scatter(X[::3, ::3], Y[::3, ::3], color='r', s=5)

###############################################################################

plt.figure()
plt.title("pivot='tip'; scales with x view")
M = np.hypot(U, V)
Expand Down
6 changes: 6 additions & 0 deletions examples/images_contours_and_fields/tricontour_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
import math

###############################################################################
# Creating a Triangulation without specifying the triangles results in the
# Delaunay triangulation of the points.

Expand All @@ -36,14 +37,17 @@
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)

###############################################################################
# pcolor plot.

plt.figure()
plt.gca().set_aspect('equal')
plt.tricontourf(triang, z)
plt.colorbar()
plt.tricontour(triang, z, colors='k')
plt.title('Contour plot of Delaunay triangulation')

###############################################################################
# You can specify your own triangulation rather than perform a Delaunay
# triangulation of the points, where each triangle is given by the indices of
# the three points that make up the triangle, ordered in either a clockwise or
Expand Down Expand Up @@ -93,10 +97,12 @@
[42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38],
[33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]])

###############################################################################
# Rather than create a Triangulation object, can simply pass x, y and triangles
# arrays to tripcolor directly. It would be better to use a Triangulation
# object if the same triangulation was to be used more than once to save
# duplicated calculations.

plt.figure()
plt.gca().set_aspect('equal')
plt.tricontourf(x, y, triangles, z)
Expand Down
Loading