Skip to content

REORG: add a LineStyle class #18664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 23 additions & 69 deletions examples/lines_bars_and_markers/linestyles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,28 @@
Linestyles
==========

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 </tutorials/intermediate/color_cycle>`.
The Matplotlib `~mpl._enums.LineStyle` specifies the dash pattern used to draw
a given line. The simplest line styles can be accessed by name using the
strings "solid", "dotted", "dashed" and "dashdot" (or their short names, "-",
":", "--", and "-.", respectively).

The exact spacing of the dashes used can be controlled using the
'lines.*_pattern' family of rc parameters. For example,
:rc:`lines.dashdot_pattern` controls the exact spacing of dashed used whenever
the '-.' `~mpl._enums.LineStyle` is specified.

For more information about how to create custom `~mpl._enums.LineStyle`
specifications, see `the LineStyle docs <mpl._enums.LineStyle>`.

*Note*: For historical reasons, one can also specify the dash pattern for a
particular line using `.Line2D.set_dashes` as shown in
:doc:`/gallery/lines_bars_and_markers/line_demo_dash_control` (or by passing a
list of dash sequences using the keyword *dashes* to the cycler in
:doc:`property_cycle </tutorials/intermediate/color_cycle>`). This interface is
strictly less expressive, and we recommend using LineStyle (or the keyword
*linestyle* to the :doc:`property cycler
</tutorials/intermediate/color_cycle>`).
"""
import numpy as np
import matplotlib.pyplot as plt

linestyle_str = [
('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))),
('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))),
('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]


def plot_linestyles(ax, linestyles, title):
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_title(title)
ax.set(ylim=(-0.5, len(linestyles)-0.5),
yticks=np.arange(len(linestyles)),
yticklabels=yticklabels)
ax.tick_params(left=False, bottom=False, labelbottom=False)
ax.spines[:].set_visible(False)

# 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).
for i, (name, linestyle) in enumerate(linestyles):
ax.annotate(repr(linestyle),
xy=(0.0, i), xycoords=ax.get_yaxis_transform(),
xytext=(-6, -12), textcoords='offset points',
color="blue", fontsize=8, ha="right", family="monospace")


ax0, ax1 = (plt.figure(figsize=(10, 8))
.add_gridspec(2, 1, height_ratios=[1, 3])
.subplots())

plot_linestyles(ax0, linestyle_str[::-1], title='Named linestyles')
plot_linestyles(ax1, linestyle_tuple[::-1], title='Parametrized linestyles')
from matplotlib._enums import LineStyle

plt.tight_layout()
plt.show()
LineStyle.demo()
4 changes: 4 additions & 0 deletions lib/matplotlib/_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,7 @@ def warn_external(message, category=None):
break
frame = frame.f_back
warnings.warn(message, category, stacklevel)


def is_string_like(x):
return isinstance(x, (str, bytes, bytearray))
Loading