-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatplotlibInCpp.html
288 lines (218 loc) · 21.3 KB
/
MatplotlibInCpp.html
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
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Matplotlib in C++</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/bootstrap-sphinx.css?v=fadd4351" />
<link rel="stylesheet" type="text/css" href="_static/custom.css?v=77160d70" />
<script src="_static/documentation_options.js?v=a8da1a53"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Multithreading in Mantid" href="MultiThreadingInMantid.html" />
<link rel="prev" title="Logging" href="Logging.html" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59110517-1', 'auto');
ga('send', 'pageview');
</script>
</head><body>
<div id="navbar" class="navbar navbar-default ">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://www.mantidproject.org">
</a>
<span class="navbar-text navbar-version pull-left"><b>main</b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li class="divider-vertical"></li>
<li><a href="index.html">Home</a></li>
<li><a href="https://download.mantidproject.org">Download</a></li>
<li><a href="https://docs.mantidproject.org">User Documentation</a></li>
<li><a href="http://www.mantidproject.org/contact">Contact Us</a></li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="index.html">Documentation</a> »</li>
<li class="nav-item nav-item-this"><a href="">Matplotlib in C++</a></li>
</ul>
</div> </p>
</div>
<div class="container">
<div class="row">
<div class="body col-md-12 content" role="main">
<section id="matplotlib-in-c">
<span id="mpl-in-cpp"></span><h1>Matplotlib in C++<a class="headerlink" href="#matplotlib-in-c" title="Link to this heading">¶</a></h1>
<nav class="contents local" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#overview" id="id1">Overview</a></p></li>
<li><p><a class="reference internal" href="#embedding-figures" id="id2">Embedding Figures</a></p>
<ul>
<li><p><a class="reference internal" href="#plotting-from-vector-data" id="id3">Plotting from Vector Data</a></p></li>
<li><p><a class="reference internal" href="#plotting-from-workspace-data" id="id4">Plotting from Workspace Data</a></p></li>
</ul>
</li>
</ul>
</nav>
<section id="overview">
<h2><a class="toc-backref" href="#id1" role="doc-backlink">Overview</a><a class="headerlink" href="#overview" title="Link to this heading">¶</a></h2>
<p>This document describes the functionality with the C++ layer for interacting with matplotlib plotting.</p>
</section>
<section id="embedding-figures">
<h2><a class="toc-backref" href="#id2" role="doc-backlink">Embedding Figures</a><a class="headerlink" href="#embedding-figures" title="Link to this heading">¶</a></h2>
<p>Matplotlib is designed such that figures can be embedded within other Qt widgets and not just created as separate
figure windows.
An example of how this can be achieved is described in the matplotlib
<a class="reference external" href="https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_qt_sgskip.html">examples</a> section for a pure-Python
based implementation.</p>
<p>Many of our scientific interfaces are written in C++ and require the ability to embed a matplotlib figure within them.
Mantid contains a library, <code class="docutils literal notranslate"><span class="pre">mplcpp</span></code>, to allow this interaction to occur without every developer having to be
concerned with the Python/C++ translation.</p>
<p>The main work is done by the <code class="docutils literal notranslate"><span class="pre">FigureCanvasQt</span></code> class and the <code class="docutils literal notranslate"><span class="pre">Axes</span></code> class. These are designed to closely mimic the
matplotlib classes in the methods that they offer.</p>
<section id="plotting-from-vector-data">
<h3><a class="toc-backref" href="#id3" role="doc-backlink">Plotting from Vector Data</a><a class="headerlink" href="#plotting-from-vector-data" title="Link to this heading">¶</a></h3>
<p>As a simple example consider a widget that wishes to display a single figure with data from two vectors:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="c1">// ------------- .h file ------------------</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">"MantidQtWidgets/MplCpp/Line2D.h"</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><QVBoxLayout></span>
<span class="k">namespace</span><span class="w"> </span><span class="nn">MantidQt</span><span class="w"> </span><span class="p">{</span>
<span class="k">namespace</span><span class="w"> </span><span class="nn">Widgets</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">namespace</span><span class="w"> </span><span class="nn">MplCpp</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">FigureCanvasQt</span><span class="p">;</span>
<span class="p">}}</span>
<span class="k">namespace</span><span class="w"> </span><span class="nn">MyWidgets</span><span class="w"> </span><span class="p">{</span>
<span class="k">class</span><span class="w"> </span><span class="nc">WidgetWithFigure</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">QWidget</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Q_OBJECT</span>
<span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="n">WidgetWithFigure</span><span class="p">(</span><span class="n">QWidget</span><span class="w"> </span><span class="o">*</span><span class="n">parent</span><span class="o">=</span><span class="k">nullptr</span><span class="p">);</span>
<span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">initLayout</span><span class="p">();</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doPlot</span><span class="p">();</span>
<span class="w"> </span><span class="n">FigureCanvasQt</span><span class="w"> </span><span class="o">*</span><span class="n">m_canvas</span><span class="p">;</span>
<span class="w"> </span><span class="n">Line2D</span><span class="w"> </span><span class="n">m_line</span><span class="p">;</span>
<span class="p">};</span>
<span class="c1">// ------------- .cpp file ------------------</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">"MantidQtWidgets/MplCpp/FigureCanvasQt.h"</span>
<span class="n">WidgetWithFigure</span><span class="o">::</span><span class="n">WidgetWithFigure</span><span class="p">(</span><span class="n">QWidget</span><span class="w"> </span><span class="o">*</span><span class="n">parent</span><span class="o">=</span><span class="k">nullptr</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">QWidget</span><span class="p">(</span><span class="n">parent</span><span class="p">),</span>
<span class="w"> </span><span class="c1">/// 111 is a standard matplotlib subplot specification</span>
<span class="w"> </span><span class="n">m_canvas</span><span class="p">{</span><span class="k">new</span><span class="w"> </span><span class="n">FigureCanvasQt</span><span class="p">(</span><span class="mi">111</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">)}</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">initLayout</span><span class="p">();</span>
<span class="w"> </span><span class="n">doPlot</span><span class="p">();</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="n">WidgetWithFigure</span><span class="o">::</span><span class="n">initLayout</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">plotLayout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">QVBoxLayout</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
<span class="w"> </span><span class="n">plotLayout</span><span class="o">-></span><span class="n">addWidget</span><span class="p">(</span><span class="n">m_canvas</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="n">WidgetWithFigure</span><span class="o">::</span><span class="n">doPlot</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="kt">double</span><span class="o">></span><span class="w"> </span><span class="n">xpoints</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">},</span><span class="w"> </span><span class="n">ypoints</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">};</span>
<span class="w"> </span><span class="c1">// In this mode make sure to capture the returned object as it owns the data</span>
<span class="w"> </span><span class="c1">// and when the Line2D is destroyed it will be removed from the canvas.</span>
<span class="w"> </span><span class="n">m_line</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m_canvas</span><span class="o">-></span><span class="n">gca</span><span class="p">().</span><span class="n">plot</span><span class="p">(</span><span class="n">xpoints</span><span class="p">,</span><span class="w"> </span><span class="n">ypoints</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// Most operations on the canvas don't automatically redraw the canvas</span>
<span class="w"> </span><span class="c1">// so drawing must be forced</span>
<span class="w"> </span><span class="n">m_canvas</span><span class="o">-></span><span class="n">draw</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The most important part of the above example is capturing the returned <code class="docutils literal notranslate"><span class="pre">Line2D</span></code> object.
In this basic mode with vectors the library avoids copying the data vectors but this means that
something needs to keep the data alive while it is displayed.</p>
</section>
<section id="plotting-from-workspace-data">
<h3><a class="toc-backref" href="#id4" role="doc-backlink">Plotting from Workspace Data</a><a class="headerlink" href="#plotting-from-workspace-data" title="Link to this heading">¶</a></h3>
<p>A common case will be plotting data from a <code class="docutils literal notranslate"><span class="pre">MatrixWorkspace</span></code>. In this case we leverage the <code class="docutils literal notranslate"><span class="pre">mantid</span></code>
projection in a similar manner to a <a class="reference external" href="https://docs.mantidproject.org/nightly/plotting/index.html#plotting" title="(in MantidProject v6.11)"><span class="xref std std-ref">standard script</span></a>:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="c1">// ------------- .h file ------------------</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><QVBoxLayout></span>
<span class="k">namespace</span><span class="w"> </span><span class="nn">MantidQt</span><span class="w"> </span><span class="p">{</span>
<span class="k">namespace</span><span class="w"> </span><span class="nn">Widgets</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">namespace</span><span class="w"> </span><span class="nn">MplCpp</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">FigureCanvasQt</span><span class="p">;</span>
<span class="p">}}</span>
<span class="k">namespace</span><span class="w"> </span><span class="nn">MyWidgets</span><span class="w"> </span><span class="p">{</span>
<span class="k">class</span><span class="w"> </span><span class="nc">WidgetWithFigure</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">QWidget</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Q_OBJECT</span>
<span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="n">WidgetWithFigure</span><span class="p">(</span><span class="n">QWidget</span><span class="w"> </span><span class="o">*</span><span class="n">parent</span><span class="o">=</span><span class="k">nullptr</span><span class="p">);</span>
<span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">initLayout</span><span class="p">();</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doPlot</span><span class="p">();</span>
<span class="w"> </span><span class="n">FigureCanvasQt</span><span class="w"> </span><span class="o">*</span><span class="n">m_canvas</span><span class="p">;</span>
<span class="p">};</span>
<span class="c1">// ------------- .cpp file ------------------</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">"MantidQtWidgets/MplCpp/FigureCanvasQt.h"</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">"MantidQtWidgets/MplCpp/MantidAxes.h"</span>
<span class="k">using</span><span class="w"> </span><span class="n">MantidQt</span><span class="o">::</span><span class="n">Widgets</span><span class="o">::</span><span class="n">MplCpp</span><span class="o">::</span><span class="n">MantidAxes</span><span class="p">;</span>
<span class="k">namespace</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">constexpr</span><span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">PROJECTION</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"mantid"</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">WidgetWithFigure</span><span class="o">::</span><span class="n">WidgetWithFigure</span><span class="p">(</span><span class="n">QWidget</span><span class="w"> </span><span class="o">*</span><span class="n">parent</span><span class="o">=</span><span class="k">nullptr</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">QWidget</span><span class="p">(</span><span class="n">parent</span><span class="p">),</span>
<span class="w"> </span><span class="c1">/// 111 is a standard matplotlib subplot specification</span>
<span class="w"> </span><span class="n">m_canvas</span><span class="p">{</span><span class="k">new</span><span class="w"> </span><span class="n">FigureCanvasQt</span><span class="p">(</span><span class="mi">111</span><span class="p">,</span><span class="w"> </span><span class="n">PROJECTION</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">)}</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">initLayout</span><span class="p">();</span>
<span class="w"> </span><span class="n">doPlot</span><span class="p">();</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="n">WidgetWithFigure</span><span class="o">::</span><span class="n">initLayout</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">plotLayout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">QVBoxLayout</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
<span class="w"> </span><span class="n">plotLayout</span><span class="o">-></span><span class="n">addWidget</span><span class="p">(</span><span class="n">m_canvas</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span><span class="w"> </span><span class="n">WidgetWithFigure</span><span class="o">::</span><span class="n">doPlot</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">ws</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">getMyWorkspaceFromSomewhere</span><span class="p">();</span>
<span class="w"> </span><span class="c1">// In this mode the data is copied to matplotib so there is no need to keep</span>
<span class="w"> </span><span class="c1">// hold of the returned object</span>
<span class="w"> </span><span class="n">m_line</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">m_canvas</span><span class="o">-></span><span class="n">gca</span><span class="o"><</span><span class="n">MantidAxes</span><span class="o">></span><span class="p">().</span><span class="n">plot</span><span class="p">(</span><span class="n">ws</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// Most operations on the canvas don't automatically redraw the canvas</span>
<span class="w"> </span><span class="c1">// so drawing must be forced</span>
<span class="w"> </span><span class="n">m_canvas</span><span class="o">-></span><span class="n">draw</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>In this mode the data does not need to be held by the C++ object as it is copied to the matplotlib curve.</p>
</section>
</section>
</section>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<ul class="nav navbar-nav" style=" float: right;">
<li>
<a href="Logging.html" title="Previous Chapter: Logging"><span class="glyphicon glyphicon-chevron-left visible-sm"></span><span class="hidden-sm hidden-tablet">« Logging</span>
</a>
</li>
<li>
<a href="MultiThreadingInMantid.html" title="Next Chapter: Multithreading in Mantid"><span class="glyphicon glyphicon-chevron-right visible-sm"></span><span class="hidden-sm hidden-tablet">Multithreadin... »</span>
</a>
</li>
<li><a href="#">Back to top</a></li>
</ul>
<p>
</p>
</div>
</footer>
</body>
</html>