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 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')
+
+#-- Data generation ----------------------
+
+def stackplot_data():
+ x = np.linspace(0, 10, 100)
+ y = np.random.normal(0, 1, (5, 100))
+ y = y.cumsum(axis=1)
+ y -= y.min(axis=0, keepdims=True)
+ return x, y
+
+def sin_data():
+ x = np.linspace(0, 10, 100)
+ y = np.sin(x)
+ y2 = np.cos(x)
+ return x, y, y2
+
+def fill_data():
+ t = np.linspace(0, 2*np.pi, 100)
+ r = np.random.normal(0, 1, 100).cumsum()
+ r -= r.min()
+ return r * np.cos(t), r * np.sin(t)
+
+main()
diff --git a/examples/imshow_example.py b/examples/imshow_example.py
new file mode 100644
index 0000000..87f5561
--- /dev/null
+++ b/examples/imshow_example.py
@@ -0,0 +1,45 @@
+import matplotlib.pyplot as plt
+import numpy as np
+from matplotlib.cbook import get_sample_data
+from mpl_toolkits import axes_grid1
+
+import example_utils
+
+def main():
+ fig, axes = setup_axes()
+ plot(axes, *load_data())
+ example_utils.title(fig, '"ax.imshow(data, ...)": Colormapped or RGB arrays')
+ fig.savefig('imshow_example.png', facecolor='none')
+ plt.show()
+
+def plot(axes, img_data, scalar_data, ny):
+ # Note: I'm defining the extent so I can cheat a bit when using ImageGrid
+ # to make all of the axes the same height...
+
+ # Default: Linear interpolation
+ axes[0].imshow(scalar_data, cmap='gist_earth', extent=[0, ny, ny, 0])
+
+ # Use nearest interpolation instead.
+ axes[1].imshow(scalar_data, cmap='gist_earth', interpolation='nearest',
+ extent=[0, ny, ny, 0])
+
+ # Show RGB/RGBA data instead of colormapping a scalar.
+ axes[2].imshow(img_data)
+
+def load_data():
+ img_data = plt.imread(get_sample_data('grace_hopper.png'))
+ ny, nx, nbands = img_data.shape
+ scalar_data = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))
+ return img_data, scalar_data, ny
+
+def setup_axes():
+ # We'll set up the axes a bit differently here so that they'll all be the
+ # same height even though the aspect will be set and adjustable is "box".
+ fig = plt.figure(figsize=(6,3))
+ axes = axes_grid1.ImageGrid(fig, [0, 0, .93, 1], (1, 3), axes_pad=0)
+
+ for ax in axes:
+ ax.set(xticks=[], yticks=[])
+ return fig, axes
+
+main()
diff --git a/examples/pcolor_example.py b/examples/pcolor_example.py
new file mode 100644
index 0000000..85468b2
--- /dev/null
+++ b/examples/pcolor_example.py
@@ -0,0 +1,42 @@
+"""
+Shows the basics of pcolor/pcolormesh.
+One note: Use imshow if your data is on a rectangular grid, as it's much
+faster. This example shows a case that imshow can't handle.
+"""
+import matplotlib.pyplot as plt
+import numpy as np
+from matplotlib.cbook import get_sample_data
+
+import example_utils
+
+# 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')
+
+plt.show()
diff --git a/examples/plot_example.py b/examples/plot_example.py
new file mode 100644
index 0000000..3a56ef3
--- /dev/null
+++ b/examples/plot_example.py
@@ -0,0 +1,27 @@
+import numpy as np
+import matplotlib.pyplot as plt
+
+import example_utils
+
+x = np.linspace(0, 10, 100)
+
+fig, axes = example_utils.setup_axes()
+for ax in axes:
+ ax.margins(y=0.10)
+
+# Default plotting, colors will be determined by the axes' color_cycle
+for i in range(1, 6):
+ axes[0].plot(x, i * x)
+
+# Demonstrating different linestyles
+for i, ls in enumerate(['-', '--', ':', '-.']):
+ axes[1].plot(x, np.cos(x) + i, linestyle=ls)
+
+# Using linestyles and markers
+for i, (ls, mk) in enumerate(zip(['', '-', ':'], ['o', '^', 's'])):
+ axes[2].plot(x, np.cos(x) + i * x, linestyle=ls, marker=mk, markevery=10)
+
+example_utils.title(fig, '"ax.plot(x, y, ...)": Lines and/or markers', y=0.95)
+fig.savefig('plot_example.png', facecolor='none')
+
+plt.show()
diff --git a/examples/scatter_example.py b/examples/scatter_example.py
new file mode 100644
index 0000000..33a4848
--- /dev/null
+++ b/examples/scatter_example.py
@@ -0,0 +1,30 @@
+"""
+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()
diff --git a/examples/statistical_example.py b/examples/statistical_example.py
new file mode 100644
index 0000000..c98add8
--- /dev/null
+++ b/examples/statistical_example.py
@@ -0,0 +1,77 @@
+"""
+Matplotlib has a handful of specalized statistical plotting methods.
+
+For many statistical plots, you may find that a specalized statistical plotting
+package such as Seaborn (which uses matplotlib behind-the-scenes) is a better
+fit to your needs.
+"""
+import numpy as np
+import matplotlib.pyplot as plt
+
+import example_utils
+
+def main():
+ colors = ['cyan', 'red', 'blue', 'green', 'purple']
+ dists = generate_data()
+
+ fig, axes = example_utils.setup_axes()
+ hist(axes[0], dists, colors)
+ boxplot(axes[1], dists, colors)
+ violinplot(axes[2], dists, colors)
+
+ example_utils.title(fig, 'hist/boxplot/violinplot: Statistical plotting',
+ y=0.9)
+ fig.savefig('statistical_example.png', facecolor='none')
+
+ plt.show()
+
+def generate_data():
+ means = [0, -1, 2.5, 4.3, -3.6]
+ sigmas = [1.2, 5, 3, 1.5, 2]
+ # Each distribution has a different number of samples.
+ nums = [150, 1000, 100, 200, 500]
+
+ dists = [np.random.normal(*args) for args in zip(means, sigmas, nums)]
+ return dists
+
+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)')
+
+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 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)')
+
+main()
diff --git a/examples/vector_example.py b/examples/vector_example.py
new file mode 100644
index 0000000..05c3d53
--- /dev/null
+++ b/examples/vector_example.py
@@ -0,0 +1,39 @@
+import matplotlib.pyplot as plt
+import numpy as np
+
+import example_utils
+
+# 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)')
+
+example_utils.title(fig, '"arrow/quiver/streamplot": Vector fields', y=0.96)
+fig.savefig('vector_example.png', facecolor='none')
+
+plt.show()
diff --git a/exercises/1.1-limits.py b/exercises/1.1-limits.py
deleted file mode 100644
index 73d5015..0000000
--- a/exercises/1.1-limits.py
+++ /dev/null
@@ -1,3 +0,0 @@
-fig, ax = plt.subplots(1, 1)
-ax.set_ylim( )
-plt.show()
diff --git a/exercises/1.1-subplots_and_basic_plotting.py b/exercises/1.1-subplots_and_basic_plotting.py
new file mode 100644
index 0000000..1ff79ca
--- /dev/null
+++ b/exercises/1.1-subplots_and_basic_plotting.py
@@ -0,0 +1,11 @@
+import numpy as np
+import matplotlib.pyplot as plt
+
+# Try to reproduce the figure shown in images/exercise_1-1.png
+
+# Our data...
+x = np.linspace(0, 10, 100)
+y1, y2, y3 = np.cos(x), np.cos(x + 1), np.cos(x + 2)
+names = ['Signal 1', 'Signal 2', 'Signal 3']
+
+# Can you figure out what to do next to plot x vs y1, y2, and y3 on one figure?
diff --git a/exercises/1.2-spines.py b/exercises/1.2-spines.py
deleted file mode 100644
index 6abdb53..0000000
--- a/exercises/1.2-spines.py
+++ /dev/null
@@ -1,23 +0,0 @@
-fig, ax = plt.subplots(1, 1)
-ax.plot([-2, 2, 3, 4], [-10, 20, 25, 5])
-ax.spines['top'].set_visible(False)
-ax.xaxis.set_ticks_position('bottom') # no ticklines at the top
-ax.spines['right'].set_visible(False)
-ax.yaxis.set_ticks_position('left') # no ticklines on the right
-
-# "outward"
-# Move the two remaining spines "out" away from the plot by 10 points
-ax.spines['bottom'].set_position(('outward', 10))
-ax.spines['left'].set_position(('outward', 10))
-
-# "data"
-# Have the spines stay intersected at (0,0)
-#ax.spines['bottom'].set_position(('data', 0))
-#ax.spines['left'].set_position(('data', 0))
-
-# "axes"
-# Have the two remaining spines placed at a fraction of the axes
-#ax.spines['bottom'].set_position(('axes', 0.75))
-#ax.spines['left'].set_position(('axes', 0.25))
-
-plt.show()
diff --git a/exercises/2.1-bar_and_fill_between.py b/exercises/2.1-bar_and_fill_between.py
new file mode 100644
index 0000000..5ca04cd
--- /dev/null
+++ b/exercises/2.1-bar_and_fill_between.py
@@ -0,0 +1,24 @@
+import numpy as np
+import matplotlib.pyplot as plt
+np.random.seed(1)
+
+# Generate data...
+y_raw = np.random.randn(1000).cumsum() + 15
+x_raw = np.linspace(0, 24, y_raw.size)
+
+# Get averages of every 100 samples...
+x_pos = x_raw.reshape(-1, 100).min(axis=1)
+y_avg = y_raw.reshape(-1, 100).mean(axis=1)
+y_err = y_raw.reshape(-1, 100).ptp(axis=1)
+
+bar_width = x_pos[1] - x_pos[0]
+
+# Make a made up future prediction with a fake confidence
+x_pred = np.linspace(0, 30)
+y_max_pred = y_avg[0] + y_err[0] + 2.3 * x_pred
+y_min_pred = y_avg[0] - y_err[0] + 1.2 * x_pred
+
+# Just so you don't have to guess at the colors...
+barcolor, linecolor, fillcolor = 'wheat', 'salmon', 'lightblue'
+
+# Now you're on your own!
diff --git a/exercises/2.2-vmin_vmax_imshow_and_colorbars.py b/exercises/2.2-vmin_vmax_imshow_and_colorbars.py
new file mode 100644
index 0000000..4922fb3
--- /dev/null
+++ b/exercises/2.2-vmin_vmax_imshow_and_colorbars.py
@@ -0,0 +1,15 @@
+import numpy as np
+import matplotlib.pyplot as plt
+np.random.seed(1)
+
+# Generate random data with different ranges...
+data1 = np.random.random((10, 10))
+data2 = 2 * np.random.random((10, 10))
+data3 = 3 * np.random.random((10, 10))
+
+# Set up our figure and axes...
+fig, axes = plt.subplots(ncols=3, figsize=plt.figaspect(0.5))
+fig.tight_layout() # Make the subplots fill up the figure a bit more...
+cax = fig.add_axes([0.25, 0.1, 0.55, 0.03]) # Add an axes for the colorbar
+
+# Now you're on your own!
diff --git a/exercises/2.1-colors.py b/exercises/3.1-colors.py
similarity index 100%
rename from exercises/2.1-colors.py
rename to exercises/3.1-colors.py
diff --git a/exercises/2.2-markers.py b/exercises/3.2-markers.py
similarity index 100%
rename from exercises/2.2-markers.py
rename to exercises/3.2-markers.py
diff --git a/exercises/2.3-properties.py b/exercises/3.3-properties.py
similarity index 100%
rename from exercises/2.3-properties.py
rename to exercises/3.3-properties.py
diff --git a/exercises/2.4-arrows.py b/exercises/3.4-arrows.py
similarity index 100%
rename from exercises/2.4-arrows.py
rename to exercises/3.4-arrows.py
diff --git a/exercises/4.1-legends_and_scaling.py b/exercises/4.1-legends_and_scaling.py
new file mode 100644
index 0000000..3f96e87
--- /dev/null
+++ b/exercises/4.1-legends_and_scaling.py
@@ -0,0 +1,17 @@
+import numpy as np
+import matplotlib.pyplot as plt
+
+plt.style.use('classic')
+
+# Try to reproduce the figure shown in images/exercise_4-1.png
+# Here's the data and colors used.
+
+t = np.linspace(0, 2 * np.pi, 150)
+x1, y1 = np.cos(t), np.sin(t)
+x2, y2 = 2 * x1, 2 * y1
+
+colors = ['darkred', 'darkgreen']
+
+# Try to plot the two circles, scale the axes as shown and add a legend
+# Hint: it's easiest to combine `ax.axis(...)` and `ax.margins(...)` to scale
+# the axes
diff --git a/exercises/4.2-spines_ticks_and_subplot_spacing.py b/exercises/4.2-spines_ticks_and_subplot_spacing.py
new file mode 100644
index 0000000..2f607c0
--- /dev/null
+++ b/exercises/4.2-spines_ticks_and_subplot_spacing.py
@@ -0,0 +1,9 @@
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Try to reproduce the figure shown in images/exercise_4.2.png
+# This one is a bit trickier!
+
+# Here's the data...
+data = [('dogs', 4, 4), ('frogs', -3, 1), ('cats', 1, 5), ('goldfish', -2, 2)]
+animals, friendliness, popularity = zip(*data)
diff --git a/exercises/3.1-goldstar.py b/exercises/5.1-goldstar.py
similarity index 100%
rename from exercises/3.1-goldstar.py
rename to exercises/5.1-goldstar.py
diff --git a/images/bar_example.png b/images/bar_example.png
new file mode 100644
index 0000000..8aebe55
Binary files /dev/null and b/images/bar_example.png differ
diff --git a/images/contour_example.png b/images/contour_example.png
new file mode 100644
index 0000000..7e0ead7
Binary files /dev/null and b/images/contour_example.png differ
diff --git a/images/exercise_1-1.png b/images/exercise_1-1.png
new file mode 100644
index 0000000..b64ffea
Binary files /dev/null and b/images/exercise_1-1.png differ
diff --git a/images/exercise_2.1-bar_and_fill_between.png b/images/exercise_2.1-bar_and_fill_between.png
new file mode 100644
index 0000000..37ef247
Binary files /dev/null and b/images/exercise_2.1-bar_and_fill_between.png differ
diff --git a/images/exercise_2.2-vmin_vmax_imshow_and_colorbars.png b/images/exercise_2.2-vmin_vmax_imshow_and_colorbars.png
new file mode 100644
index 0000000..45db4d9
Binary files /dev/null and b/images/exercise_2.2-vmin_vmax_imshow_and_colorbars.png differ
diff --git a/images/exercise_4-1.png b/images/exercise_4-1.png
new file mode 100644
index 0000000..a981f2c
Binary files /dev/null and b/images/exercise_4-1.png differ
diff --git a/images/exercise_4-2.png b/images/exercise_4-2.png
new file mode 100644
index 0000000..e5fda91
Binary files /dev/null and b/images/exercise_4-2.png differ
diff --git a/images/figure_axes_axis_labeled.png b/images/figure_axes_axis_labeled.png
new file mode 100644
index 0000000..fc3a2f1
Binary files /dev/null and b/images/figure_axes_axis_labeled.png differ
diff --git a/images/fill_example.png b/images/fill_example.png
new file mode 100644
index 0000000..1d60c0c
Binary files /dev/null and b/images/fill_example.png differ
diff --git a/images/imshow_example.png b/images/imshow_example.png
new file mode 100644
index 0000000..6306f36
Binary files /dev/null and b/images/imshow_example.png differ
diff --git a/images/pcolor_example.png b/images/pcolor_example.png
new file mode 100644
index 0000000..ab3a8f5
Binary files /dev/null and b/images/pcolor_example.png differ
diff --git a/images/plot_example.png b/images/plot_example.png
new file mode 100644
index 0000000..afbc85e
Binary files /dev/null and b/images/plot_example.png differ
diff --git a/images/scatter_example.png b/images/scatter_example.png
new file mode 100644
index 0000000..1f38270
Binary files /dev/null and b/images/scatter_example.png differ
diff --git a/images/statistical_example.png b/images/statistical_example.png
new file mode 100644
index 0000000..cc398d6
Binary files /dev/null and b/images/statistical_example.png differ
diff --git a/images/vector_example.png b/images/vector_example.png
new file mode 100644
index 0000000..3b99d73
Binary files /dev/null and b/images/vector_example.png differ
diff --git a/solutions/1.1-limits.py b/solutions/1.1-limits.py
deleted file mode 100644
index 37bf892..0000000
--- a/solutions/1.1-limits.py
+++ /dev/null
@@ -1,3 +0,0 @@
-fig, ax = plt.subplots(1, 1)
-ax.set_ylim(1000, 500)
-plt.show()
diff --git a/solutions/1.1-subplots_and_basic_plotting.py b/solutions/1.1-subplots_and_basic_plotting.py
new file mode 100644
index 0000000..4a641d6
--- /dev/null
+++ b/solutions/1.1-subplots_and_basic_plotting.py
@@ -0,0 +1,14 @@
+import numpy as np
+import matplotlib.pyplot as plt
+
+x = np.linspace(0, 10, 100)
+y1, y2, y3 = np.cos(x), np.cos(x + 1), np.cos(x + 2)
+names = ['Signal 1', 'Signal 2', 'Signal 3']
+
+fig, axes = plt.subplots(nrows=3)
+
+for ax, y, name in zip(axes, [y1, y2, y3], names):
+ ax.plot(x, y, color='black')
+ ax.set(xticks=[], yticks=[], title=name)
+
+plt.show()
diff --git a/solutions/1.2-spines.py b/solutions/1.2-spines.py
deleted file mode 100644
index ae58e32..0000000
--- a/solutions/1.2-spines.py
+++ /dev/null
@@ -1,12 +0,0 @@
-fig, ax = plt.subplots(1, 1)
-ax.plot([-2, 2, 3, 4], [-10, 20, 25, 5])
-
-ax.spines['top'].set_position('center')
-ax.spines['right'].set_position('center')
-ax.tick_params(axis='both', direction='inout', length=10)
-
-# Move the two remaining spines "out" away from the plot by 10 points
-ax.spines['bottom'].set_position(('outward', 10))
-ax.spines['left'].set_position(('outward', 10))
-
-plt.show()
diff --git a/solutions/2.1-bar_and_fill_between.py b/solutions/2.1-bar_and_fill_between.py
new file mode 100644
index 0000000..125de02
--- /dev/null
+++ b/solutions/2.1-bar_and_fill_between.py
@@ -0,0 +1,37 @@
+import numpy as np
+import matplotlib.pyplot as plt
+np.random.seed(1)
+
+# Generate data...
+y_raw = np.random.randn(1000).cumsum() + 15
+x_raw = np.linspace(0, 24, y_raw.size)
+
+# Get averages of every 100 samples...
+x_pos = x_raw.reshape(-1, 100).min(axis=1)
+y_avg = y_raw.reshape(-1, 100).mean(axis=1)
+y_err = y_raw.reshape(-1, 100).ptp(axis=1)
+
+bar_width = x_pos[1] - x_pos[0]
+
+# Make a made up future prediction with a fake confidence
+x_pred = np.linspace(0, 30)
+y_max_pred = y_avg[0] + y_err[0] + 2.3 * x_pred
+y_min_pred = y_avg[0] - y_err[0] + 1.2 * x_pred
+
+# Just so you don't have to guess at the colors...
+barcolor, linecolor, fillcolor = 'wheat', 'salmon', 'lightblue'
+
+# Now you're on your own!
+
+fig, ax = plt.subplots()
+
+ax.plot(x_raw, y_raw, color=linecolor)
+ax.bar(x_pos, y_avg, width=bar_width, color=barcolor, yerr=y_err,
+ ecolor='gray', edgecolor='gray')
+ax.fill_between(x_pred, y_min_pred, y_max_pred, color=fillcolor)
+
+ax.set(title='Future Projection of Attitudes',
+ ylabel='Snarkiness (snark units)',
+ xlabel='Minutes since class began')
+
+plt.show()
diff --git a/solutions/2.2-vmin_vmax_imshow_and_colorbars.py b/solutions/2.2-vmin_vmax_imshow_and_colorbars.py
new file mode 100644
index 0000000..208e6ea
--- /dev/null
+++ b/solutions/2.2-vmin_vmax_imshow_and_colorbars.py
@@ -0,0 +1,22 @@
+import numpy as np
+import matplotlib.pyplot as plt
+np.random.seed(1)
+
+
+# Generate random data with different ranges...
+data1 = np.random.random((10, 10))
+data2 = 2 * np.random.random((10, 10))
+data3 = 3 * np.random.random((10, 10))
+
+# Set up our figure and axes...
+fig, axes = plt.subplots(ncols=3, figsize=plt.figaspect(0.5))
+fig.tight_layout() # Make the subplots fill up the figure a bit more...
+cax = fig.add_axes([0.25, 0.1, 0.55, 0.03]) # Add an axes for the colorbar
+
+# Now you're on your own!
+for ax, data in zip(axes, [data1, data2, data3]):
+ # Display data, explicitly making the colormap cover values from 0 to 3
+ im = ax.imshow(data, vmin=0, vmax=3, interpolation='nearest')
+
+fig.colorbar(im, cax=cax, orientation='horizontal')
+plt.show()
diff --git a/solutions/2.1-colors.py b/solutions/3.1-colors.py
similarity index 100%
rename from solutions/2.1-colors.py
rename to solutions/3.1-colors.py
diff --git a/solutions/2.2-markers.py b/solutions/3.2-markers.py
similarity index 100%
rename from solutions/2.2-markers.py
rename to solutions/3.2-markers.py
diff --git a/solutions/2.3-properties.py b/solutions/3.3-properties.py
similarity index 64%
rename from solutions/2.3-properties.py
rename to solutions/3.3-properties.py
index 340f787..7e0c9eb 100644
--- a/solutions/2.3-properties.py
+++ b/solutions/3.3-properties.py
@@ -1,4 +1,4 @@
t = np.arange(0.0, 5.0, 0.1)
a = np.exp(-t) * np.cos(2*np.pi*t)
-plt.plot(t, a, 'r:', marker='D', mfc='y')
+plt.plot(t, a, 'r:D', mfc='y', mec='g')
plt.show()
diff --git a/solutions/2.4-arrows.py b/solutions/3.4-arrows.py
similarity index 100%
rename from solutions/2.4-arrows.py
rename to solutions/3.4-arrows.py
diff --git a/solutions/4.1-legends_and_scaling.py b/solutions/4.1-legends_and_scaling.py
new file mode 100644
index 0000000..10e53b3
--- /dev/null
+++ b/solutions/4.1-legends_and_scaling.py
@@ -0,0 +1,20 @@
+import numpy as np
+import matplotlib.pyplot as plt
+
+plt.style.use('classic')
+
+t = np.linspace(0, 2 * np.pi, 150)
+x1, y1 = np.cos(t), np.sin(t)
+x2, y2 = 2 * x1, 2 * y1
+
+colors = ['darkred', 'darkgreen']
+
+fig, ax = plt.subplots()
+ax.plot(x1, y1, color=colors[0], label='Inner', linewidth=3)
+ax.plot(x2, y2, color=colors[1], label='Outer', linewidth=3)
+ax.legend()
+
+ax.axis('equal')
+ax.margins(0.05)
+
+plt.show()
diff --git a/solutions/4.2-spines_ticks_and_subplot_spacing.py b/solutions/4.2-spines_ticks_and_subplot_spacing.py
new file mode 100644
index 0000000..088d47b
--- /dev/null
+++ b/solutions/4.2-spines_ticks_and_subplot_spacing.py
@@ -0,0 +1,25 @@
+import matplotlib.pyplot as plt
+import numpy as np
+
+data = [('dogs', 4, 4), ('frogs', -3, 1), ('cats', 1, 5), ('goldfish', -2, 2)]
+animals, friendliness, popularity = zip(*data)
+
+
+def plot_and_setup_spines(ax, animals, y, ylabel):
+ x = np.arange(len(animals))
+ ax.bar(x, y, align='center', color='gray')
+ ax.set(xticks=x, xticklabels=animals, ylabel=ylabel)
+
+ ax.xaxis.set_ticks_position('bottom')
+ ax.yaxis.set_ticks_position('left')
+ ax.spines['bottom'].set_position(('data', 0))
+ ax.tick_params(axis='x', direction='inout', length=8)
+ ax.margins(0.05)
+
+fig, axes = plt.subplots(nrows=2)
+fig.subplots_adjust(hspace=0.0)
+
+plot_and_setup_spines(axes[0], animals, friendliness, 'Friendliness')
+plot_and_setup_spines(axes[1], animals, popularity, 'Popularity')
+
+plt.show()
diff --git a/solutions/3.1-goldstar.py b/solutions/5.1-goldstar.py
similarity index 70%
rename from solutions/3.1-goldstar.py
rename to solutions/5.1-goldstar.py
index 4057d05..7a04d3e 100644
--- a/solutions/3.1-goldstar.py
+++ b/solutions/5.1-goldstar.py
@@ -3,11 +3,12 @@
fig, ax = plt.subplots(1, 1)
-offsets = zip([0.2, 0.4, 0.6, 0.8], [0.5] * 4)
+offsets = list(zip([0.2, 0.4, 0.6, 0.8], [0.5] * 4))
collection = StarPolygonCollection(5,
offsets=offsets,
transOffset=ax.transData,
facecolors=['gold'],
- sizes=[75])
+ sizes=[175],
+ edgecolors=['k'])
ax.add_collection(collection)
plt.show()
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy