From 69a634a791878cd69d43a4c2ee671b3f9ee623ae Mon Sep 17 00:00:00 2001 From: Naoya Kanai Date: Fri, 20 Jan 2017 11:41:24 -0800 Subject: [PATCH 1/3] MEP12ify pie and polar chart examples --- examples/pie_and_polar_charts/pie_demo_features.py | 4 ++++ examples/pie_and_polar_charts/polar_bar_demo.py | 5 +++++ examples/pie_and_polar_charts/polar_scatter_demo.py | 9 ++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/pie_and_polar_charts/pie_demo_features.py b/examples/pie_and_polar_charts/pie_demo_features.py index cfb6b124ba52..65b85b02320a 100644 --- a/examples/pie_and_polar_charts/pie_demo_features.py +++ b/examples/pie_and_polar_charts/pie_demo_features.py @@ -1,4 +1,8 @@ """ +=============== +Basic pie chart +=============== + Demo of a basic pie chart plus a few additional features. In addition to the basic pie chart, this demo shows a few optional features: diff --git a/examples/pie_and_polar_charts/polar_bar_demo.py b/examples/pie_and_polar_charts/polar_bar_demo.py index 4268d33d102a..cad68a7fe201 100644 --- a/examples/pie_and_polar_charts/polar_bar_demo.py +++ b/examples/pie_and_polar_charts/polar_bar_demo.py @@ -1,4 +1,8 @@ """ +======================= +Pie chart on polar axis +======================= + Demo of bar plot on a polar axis. """ import numpy as np @@ -7,6 +11,7 @@ # Fixing random state for reproducibility np.random.seed(19680801) +# Compute pie slices N = 20 theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) radii = 10 * np.random.rand(N) diff --git a/examples/pie_and_polar_charts/polar_scatter_demo.py b/examples/pie_and_polar_charts/polar_scatter_demo.py index 41f8033e33e9..d05299e8616a 100644 --- a/examples/pie_and_polar_charts/polar_scatter_demo.py +++ b/examples/pie_and_polar_charts/polar_scatter_demo.py @@ -1,4 +1,8 @@ """ +========================== +Scatter plot on polar axis +========================== + Demo of scatter plot on a polar axis. Size increases radially in this example and color increases with angle @@ -10,7 +14,7 @@ # Fixing random state for reproducibility np.random.seed(19680801) - +# Compute areas and colors N = 150 r = 2 * np.random.rand(N) theta = 2 * np.pi * np.random.rand(N) @@ -18,7 +22,6 @@ colors = theta ax = plt.subplot(111, projection='polar') -c = ax.scatter(theta, r, c=colors, s=area, cmap=plt.cm.hsv) -c.set_alpha(0.75) +c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75) plt.show() From ab29f84771543b6639bdaf4ad493a509558289d3 Mon Sep 17 00:00:00 2001 From: Naoya Kanai Date: Fri, 20 Jan 2017 12:35:49 -0800 Subject: [PATCH 2/3] MEP12ify color examples --- examples/color/color_cycle_default.py | 6 ++++- examples/color/color_cycle_demo.py | 16 +++++++---- examples/color/colormaps_reference.py | 11 +++++--- examples/color/named_colors.py | 27 +++++++------------ .../pie_and_polar_charts/polar_bar_demo.py | 1 + .../polar_scatter_demo.py | 1 + 6 files changed, 36 insertions(+), 26 deletions(-) diff --git a/examples/color/color_cycle_default.py b/examples/color/color_cycle_default.py index dff1b4ada0a2..29afdef912d2 100644 --- a/examples/color/color_cycle_default.py +++ b/examples/color/color_cycle_default.py @@ -1,10 +1,14 @@ """ +==================================== +Colors in the default property cycle +==================================== + Display the colors from the default prop_cycle. """ - import numpy as np import matplotlib.pyplot as plt + prop_cycle = plt.rcParams['axes.prop_cycle'] colors = prop_cycle.by_key()['color'] diff --git a/examples/color/color_cycle_demo.py b/examples/color/color_cycle_demo.py index 84203febc10d..cec1b278c886 100644 --- a/examples/color/color_cycle_demo.py +++ b/examples/color/color_cycle_demo.py @@ -1,23 +1,28 @@ """ -Demo of custom property-cycle settings to control colors and such -for multi-line plots. +=================== +Styling with cycler +=================== + +Demo of custom property-cycle settings to control colors and other style +properties for multi-line plots. This example demonstrates two different APIs: - 1. Setting the default rc-parameter specifying the property cycle. + 1. Setting the default rc parameter specifying the property cycle. This affects all subsequent axes (but not axes already created). - 2. Setting the property cycle for a specific axes. This only - affects a single axes. + 2. Setting the property cycle for a single pair of axes. """ from cycler import cycler import numpy as np import matplotlib.pyplot as plt + x = np.linspace(0, 2 * np.pi) offsets = np.linspace(0, 2*np.pi, 4, endpoint=False) # Create array with shifted-sine curve along each column yy = np.transpose([np.sin(x + phi) for phi in offsets]) +# 1. Setting prop cycle on default rc parameter plt.rc('lines', linewidth=4) plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) + cycler('linestyle', ['-', '--', ':', '-.']))) @@ -25,6 +30,7 @@ ax0.plot(yy) ax0.set_title('Set default color cycle to rgby') +# 2. Define prop cycle for single set of axes ax1.set_prop_cycle(cycler('color', ['c', 'm', 'y', 'k']) + cycler('lw', [1, 2, 3, 4])) ax1.plot(yy) diff --git a/examples/color/colormaps_reference.py b/examples/color/colormaps_reference.py index 9f2c87e4a661..7b3611a3ff9e 100644 --- a/examples/color/colormaps_reference.py +++ b/examples/color/colormaps_reference.py @@ -1,4 +1,8 @@ """ +================== +Colormap reference +================== + Reference for colormaps included with Matplotlib. This reference example shows all colormaps included with Matplotlib. Note that @@ -35,9 +39,9 @@ import numpy as np import matplotlib.pyplot as plt + # Have colormaps separated into categories: # http://matplotlib.org/examples/color/colormaps_reference.html - cmaps = [('Perceptually Uniform Sequential', ['viridis', 'inferno', 'plasma', 'magma']), ('Sequential', ['Blues', 'BuGn', 'BuPu', @@ -65,7 +69,7 @@ gradient = np.vstack((gradient, gradient)) -def plot_color_gradients(cmap_category, cmap_list): +def plot_color_gradients(cmap_category, cmap_list, nrows): fig, axes = plt.subplots(nrows=nrows) fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99) axes[0].set_title(cmap_category + ' colormaps', fontsize=14) @@ -81,7 +85,8 @@ def plot_color_gradients(cmap_category, cmap_list): for ax in axes: ax.set_axis_off() + for cmap_category, cmap_list in cmaps: - plot_color_gradients(cmap_category, cmap_list) + plot_color_gradients(cmap_category, cmap_list, nrows) plt.show() diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index 2280e6ea18bb..5fcf95974d1c 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -1,44 +1,37 @@ """ -Visualization of named colors. +======================== +Visualizing named colors +======================== Simple plot example with the named colors and its visual representation. """ +from __future__ import division -from __future__ import (absolute_import, division, print_function, - unicode_literals) - -import six - -import numpy as np import matplotlib.pyplot as plt from matplotlib import colors as mcolors colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) -# Sort by hue, saturation, value and name. +# Sort colors by hue, saturation, value and name. by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) for name, color in colors.items()) - -# Get the sorted color names. sorted_names = [name for hsv, name in by_hsv] n = len(sorted_names) ncols = 4 -nrows = int(np.ceil(1. * n / ncols)) +nrows = n // ncols + 1 fig, ax = plt.subplots(figsize=(8, 5)) +# Get height and width X, Y = fig.get_dpi() * fig.get_size_inches() - -# row height h = Y / (nrows + 1) -# col width w = X / ncols for i, name in enumerate(sorted_names): col = i % ncols - row = int(i / ncols) + row = i // ncols y = Y - (row * h) - h xi_line = w * (col + 0.05) @@ -49,8 +42,8 @@ horizontalalignment='left', verticalalignment='center') - ax.hlines( - y + h * 0.1, xi_line, xf_line, color=colors[name], linewidth=(h * 0.6)) + ax.hlines(y + h * 0.1, xi_line, xf_line, + color=colors[name], linewidth=(h * 0.6)) ax.set_xlim(0, X) ax.set_ylim(0, Y) diff --git a/examples/pie_and_polar_charts/polar_bar_demo.py b/examples/pie_and_polar_charts/polar_bar_demo.py index cad68a7fe201..38a557ab4553 100644 --- a/examples/pie_and_polar_charts/polar_bar_demo.py +++ b/examples/pie_and_polar_charts/polar_bar_demo.py @@ -8,6 +8,7 @@ import numpy as np import matplotlib.pyplot as plt + # Fixing random state for reproducibility np.random.seed(19680801) diff --git a/examples/pie_and_polar_charts/polar_scatter_demo.py b/examples/pie_and_polar_charts/polar_scatter_demo.py index d05299e8616a..5c9ca2c35f1c 100644 --- a/examples/pie_and_polar_charts/polar_scatter_demo.py +++ b/examples/pie_and_polar_charts/polar_scatter_demo.py @@ -11,6 +11,7 @@ import numpy as np import matplotlib.pyplot as plt + # Fixing random state for reproducibility np.random.seed(19680801) From eecad0e931e4524603b40f94757ea589af19d277 Mon Sep 17 00:00:00 2001 From: Naoya Kanai Date: Fri, 20 Jan 2017 20:38:30 -0800 Subject: [PATCH 3/3] fix style sheet examples --- examples/style_sheets/plot_bmh.py | 1 - examples/style_sheets/plot_fivethirtyeight.py | 17 +++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/style_sheets/plot_bmh.py b/examples/style_sheets/plot_bmh.py index c37f4dbfa534..097893607c1b 100644 --- a/examples/style_sheets/plot_bmh.py +++ b/examples/style_sheets/plot_bmh.py @@ -18,7 +18,6 @@ def plot_beta_hist(ax, a, b): ax.hist(beta(a, b, size=10000), histtype="stepfilled", bins=25, alpha=0.8, normed=True) - return ax fig, ax = plt.subplots() diff --git a/examples/style_sheets/plot_fivethirtyeight.py b/examples/style_sheets/plot_fivethirtyeight.py index 79fb5cd84876..13abea16f184 100644 --- a/examples/style_sheets/plot_fivethirtyeight.py +++ b/examples/style_sheets/plot_fivethirtyeight.py @@ -11,6 +11,8 @@ from matplotlib import pyplot as plt import numpy as np +plt.style.use('fivethirtyeight') + x = np.linspace(0, 10) # Fixing random state for reproducibility @@ -18,14 +20,13 @@ fig, ax = plt.subplots() -with plt.style.context('fivethirtyeight'): - ax.plot(x, np.sin(x) + x + np.random.randn(50)) - ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50)) - ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50)) - ax.plot(x, np.sin(x) - 0.5 * x + np.random.randn(50)) - ax.plot(x, np.sin(x) - 2 * x + np.random.randn(50)) - ax.plot(x, np.sin(x) + np.random.randn(50)) - ax.set_title("'fivethirtyeight' style sheet") +ax.plot(x, np.sin(x) + x + np.random.randn(50)) +ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50)) +ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50)) +ax.plot(x, np.sin(x) - 0.5 * x + np.random.randn(50)) +ax.plot(x, np.sin(x) - 2 * x + np.random.randn(50)) +ax.plot(x, np.sin(x) + np.random.randn(50)) +ax.set_title("'fivethirtyeight' style sheet") plt.show()