diff --git a/examples/lines_bars_and_markers/line_styles_reference.py b/examples/lines_bars_and_markers/line_styles_reference.py deleted file mode 100644 index 58c72c6ff6fe..000000000000 --- a/examples/lines_bars_and_markers/line_styles_reference.py +++ /dev/null @@ -1,24 +0,0 @@ -""" -==================== -Line-style reference -==================== - -Reference for line-styles included with Matplotlib. -""" - -import matplotlib.pyplot as plt - - -# Plot all line styles. -fig, ax = plt.subplots() - -linestyles = ['-', '--', '-.', ':'] -for y, linestyle in enumerate(linestyles): - ax.text(-0.1, y, repr(linestyle), - horizontalalignment='center', verticalalignment='center') - ax.plot([y, y], linestyle=linestyle, linewidth=3, color='tab:blue') - -ax.set_axis_off() -ax.set_title('line styles') - -plt.show() diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index e62ceab6f7cb..50b2e6fcd7f4 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -3,50 +3,70 @@ Linestyles ========== -This examples showcases different linestyles copying those of Tikz/PGF. +Simple linestyles can be defined using the strings "solid", "dotted", "dashed" +or "dashdot". More refined control can be achieved by providing a dash tuple +``(offset, (on_off_seq))``. For example, ``(0, (3, 10, 1, 15))`` means +(3pt line, 10pt space, 1pt line, 15pt space) with no offset. See also +`.Line2D.set_linestyle`. + +*Note*: The dash style can also be configured via `.Line2D.set_dashes` +as shown in :doc:`/gallery/lines_bars_and_markers/line_demo_dash_control` +and passing a list of dash sequences using the keyword *dashes* to the +cycler in :doc:`property_cycle `. """ import numpy as np import matplotlib.pyplot as plt -from collections import OrderedDict from matplotlib.transforms import blended_transform_factory -linestyles = OrderedDict( - [('solid', (0, ())), - ('loosely dotted', (0, (1, 10))), - ('dotted', (0, (1, 5))), - ('densely dotted', (0, (1, 1))), +linestyle_str = [ + ('solid', 'solid'), # Same as (0, ()) or '-' + ('dotted', 'dotted'), # Same as (0, (1, 1)) or '.' + ('dashed', 'dashed'), # Same as '--' + ('dashdot', 'dashdot')] # Same as '-.' - ('loosely dashed', (0, (5, 10))), - ('dashed', (0, (5, 5))), - ('densely dashed', (0, (5, 1))), +linestyle_tuple = [ + ('loosely dotted', (0, (1, 10))), + ('dotted', (0, (1, 1))), + ('densely dotted', (0, (1, 1))), - ('loosely dashdotted', (0, (3, 10, 1, 10))), - ('dashdotted', (0, (3, 5, 1, 5))), - ('densely dashdotted', (0, (3, 1, 1, 1))), + ('loosely dashed', (0, (5, 10))), + ('dashed', (0, (5, 5))), + ('densely dashed', (0, (5, 1))), + + ('loosely dashdotted', (0, (3, 10, 1, 10))), + ('dashdotted', (0, (3, 5, 1, 5))), + ('densely dashdotted', (0, (3, 1, 1, 1))), - ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))), - ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]) + ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), + ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))] + + +def plot_linestyles(ax, linestyles): + X, Y = np.linspace(0, 100, 10), np.zeros(10) + yticklabels = [] + + for i, (name, linestyle) in enumerate(linestyles): + ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black') + yticklabels.append(name) + ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5), + yticks=np.arange(len(linestyles)), yticklabels=yticklabels) -plt.figure(figsize=(10, 6)) -ax = plt.subplot(1, 1, 1) + # For each line style, add a text annotation with a small offset from + # the reference point (0 in Axes coords, y tick value in Data coords). + reference_transform = blended_transform_factory(ax.transAxes, ax.transData) + for i, (name, linestyle) in enumerate(linestyles): + ax.annotate(repr(linestyle), xy=(0.0, i), xycoords=reference_transform, + xytext=(-6, -12), textcoords='offset points', color="blue", + fontsize=8, ha="right", family="monospace") -X, Y = np.linspace(0, 100, 10), np.zeros(10) -for i, (name, linestyle) in enumerate(linestyles.items()): - ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black') -ax.set_ylim(-0.5, len(linestyles)-0.5) -plt.yticks(np.arange(len(linestyles)), linestyles.keys()) -plt.xticks([]) +fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 3]}, + figsize=(10, 8)) -# For each line style, add a text annotation with a small offset from -# the reference point (0 in Axes coords, y tick value in Data coords). -reference_transform = blended_transform_factory(ax.transAxes, ax.transData) -for i, (name, linestyle) in enumerate(linestyles.items()): - ax.annotate(str(linestyle), xy=(0.0, i), xycoords=reference_transform, - xytext=(-6, -12), textcoords='offset points', color="blue", - fontsize=8, ha="right", family="monospace") +plot_linestyles(ax0, linestyle_str[::-1]) +plot_linestyles(ax1, linestyle_tuple[::-1]) plt.tight_layout() plt.show()