You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/day3/new-matplotlib-intro.rst
+98-58Lines changed: 98 additions & 58 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -121,53 +121,88 @@ In most cases, you will need to load a compatible version of SciPy-bundle to use
121
121
122
122
.. tab:: UPPMAX
123
123
124
-
On Rackham, **loading Python version 3.8.7 or newer will allow you to import Matplotlib and NumPy** without having to load anything else. If you wish to also import Jupyter, Pandas, and/or Seaborn, those and Matplotlib are also provided all together by ``python_ML_packages``. The output of ``module spider python_ML_packages`` is
124
+
On **Pelle**, there is currently only 1 version of Matplotlib, ``matplotlib/3.9.2-gfbf-2024a``, and it can be loaded directly. The following shows the output of ``ml`` (module list) after loading ``matplotlib/3.9.2-gfbf-2024a``:
matplotlib is a python 2D plotting library which produces publication
157
-
quality figures in a variety of hardcopy formats and interactive
158
-
environments across platforms. matplotlib can be used in python
159
-
scripts, the python and ipython shell, web application servers, and
160
-
six graphical user interface toolkits.
161
-
162
-
Versions:
163
-
matplotlib/2.2.3-fosscuda-2018b-Python-2.7.15
164
-
matplotlib/3.0.0-intel-2018b-Python-3.6.6
165
-
matplotlib/3.0.3-foss-2019a-Python-3.7.2
166
-
matplotlib/3.3.3-foss-2020b
167
-
matplotlib/3.3.3-fosscuda-2020b
168
-
matplotlib/3.4.3-foss-2021b
169
-
170
-
The native backend should work if you are logged in via Thinlinc, but if there is a problem, try setting ``matplotlib.use('Qt5Agg')`` in your script. You'll need X-forwarding to view any graphics via SSH, and that may be prohibitively slow.
Take note of the ``-gfbf-2024a`` suffix on some Python modules and the ``-GCCcore-13.3.0`` suffix on most other modules. You should stick to these for the duration of this course to ensure compatibility.
155
+
156
+
.. adminition:: Rackham (Soon to be retired)
157
+
:collapsible: closed
158
+
159
+
On Rackham, **loading Python version 3.8.7 or newer will allow you to import Matplotlib and NumPy** without having to load anything else. If you wish to also import Jupyter, Pandas, and/or Seaborn, those and Matplotlib are also provided all together by ``python_ML_packages``. The output of ``module spider python_ML_packages`` is
matplotlib is a python 2D plotting library which produces publication
192
+
quality figures in a variety of hardcopy formats and interactive
193
+
environments across platforms. matplotlib can be used in python
194
+
scripts, the python and ipython shell, web application servers, and
195
+
six graphical user interface toolkits.
196
+
197
+
Versions:
198
+
matplotlib/2.2.3-fosscuda-2018b-Python-2.7.15
199
+
matplotlib/3.0.0-intel-2018b-Python-3.6.6
200
+
matplotlib/3.0.3-foss-2019a-Python-3.7.2
201
+
matplotlib/3.3.3-foss-2020b
202
+
matplotlib/3.3.3-fosscuda-2020b
203
+
matplotlib/3.4.3-foss-2021b
204
+
205
+
The native backend should work if you are logged in via Thinlinc, but if there is a problem, try setting ``matplotlib.use('Qt5Agg')`` in your script. You'll need X-forwarding to view any graphics via SSH, and that may be prohibitively slow.
171
206
172
207
.. tab:: NSC (Tetralith)
173
208
@@ -329,22 +364,22 @@ A figure and a set of axes objects are created explicitly, usually with ``fig,ax
329
364
330
365
The outputs look the same for both of these examples because the plot type was chosen to work with both APIs, but the explicit API offers a much wider range of plot types and customizations.
331
366
332
-
.. challenge:: Let x be an array of 50 values from -5 to 5. Plot y = 1/(1+exp(-x)).
367
+
.. challenge:: Let x be an array of 50 values from -5 to 5. Write a script to plot y = 1/(1+exp(-x)) that will run from the command line. (Hint: Refer to the section above on *Controlling the Display*.)
333
368
334
369
.. solution:: Solution
335
370
:class: dropdown
336
371
337
-
The code block below uses Jupyter to render the output, which requires ``%matplotlib inline``. If you're at the command line, you would have had to import matplotlib and set ``matplotlib.use('TkAgg')`` or the recommended backend from the section on controlling the display. You did not have to choose a format string.
338
-
339
-
.. jupyter-execute::
340
-
341
-
import numpy as np
342
-
import matplotlib.pyplot as plt
343
-
%matplotlib inline
344
-
x = np.linspace(-5,5, 50)
345
-
fig, ax = plt.subplots()
346
-
ax.plot(x, 1/(1+np.exp(-x)))
347
-
plt.show()
372
+
The code block below uses Jupyter to render the output, which requires ``%matplotlib inline``. At the command line, you would have had to import matplotlib and set ``matplotlib.use('TkAgg')`` or the recommended backend from the section on controlling the display.
373
+
374
+
.. jupyter-execute::
375
+
376
+
import numpy as np
377
+
import matplotlib.pyplot as plt
378
+
%matplotlib inline
379
+
x = np.linspace(-5,5, 50)
380
+
fig, ax = plt.subplots()
381
+
ax.plot(x, 1/(1+np.exp(-x)))
382
+
plt.show()
348
383
349
384
350
385
.. admonition:: Subplots and Subplot Mosaics
@@ -409,9 +444,14 @@ The Matplotlib GUI has a typical save menu option (indicated by the usual floppy
409
444
- ``plt.savefig(fname, *, transparent=None, dpi='figure', format=None)`` is the general-purpose save function. There are other kwargs not shown here, but these are the most important. The file type can be given ``format`` or inferred from an extension given in ``fname``. The default ``dpi`` is inherited from ``plt.figure()`` or ``plt.subplots()``. If ``transparent=True``, the white background of a typical figure is removed so the figure can be displayed on top of other content.
410
445
- ``plt.imsave(fname, arr, **kwargs)`` is specifically for saving arrays to images. It accepts a 2D (single-channel) array with a specified colormap and normalization, or an RGB(A) array (a stack of images in 3 color channels, or 3 color channels and an opacity array). Generally you also have to set ``origin='lower'`` for the image to be rendered right-side up.
411
446
412
-
A few common formats that Matplotlib supports include PDF, PS, EPS, PNG, and JPG/JPEG. Other desirable formats like TIFF and SVG are not supported natively in interactive display backends, but can be used with static backends (used for saving figures without displaying them) or with the installation of the ``Pillow`` module. At most facilities, Pillow is loaded with Matplotlib, so you will see SVG as a save-format option in the GUI. `Matplotlib has a tutorial here <https://matplotlib.org/stable/tutorials/images.html>`_ on importing images into arrays for use with ``pyplot.imshow()``.
447
+
A few common formats that Matplotlib supports include PDF, PS, EPS, PNG, and JPG/JPEG. Other desirable formats like TIFF and SVG are not supported natively in interactive display backends, but can be used with static backends (used for saving figures without displaying them) or with the installation of the ``Pillow`` module. At most facilities, Pillow is loaded with Matplotlib, so you will see SVG as a save-format option in the GUI, and it should be allowed in the format kwarg of ``plt.savefig()``. `Matplotlib has a tutorial here <https://matplotlib.org/stable/tutorials/images.html>`_ on importing images into arrays for use with ``plt.imshow()``.
448
+
449
+
.. challenge:: Reformat the earlier example to save a file in the format ``'svg'`` without going through the GUI, as you would have to do to create plots with code that runs via batch script.
450
+
451
+
.. solution:: Solution
452
+
:class: dropdown
413
453
414
-
.. challenge:: Rerun your earlier example and save it as an SVG file if the option is available, PDF otherwise.
454
+
If your previous code already runs at the command line, all you had to do was replace ``plt.show()`` with something like ``plt.savefig('my_fig', format='svg')``.
0 commit comments