From 0880c16dc401a0a1ef75a42548dd5559ab0bb32a Mon Sep 17 00:00:00 2001 From: thoo Date: Sun, 21 Oct 2018 17:49:32 -0400 Subject: [PATCH 1/9] combine two examples into one --- .../line_styles_reference.py | 24 -------------- examples/lines_bars_and_markers/linestyles.py | 31 +++++++++++++------ 2 files changed, 22 insertions(+), 33 deletions(-) delete mode 100644 examples/lines_bars_and_markers/line_styles_reference.py 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..ea5fe3670db1 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -3,31 +3,44 @@ Linestyles ========== -This examples showcases different linestyles copying those of Tikz/PGF. +This example showcases different linestyles copying those of Tikz/PGF. +Linestyle can be provided as simple as *solid* , *dotted* or *dashed*. +Moreover, the dashing of the line can be controlled by a dash tuple +such as (offset, (on_off_seq)) as mentioned in `.Line2D.set_linestyle`. +For e.g., ``(0, (3, 10, 1, 15))`` means 3pt-line,10pt-space,1pt-line,15pt-space +with no offset. + +*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, ())), +linestyles = [ + ('solid', ('-')), # Same as (0, ()) + ('dotted', (':')), # Same as (0, (1, 1)) + ('dashed', ('--')), + ('dashdot', ('-.')), + ('loosely dotted', (0, (1, 10))), - ('dotted', (0, (1, 5))), ('densely dotted', (0, (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))), + ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))), ('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)))]) + ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))] +# Reverse the list so that plots and linestyles are in same order. +linestyles = OrderedDict(linestyles[::-1]) plt.figure(figsize=(10, 6)) ax = plt.subplot(1, 1, 1) @@ -44,7 +57,7 @@ # 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, + 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") From 236eb2378c0ee6c0d38a00f1dffa0c31164894df Mon Sep 17 00:00:00 2001 From: thoo Date: Mon, 22 Oct 2018 19:11:40 -0400 Subject: [PATCH 2/9] Made changes as recommended --- examples/lines_bars_and_markers/linestyles.py | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index ea5fe3670db1..217aa9314226 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -3,12 +3,11 @@ Linestyles ========== -This example showcases different linestyles copying those of Tikz/PGF. -Linestyle can be provided as simple as *solid* , *dotted* or *dashed*. -Moreover, the dashing of the line can be controlled by a dash tuple -such as (offset, (on_off_seq)) as mentioned in `.Line2D.set_linestyle`. -For e.g., ``(0, (3, 10, 1, 15))`` means 3pt-line,10pt-space,1pt-line,15pt-space -with no offset. +Linestyle can be provided as simple as *solid* , *dotted*, *dashed* +or *dashdot*. Moreover, the dashing of the line can be controlled by +a dash tuple such as (offset, (on_off_seq)) as mentioned in +`.Line2D.set_linestyle`. For e.g., ``(0, (3, 10, 1, 15))`` means + 3pt-line,10pt-space,1pt-line,15pt-space with no offset. *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` @@ -21,37 +20,34 @@ from matplotlib.transforms import blended_transform_factory linestyles = [ - ('solid', ('-')), # Same as (0, ()) - ('dotted', (':')), # Same as (0, (1, 1)) - ('dashed', ('--')), - ('dashdot', ('-.')), + ('solid', ('solid')), # Same as (0, ()) or '-' + ('dotted', ('dotted')), # Same as (0, (1, 1)) or '.' + ('dashed', ('dashed')), # Same as '--' + ('dashdot', ('dashdot')), # Same as '-.' - ('loosely dotted', (0, (1, 10))), - ('densely dotted', (0, (1, 1))), + ('loosely dotted', (0, (1, 10))), + ('densely dotted', (0, (1, 1))), - ('loosely dashed', (0, (5, 10))), - ('densely dashed', (0, (5, 1))), + ('loosely dashed', (0, (5, 10))), + ('densely dashed', (0, (5, 1))), - ('loosely dashdotted', (0, (3, 10, 1, 10))), - ('densely dashdotted', (0, (3, 1, 1, 1))), + ('loosely dashdotted', (0, (3, 10, 1, 10))), + ('densely dashdotted', (0, (3, 1, 1, 1))), - ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))), + ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))), ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))] # Reverse the list so that plots and linestyles are in same order. linestyles = OrderedDict(linestyles[::-1]) - -plt.figure(figsize=(10, 6)) -ax = plt.subplot(1, 1, 1) - X, Y = np.linspace(0, 100, 10), np.zeros(10) + +fig, ax = plt.subplots(figsize=(10, 6)) 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([]) +ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5), + yticks=np.arange(len(linestyles)), yticklabels=linestyles.keys()) # 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). From bcc63a40334f9dbb6c88428747971c9f72d3c8cb Mon Sep 17 00:00:00 2001 From: thoo Date: Wed, 24 Oct 2018 16:34:54 -0400 Subject: [PATCH 3/9] Split into two subplots --- examples/lines_bars_and_markers/linestyles.py | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index 217aa9314226..5473307c8fc4 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -19,12 +19,14 @@ from collections import OrderedDict from matplotlib.transforms import blended_transform_factory -linestyles = [ + +linestyle_str = [ ('solid', ('solid')), # Same as (0, ()) or '-' ('dotted', ('dotted')), # Same as (0, (1, 1)) or '.' ('dashed', ('dashed')), # Same as '--' - ('dashdot', ('dashdot')), # Same as '-.' + ('dashdot', ('dashdot'))] # Same as '-.' +linestyle_tuple = [ ('loosely dotted', (0, (1, 10))), ('densely dotted', (0, (1, 1))), @@ -38,24 +40,31 @@ ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))] -# Reverse the list so that plots and linestyles are in same order. -linestyles = OrderedDict(linestyles[::-1]) -X, Y = np.linspace(0, 100, 10), np.zeros(10) -fig, ax = plt.subplots(figsize=(10, 6)) -for i, (name, linestyle) in enumerate(linestyles.items()): - ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black') +def simple_plot(ax, linestyles): + 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) -ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5), - yticks=np.arange(len(linestyles)), yticklabels=linestyles.keys()) + # 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) +fig, ax = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 2]}, + figsize=(10, 6)) -# 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(repr(linestyle), xy=(0.0, i), xycoords=reference_transform, - xytext=(-6, -12), textcoords='offset points', color="blue", - fontsize=8, ha="right", family="monospace") +simple_plot(ax[0], linestyle_str[::-1]) +simple_plot(ax[1], linestyle_tuple[::-1]) plt.tight_layout() plt.show() From adb9733479cc8afa81d6523fc8ab75865852bbe4 Mon Sep 17 00:00:00 2001 From: thoo Date: Wed, 24 Oct 2018 17:26:36 -0400 Subject: [PATCH 4/9] remove OrderedDict --- examples/lines_bars_and_markers/linestyles.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index 5473307c8fc4..299a3c815edf 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -16,7 +16,6 @@ """ import numpy as np import matplotlib.pyplot as plt -from collections import OrderedDict from matplotlib.transforms import blended_transform_factory From 01792dcd2a6ccb67c7bee4ffcc81c7d46cc335eb Mon Sep 17 00:00:00 2001 From: thoo Date: Wed, 24 Oct 2018 17:31:24 -0400 Subject: [PATCH 5/9] remove blank lines --- examples/lines_bars_and_markers/linestyles.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index 299a3c815edf..9950708117df 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -18,7 +18,6 @@ import matplotlib.pyplot as plt from matplotlib.transforms import blended_transform_factory - linestyle_str = [ ('solid', ('solid')), # Same as (0, ()) or '-' ('dotted', ('dotted')), # Same as (0, (1, 1)) or '.' @@ -57,7 +56,6 @@ def simple_plot(ax, linestyles): xytext=(-6, -12), textcoords='offset points', color="blue", fontsize=8, ha="right", family="monospace") - X, Y = np.linspace(0, 100, 10), np.zeros(10) fig, ax = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 2]}, figsize=(10, 6)) From 310c72285823e875fe210071c6e15fb29783f18e Mon Sep 17 00:00:00 2001 From: thoo Date: Fri, 26 Oct 2018 01:36:10 -0400 Subject: [PATCH 6/9] Fix typo and remove () --- examples/lines_bars_and_markers/linestyles.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index 9950708117df..6346ad8e2c72 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -6,7 +6,7 @@ Linestyle can be provided as simple as *solid* , *dotted*, *dashed* or *dashdot*. Moreover, the dashing of the line can be controlled by a dash tuple such as (offset, (on_off_seq)) as mentioned in -`.Line2D.set_linestyle`. For e.g., ``(0, (3, 10, 1, 15))`` means +`.Line2D.set_linestyle`. For example, ``(0, (3, 10, 1, 15))`` means 3pt-line,10pt-space,1pt-line,15pt-space with no offset. *Note*: The dash style can also be configured via `.Line2D.set_dashes` @@ -19,10 +19,10 @@ from matplotlib.transforms import blended_transform_factory linestyle_str = [ - ('solid', ('solid')), # Same as (0, ()) or '-' - ('dotted', ('dotted')), # Same as (0, (1, 1)) or '.' - ('dashed', ('dashed')), # Same as '--' - ('dashdot', ('dashdot'))] # Same as '-.' + ('solid','solid'), # Same as (0, ()) or '-' + ('dotted', 'dotted'), # Same as (0, (1, 1)) or '.' + ('dashed', 'dashed'), # Same as '--' + ('dashdot', 'dashdot')] # Same as '-.' linestyle_tuple = [ ('loosely dotted', (0, (1, 10))), From efebe76cdcc1f46fffd8e842756651aed434ac84 Mon Sep 17 00:00:00 2001 From: thoo Date: Fri, 26 Oct 2018 15:48:47 -0400 Subject: [PATCH 7/9] Fix typo --- examples/lines_bars_and_markers/linestyles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index 6346ad8e2c72..3d52e86ad1c8 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -19,7 +19,7 @@ from matplotlib.transforms import blended_transform_factory linestyle_str = [ - ('solid','solid'), # Same as (0, ()) or '-' + ('solid', 'solid'), # Same as (0, ()) or '-' ('dotted', 'dotted'), # Same as (0, (1, 1)) or '.' ('dashed', 'dashed'), # Same as '--' ('dashdot', 'dashdot')] # Same as '-.' From 318ff59b8d1d93bb1188bc28367cdc61f78820ab Mon Sep 17 00:00:00 2001 From: thoo Date: Fri, 26 Oct 2018 18:40:53 -0400 Subject: [PATCH 8/9] Fix Sphinx error --- examples/lines_bars_and_markers/linestyles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index 3d52e86ad1c8..e3a2317f5e30 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -7,7 +7,7 @@ or *dashdot*. Moreover, the dashing of the line can be controlled by a dash tuple such as (offset, (on_off_seq)) as mentioned in `.Line2D.set_linestyle`. For example, ``(0, (3, 10, 1, 15))`` means - 3pt-line,10pt-space,1pt-line,15pt-space with no offset. +3pt-line,10pt-space,1pt-line,15pt-space with no offset. *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` From d2e6e9260c0d25567a55f44d050d355684804044 Mon Sep 17 00:00:00 2001 From: thoo Date: Mon, 29 Oct 2018 16:29:03 -0400 Subject: [PATCH 9/9] changes as recommended --- examples/lines_bars_and_markers/linestyles.py | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/examples/lines_bars_and_markers/linestyles.py b/examples/lines_bars_and_markers/linestyles.py index e3a2317f5e30..50b2e6fcd7f4 100644 --- a/examples/lines_bars_and_markers/linestyles.py +++ b/examples/lines_bars_and_markers/linestyles.py @@ -3,11 +3,11 @@ Linestyles ========== -Linestyle can be provided as simple as *solid* , *dotted*, *dashed* -or *dashdot*. Moreover, the dashing of the line can be controlled by -a dash tuple such as (offset, (on_off_seq)) as mentioned in -`.Line2D.set_linestyle`. For example, ``(0, (3, 10, 1, 15))`` means -3pt-line,10pt-space,1pt-line,15pt-space with no offset. +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` @@ -26,12 +26,15 @@ linestyle_tuple = [ ('loosely dotted', (0, (1, 10))), + ('dotted', (0, (1, 1))), ('densely dotted', (0, (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))), ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))), @@ -39,8 +42,10 @@ ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))] -def simple_plot(ax, linestyles): +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) @@ -56,12 +61,12 @@ def simple_plot(ax, linestyles): xytext=(-6, -12), textcoords='offset points', color="blue", fontsize=8, ha="right", family="monospace") -X, Y = np.linspace(0, 100, 10), np.zeros(10) -fig, ax = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 2]}, - figsize=(10, 6)) -simple_plot(ax[0], linestyle_str[::-1]) -simple_plot(ax[1], linestyle_tuple[::-1]) +fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 3]}, + figsize=(10, 8)) + +plot_linestyles(ax0, linestyle_str[::-1]) +plot_linestyles(ax1, linestyle_tuple[::-1]) plt.tight_layout() plt.show()