-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathMEP26.html
More file actions
392 lines (365 loc) · 27.9 KB
/
MEP26.html
File metadata and controls
392 lines (365 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MEP26: Artist styling — Matplotlib 3.4.0 documentation</title>
<link rel="stylesheet" href="../../_static/mpl.css?v3.4.0-2-gcb03754703" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
<link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
<link rel="stylesheet" type="text/css" href="../../_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="../../_static/gallery.css" />
<link rel="stylesheet" type="text/css" href="../../_static/gallery-binder.css" />
<link rel="stylesheet" type="text/css" href="../../_static/gallery-dataframe.css" />
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
<script src="../../_static/jquery.js"></script>
<script src="../../_static/underscore.js"></script>
<script src="../../_static/doctools.js"></script>
<script src="../../_static/language_data.js"></script>
<script src="../../_static/clipboard.min.js"></script>
<script src="../../_static/copybutton.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Matplotlib 3.4.0 documentation"
href="../../_static/opensearch.xml"/>
<link rel="shortcut icon" href="../../_static/favicon.ico"/>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="next" title="MEP27: decouple pyplot from backends" href="MEP27.html" />
<link rel="prev" title="MEP25: Serialization" href="MEP25.html" />
<link rel="top" title="Matplotlib 3.4.0 documentation" href="#" />
<link rel="canonical" href="https://matplotlib.org/stable/devel/MEP/MEP26.html" />
<link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
<script data-domain="matplotlib.org" defer="defer" src="https://views.scientific-python.org/js/script.js"></script>
</head><body>
<div id="unreleased-message"> You are reading an old version of the documentation (v3.4.0). For the latest version see <a href="https://matplotlib.org/stable/devel/MEP/MEP26.html">https://matplotlib.org/stable/devel/MEP/MEP26.html</a></div>
<!--
<div id="annc-banner">
</div>
-->
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px; position: relative;">
<a href="../../index.html">
<div style="float: left; position: absolute; width: 496px; bottom: 0; padding-bottom: 24px"><span style="float: right; color: #789; background: white">Version 3.4.0</span></div>
<img src="../../_static/logo2_compressed.svg" height="125px" border="0" alt="matplotlib"/></a>
<!-- The "Fork me on github" ribbon -->
<div id="forkongithub"><a href="https://github.com/matplotlib/matplotlib">Fork me on GitHub</a></div>
</div>
<nav class="main-nav">
<ul>
<li><a href="../../users/installing.html">Installation</a></li>
<li><a href="../../contents.html">Documentation</a></li>
<li><a href="../../gallery/index.html">Examples</a></li>
<li><a href="../../tutorials/index.html">Tutorials</a></li>
<li><a href="../index.html">Contributing</a></li>
<li class="nav-right">
<form class="search" action="../../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" placeholder="Search"/>
</form>
</li>
</ul>
</nav>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="MEP27.html" title="MEP27: decouple pyplot from backends"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="MEP25.html" title="MEP25: Serialization"
accesskey="P">previous</a> |</li>
<li><a href="../../index.html">home</a>| </li>
<li><a href="../../contents.html">contents</a> »</li>
<li class="nav-item nav-item-1"><a href="../index.html" >The Matplotlib Developers' Guide</a> »</li>
<li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Matplotlib Enhancement Proposals</a> »</li>
<li class="nav-item nav-item-this"><a href="">MEP26: Artist styling</a></li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">MEP26: Artist styling</a><ul>
<li><a class="reference internal" href="#status">Status</a></li>
<li><a class="reference internal" href="#branches-and-pull-requests">Branches and Pull requests</a></li>
<li><a class="reference internal" href="#abstract">Abstract</a></li>
<li><a class="reference internal" href="#detailed-description">Detailed description</a></li>
<li><a class="reference internal" href="#implementation">Implementation</a><ul>
<li><a class="reference internal" href="#bnf-grammar">BNF Grammar</a></li>
<li><a class="reference internal" href="#syntax">Syntax</a><ul>
<li><a class="reference internal" href="#selectors">Selectors</a></li>
<li><a class="reference internal" href="#gid-selector">GID selector</a></li>
<li><a class="reference internal" href="#attributes-and-values">Attributes and values</a></li>
</ul>
</li>
<li><a class="reference internal" href="#parsing">Parsing</a></li>
<li><a class="reference internal" href="#visitor-pattern-for-matplotlib-figure">Visitor pattern for matplotlib figure</a></li>
</ul>
</li>
<li><a class="reference internal" href="#backward-compatibility">Backward compatibility</a></li>
<li><a class="reference internal" href="#alternatives">Alternatives</a></li>
<li><a class="reference internal" href="#appendix">Appendix</a><ul>
<li><a class="reference internal" href="#matplotlib-primitives">Matplotlib primitives</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<div id="sidebar-pagesource" role="note" aria-label="source link"
style="margin-top: 1.5em; padding-top: 0.1em; border-top: 1px solid #86989b">
<a href="../../_sources/devel/MEP/MEP26.rst.txt"
style="color: #c0c0c0" rel="nofollow">Show Page Source</a>
</div>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="mep26-artist-styling">
<h1>MEP26: Artist styling<a class="headerlink" href="#mep26-artist-styling" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#status" id="id1">Status</a></li>
<li><a class="reference internal" href="#branches-and-pull-requests" id="id2">Branches and Pull requests</a></li>
<li><a class="reference internal" href="#abstract" id="id3">Abstract</a></li>
<li><a class="reference internal" href="#detailed-description" id="id4">Detailed description</a></li>
<li><a class="reference internal" href="#implementation" id="id5">Implementation</a><ul>
<li><a class="reference internal" href="#bnf-grammar" id="id6">BNF Grammar</a></li>
<li><a class="reference internal" href="#syntax" id="id7">Syntax</a><ul>
<li><a class="reference internal" href="#selectors" id="id8">Selectors</a></li>
<li><a class="reference internal" href="#gid-selector" id="id9">GID selector</a></li>
<li><a class="reference internal" href="#attributes-and-values" id="id10">Attributes and values</a></li>
</ul>
</li>
<li><a class="reference internal" href="#parsing" id="id11">Parsing</a></li>
<li><a class="reference internal" href="#visitor-pattern-for-matplotlib-figure" id="id12">Visitor pattern for matplotlib figure</a></li>
</ul>
</li>
<li><a class="reference internal" href="#backward-compatibility" id="id13">Backward compatibility</a></li>
<li><a class="reference internal" href="#alternatives" id="id14">Alternatives</a></li>
<li><a class="reference internal" href="#appendix" id="id15">Appendix</a><ul>
<li><a class="reference internal" href="#matplotlib-primitives" id="id16">Matplotlib primitives</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="status">
<h2><a class="toc-backref" href="#id1">Status</a><a class="headerlink" href="#status" title="Permalink to this headline">¶</a></h2>
<p><strong>Rejected</strong></p>
</div>
<div class="section" id="branches-and-pull-requests">
<h2><a class="toc-backref" href="#id2">Branches and Pull requests</a><a class="headerlink" href="#branches-and-pull-requests" title="Permalink to this headline">¶</a></h2>
</div>
<div class="section" id="abstract">
<h2><a class="toc-backref" href="#id3">Abstract</a><a class="headerlink" href="#abstract" title="Permalink to this headline">¶</a></h2>
<p>This MEP proposes a new stylesheet implementation to allow more
comprehensive and dynamic styling of artists.</p>
<p>The current version of matplotlib (1.4.0) allows stylesheets based on
the rcParams syntax to be applied before creation of a plot. The
methodology below proposes a new syntax, based on CSS, which would
allow styling of individual artists and properties, which can be
applied dynamically to existing objects.</p>
<p>This is related to (and makes steps toward) the overall goal of moving
to a DOM/tree-like architecture.</p>
</div>
<div class="section" id="detailed-description">
<h2><a class="toc-backref" href="#id4">Detailed description</a><a class="headerlink" href="#detailed-description" title="Permalink to this headline">¶</a></h2>
<p>Currently, the look and appearance of existing artist objects (figure,
axes, Line2D etc...) can only be updated via <code class="docutils literal notranslate"><span class="pre">set_</span></code> and <code class="docutils literal notranslate"><span class="pre">get_</span></code> methods
on the artist object, which is quite laborious, especially if no
reference to the artist(s) has been stored. The new style sheets
introduced in 1.4 allow styling before a plot is created, but do not
offer any means to dynamically update plots or distinguish between
artists of the same type (i.e. to specify the <code class="docutils literal notranslate"><span class="pre">line</span> <span class="pre">color</span></code> and <code class="docutils literal notranslate"><span class="pre">line</span>
<span class="pre">style</span></code> separately for differing <a class="reference internal" href="../../api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D" title="matplotlib.lines.Line2D"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Line2D</span></code></a> objects).</p>
<p>The initial development should concentrate on allowing styling of
artist primitives (those <a class="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Artist</span></code></a>s that do not contain other
<a class="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Artist</span></code></a>s), and further development could expand the CSS syntax rules
and parser to allow more complex styling. See the appendix for a list
of primitives.</p>
<p>The new methodology would require development of a number of steps:</p>
<ul class="simple">
<li>A new stylesheet syntax (likely based on CSS) to allow selection of
artists by type, class, id etc...</li>
<li>A mechanism by which to parse a stylesheet into a tree</li>
<li>A mechanism by which to translate the parse-tree into something
which can be used to update the properties of relevant
artists. Ideally this would implement a method by which to traverse
the artists in a tree-like structure.</li>
<li>A mechanism by which to generate a stylesheet from existing artist
properties. This would be useful to allow a user to export a
stylesheet from an existing figure (where the appearance may have
been set using the matplotlib API)...</li>
</ul>
</div>
<div class="section" id="implementation">
<h2><a class="toc-backref" href="#id5">Implementation</a><a class="headerlink" href="#implementation" title="Permalink to this headline">¶</a></h2>
<p>It will be easiest to allow a '3rd party' to modify/set the style of an artist
if the 'style' is created as a separate class and store against the artist as a
property. The <a class="reference internal" href="../../api/backend_bases_api.html#matplotlib.backend_bases.GraphicsContextBase" title="matplotlib.backend_bases.GraphicsContextBase"><code class="xref py py-obj docutils literal notranslate"><span class="pre">GraphicsContextBase</span></code></a> class already provides a the basis of a
<code class="docutils literal notranslate"><span class="pre">Style</span></code> class and an artist's <a class="reference internal" href="../../api/_as_gen/matplotlib.artist.Artist.draw.html#matplotlib.artist.Artist.draw" title="matplotlib.artist.Artist.draw"><code class="xref py py-obj docutils literal notranslate"><span class="pre">draw</span></code></a> method can be refactored to use
the <code class="docutils literal notranslate"><span class="pre">Style</span></code> class rather than setting up its own <a class="reference internal" href="../../api/backend_bases_api.html#matplotlib.backend_bases.GraphicsContextBase" title="matplotlib.backend_bases.GraphicsContextBase"><code class="xref py py-obj docutils literal notranslate"><span class="pre">GraphicsContextBase</span></code></a> and
transferring its style-related properties to it. A minimal example of how this
could be implemented is shown here: <a class="reference external" href="https://github.com/JamesRamm/mpl_experiment">https://github.com/JamesRamm/mpl_experiment</a></p>
<p>IMO, this will also make the API and code base much neater as
individual get/set methods for artist style properties are now
redundant... Indirectly related would be a general drive to replace
get/set methods with properties. Implementing the style class with
properties would be a big stride toward this...</p>
<p>For initial development, I suggest developing a syntax based on a much
(much much) simplified version of CSS. I am in favour of dubbing this
Artist Style Sheets :+1: :</p>
<div class="section" id="bnf-grammar">
<h3><a class="toc-backref" href="#id6">BNF Grammar</a><a class="headerlink" href="#bnf-grammar" title="Permalink to this headline">¶</a></h3>
<p>I propose a very simple syntax to implement initially (like a proof of
concept), which can be expanded upon in the future. The BNF form of
the syntax is given below and then explained</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">RuleSet</span> <span class="p">:</span><span class="o">:=</span> <span class="n">SelectorSequence</span> <span class="s2">"{"</span><span class="n">Declaration</span><span class="s2">"}"</span>
<span class="n">SelectorSequence</span> <span class="p">::</span> <span class="o">=</span> <span class="n">Selector</span> <span class="p">{</span><span class="s2">","</span> <span class="n">Selector</span><span class="p">}</span>
<span class="n">Declaration</span> <span class="p">:</span><span class="o">:=</span> <span class="n">propName</span><span class="s2">":"</span> <span class="n">propValue</span><span class="s2">";"</span>
<span class="n">Selector</span> <span class="p">:</span><span class="o">:=</span> <span class="n">ArtistIdent</span><span class="p">{</span><span class="s2">"#"</span><span class="n">Ident</span><span class="p">}</span>
<span class="n">propName</span> <span class="p">:</span><span class="o">:=</span> <span class="n">Ident</span>
<span class="n">propValue</span> <span class="p">:</span><span class="o">:=</span> <span class="n">Ident</span> <span class="o">|</span> <span class="n">Number</span> <span class="o">|</span> <span class="n">Colour</span> <span class="o">|</span> <span class="s2">"None"</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ArtistIdent</span></code>, <code class="docutils literal notranslate"><span class="pre">Ident</span></code>, <code class="docutils literal notranslate"><span class="pre">Number</span></code> and <code class="docutils literal notranslate"><span class="pre">Colour</span></code> are tokens (the basic
building blocks of the expression) which are defined by regular
expressions.</p>
</div>
<div class="section" id="syntax">
<h3><a class="toc-backref" href="#id7">Syntax</a><a class="headerlink" href="#syntax" title="Permalink to this headline">¶</a></h3>
<p>A CSS stylesheet consists of a series of <strong>rule sets</strong> in hierarchical
order (rules are applied from top to bottom). Each rule follows the
syntax</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">selector</span> <span class="p">{</span><span class="n">attribute</span><span class="p">:</span> <span class="n">value</span><span class="p">;}</span>
</pre></div>
</div>
<p>Each rule can have any number of <code class="docutils literal notranslate"><span class="pre">attribute:</span> <span class="pre">value</span></code> pairs, and a
stylesheet can have any number of rules.</p>
<p>The initial syntax is designed only for <a class="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Artist</span></code></a> primitives. It does
not address the question of how to set properties on <a class="reference internal" href="../../api/container_api.html#matplotlib.container.Container" title="matplotlib.container.Container"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Container</span></code></a> types
(whose properties may themselves be <a class="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Artist</span></code></a>s with settable
properties), however, a future solution to this could simply be nested
<code class="docutils literal notranslate"><span class="pre">RuleSet</span></code>s</p>
<div class="section" id="selectors">
<h4><a class="toc-backref" href="#id8">Selectors</a><a class="headerlink" href="#selectors" title="Permalink to this headline">¶</a></h4>
<p>Selectors define the object to which the attribute updates should be
applied. As a starting point, I propose just 2 selectors to use in
initial development:</p>
<p>Artist Type Selector</p>
<p>Select an <a class="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Artist</span></code></a> by it's type. E.g <a class="reference internal" href="../../api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D" title="matplotlib.lines.Line2D"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Line2D</span></code></a> or <a class="reference internal" href="../../api/text_api.html#matplotlib.text.Text" title="matplotlib.text.Text"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Text</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Line2D</span> <span class="p">{</span><span class="n">attribute</span><span class="p">:</span> <span class="n">value</span><span class="p">}</span>
</pre></div>
</div>
<p>The regex for matching the artist type selector (<code class="docutils literal notranslate"><span class="pre">ArtistIdent</span></code> in the BNF grammar) would be:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ArtistIdent</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?P<ArtistIdent>\bLine2D\b|\bText\b|\bAxesImage\b|\bFigureImage\b|\bPatch\b)'</span>
</pre></div>
</div>
</div>
<div class="section" id="gid-selector">
<h4><a class="toc-backref" href="#id9">GID selector</a><a class="headerlink" href="#gid-selector" title="Permalink to this headline">¶</a></h4>
<p>Select an <a class="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Artist</span></code></a> by its <code class="docutils literal notranslate"><span class="pre">gid</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Line2D</span><span class="c1">#myGID {attribute: value}</span>
</pre></div>
</div>
<p>A <code class="docutils literal notranslate"><span class="pre">gid</span></code> can be any string, so the regex could be as follows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Ident</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?P<Ident>[a-zA-Z_][a-zA-Z_0-9]*)'</span>
</pre></div>
</div>
<p>The above selectors roughly correspond to their CSS counterparts
(<a class="reference external" href="http://www.w3.org/TR/CSS21/selector.html">http://www.w3.org/TR/CSS21/selector.html</a>)</p>
</div>
<div class="section" id="attributes-and-values">
<h4><a class="toc-backref" href="#id10">Attributes and values</a><a class="headerlink" href="#attributes-and-values" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">Attributes</span></code> are any valid (settable) property for the <a class="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Artist</span></code></a> in question.</li>
<li><code class="docutils literal notranslate"><span class="pre">Values</span></code> are any valid value for the property (Usually a string, or number).</li>
</ul>
</div>
</div>
<div class="section" id="parsing">
<h3><a class="toc-backref" href="#id11">Parsing</a><a class="headerlink" href="#parsing" title="Permalink to this headline">¶</a></h3>
<p>Parsing would consist of breaking the stylesheet into tokens (the
python cookbook gives a nice tokenizing recipe on page 66), applying
the syntax rules and constructing a <code class="docutils literal notranslate"><span class="pre">Tree</span></code>. This requires defining the
grammar of the stylesheet (again, we can borrow from CSS) and writing
a parser. Happily, there is a recipe for this in the python cookbook
as well.</p>
</div>
<div class="section" id="visitor-pattern-for-matplotlib-figure">
<h3><a class="toc-backref" href="#id12">Visitor pattern for matplotlib figure</a><a class="headerlink" href="#visitor-pattern-for-matplotlib-figure" title="Permalink to this headline">¶</a></h3>
<p>In order to apply the stylesheet rules to the relevant artists, we
need to 'visit' each artist in a figure and apply the relevant rule.
Here is a visitor class (again, thanks to python cookbook), where each
<code class="docutils literal notranslate"><span class="pre">node</span></code> would be an artist in the figure. A <code class="docutils literal notranslate"><span class="pre">visit_</span></code> method would need
to be implemented for each mpl artist, to handle the different
properties for each</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Visitor</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">visit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
<span class="n">name</span> <span class="o">=</span> <span class="s1">'visit_'</span> <span class="o">+</span> <span class="nb">type</span><span class="p">(</span><span class="n">node</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span>
<span class="n">meth</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">meth</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span>
<span class="k">return</span> <span class="n">meth</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
</pre></div>
</div>
<p>An <code class="docutils literal notranslate"><span class="pre">evaluator</span></code> class would then take the stylesheet rules and
implement the visitor on each one of them.</p>
</div>
</div>
<div class="section" id="backward-compatibility">
<h2><a class="toc-backref" href="#id13">Backward compatibility</a><a class="headerlink" href="#backward-compatibility" title="Permalink to this headline">¶</a></h2>
<p>Implementing a separate <code class="docutils literal notranslate"><span class="pre">Style</span></code> class would break backward
compatibility as many get/set methods on an artist would become
redundant. While it would be possible to alter these methods to hook
into the <code class="docutils literal notranslate"><span class="pre">Style</span></code> class (stored as a property against the artist), I
would be in favor of simply removing them to both neaten/simplify the
codebase and to provide a simple, uncluttered API...</p>
</div>
<div class="section" id="alternatives">
<h2><a class="toc-backref" href="#id14">Alternatives</a><a class="headerlink" href="#alternatives" title="Permalink to this headline">¶</a></h2>
<p>No alternatives, but some of the ground covered here overlaps with
MEP25, which may assist in this development</p>
</div>
<div class="section" id="appendix">
<h2><a class="toc-backref" href="#id15">Appendix</a><a class="headerlink" href="#appendix" title="Permalink to this headline">¶</a></h2>
<div class="section" id="matplotlib-primitives">
<h3><a class="toc-backref" href="#id16">Matplotlib primitives</a><a class="headerlink" href="#matplotlib-primitives" title="Permalink to this headline">¶</a></h3>
<p>This will form the initial selectors which stylesheets can use.</p>
<ul class="simple">
<li>Line2D</li>
<li>Text</li>
<li>AxesImage</li>
<li>FigureImage</li>
<li>Patch</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<footer>
<div class="footer">
© Copyright 2002 - 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team; 2012 - 2021 The Matplotlib development team.
<br />
Last updated on Mar 26, 2021.
Created using
<a href="http://sphinx-doc.org/">Sphinx</a> 3.2.1.
Doc version v3.4.0-2-gcb03754703.
</div>
</footer>
</body>
</html>