Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9fa3e60

Browse filesBrowse files
thootimhoffm
authored andcommitted
Improve linestyles example (#12586)
1 parent ad779b4 commit 9fa3e60
Copy full SHA for 9fa3e60

File tree

Expand file treeCollapse file tree

2 files changed

+50
-54
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+50
-54
lines changed
Open diff view settings
Collapse file

‎examples/lines_bars_and_markers/line_styles_reference.py‎

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/line_styles_reference.py
-24Lines changed: 0 additions & 24 deletions
This file was deleted.
Collapse file

‎examples/lines_bars_and_markers/linestyles.py‎

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/linestyles.py
+50-30Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,70 @@
33
Linestyles
44
==========
55
6-
This examples showcases different linestyles copying those of Tikz/PGF.
6+
Simple linestyles can be defined using the strings "solid", "dotted", "dashed"
7+
or "dashdot". More refined control can be achieved by providing a dash tuple
8+
``(offset, (on_off_seq))``. For example, ``(0, (3, 10, 1, 15))`` means
9+
(3pt line, 10pt space, 1pt line, 15pt space) with no offset. See also
10+
`.Line2D.set_linestyle`.
11+
12+
*Note*: The dash style can also be configured via `.Line2D.set_dashes`
13+
as shown in :doc:`/gallery/lines_bars_and_markers/line_demo_dash_control`
14+
and passing a list of dash sequences using the keyword *dashes* to the
15+
cycler in :doc:`property_cycle </tutorials/intermediate/color_cycle>`.
716
"""
817
import numpy as np
918
import matplotlib.pyplot as plt
10-
from collections import OrderedDict
1119
from matplotlib.transforms import blended_transform_factory
1220

13-
linestyles = OrderedDict(
14-
[('solid', (0, ())),
15-
('loosely dotted', (0, (1, 10))),
16-
('dotted', (0, (1, 5))),
17-
('densely dotted', (0, (1, 1))),
21+
linestyle_str = [
22+
('solid', 'solid'), # Same as (0, ()) or '-'
23+
('dotted', 'dotted'), # Same as (0, (1, 1)) or '.'
24+
('dashed', 'dashed'), # Same as '--'
25+
('dashdot', 'dashdot')] # Same as '-.'
1826

19-
('loosely dashed', (0, (5, 10))),
20-
('dashed', (0, (5, 5))),
21-
('densely dashed', (0, (5, 1))),
27+
linestyle_tuple = [
28+
('loosely dotted', (0, (1, 10))),
29+
('dotted', (0, (1, 1))),
30+
('densely dotted', (0, (1, 1))),
2231

23-
('loosely dashdotted', (0, (3, 10, 1, 10))),
24-
('dashdotted', (0, (3, 5, 1, 5))),
25-
('densely dashdotted', (0, (3, 1, 1, 1))),
32+
('loosely dashed', (0, (5, 10))),
33+
('dashed', (0, (5, 5))),
34+
('densely dashed', (0, (5, 1))),
35+
36+
('loosely dashdotted', (0, (3, 10, 1, 10))),
37+
('dashdotted', (0, (3, 5, 1, 5))),
38+
('densely dashdotted', (0, (3, 1, 1, 1))),
2639

27-
('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
2840
('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
29-
('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))])
41+
('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
42+
('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]
43+
44+
45+
def plot_linestyles(ax, linestyles):
46+
X, Y = np.linspace(0, 100, 10), np.zeros(10)
47+
yticklabels = []
48+
49+
for i, (name, linestyle) in enumerate(linestyles):
50+
ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')
51+
yticklabels.append(name)
3052

53+
ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5),
54+
yticks=np.arange(len(linestyles)), yticklabels=yticklabels)
3155

32-
plt.figure(figsize=(10, 6))
33-
ax = plt.subplot(1, 1, 1)
56+
# For each line style, add a text annotation with a small offset from
57+
# the reference point (0 in Axes coords, y tick value in Data coords).
58+
reference_transform = blended_transform_factory(ax.transAxes, ax.transData)
59+
for i, (name, linestyle) in enumerate(linestyles):
60+
ax.annotate(repr(linestyle), xy=(0.0, i), xycoords=reference_transform,
61+
xytext=(-6, -12), textcoords='offset points', color="blue",
62+
fontsize=8, ha="right", family="monospace")
3463

35-
X, Y = np.linspace(0, 100, 10), np.zeros(10)
36-
for i, (name, linestyle) in enumerate(linestyles.items()):
37-
ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')
3864

39-
ax.set_ylim(-0.5, len(linestyles)-0.5)
40-
plt.yticks(np.arange(len(linestyles)), linestyles.keys())
41-
plt.xticks([])
65+
fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 3]},
66+
figsize=(10, 8))
4267

43-
# For each line style, add a text annotation with a small offset from
44-
# the reference point (0 in Axes coords, y tick value in Data coords).
45-
reference_transform = blended_transform_factory(ax.transAxes, ax.transData)
46-
for i, (name, linestyle) in enumerate(linestyles.items()):
47-
ax.annotate(str(linestyle), xy=(0.0, i), xycoords=reference_transform,
48-
xytext=(-6, -12), textcoords='offset points', color="blue",
49-
fontsize=8, ha="right", family="monospace")
68+
plot_linestyles(ax0, linestyle_str[::-1])
69+
plot_linestyles(ax1, linestyle_tuple[::-1])
5070

5171
plt.tight_layout()
5272
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.