Skip to content

Commit fd3ca0e

Browse files
authored
Update new-matplotlib-intro.rst
added Pelle info, tweaked examples
1 parent 93148cc commit fd3ca0e

1 file changed

Lines changed: 98 additions & 58 deletions

File tree

docs/day3/new-matplotlib-intro.rst

Lines changed: 98 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -121,53 +121,88 @@ In most cases, you will need to load a compatible version of SciPy-bundle to use
121121

122122
.. tab:: UPPMAX
123123

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``:
125125

126126
.. code-block:: console
127127
128-
----------------------------------------------------------------------------
129-
python_ML_packages:
130-
----------------------------------------------------------------------------
131-
Versions:
132-
python_ML_packages/3.9.5-cpu
133-
python_ML_packages/3.9.5-gpu
134-
python_ML_packages/3.11.8-cpu
135-
136-
----------------------------------------------------------------------------
137-
For detailed information about a specific "python_ML_packages" package (includ
138-
ing how to load the modules) use the module's full name.
139-
Note that names that have a trailing (E) are extensions provided by other modu
140-
les.
141-
For example:
142-
143-
$ module spider python_ML_packages/3.11.8-cpu
144-
----------------------------------------------------------------------------
145-
146-
We recommend the latest version, ``python_ML_packages/3.11.8-cpu``
147-
148-
For versions earlier than Python 3.8.x, ``module spider matplotlib`` outputs the following:
149-
150-
.. code-block:: console
151-
152-
----------------------------------------------------------------------------
153-
matplotlib:
154-
----------------------------------------------------------------------------
155-
Description:
156-
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.
128+
Currently Loaded Modules:
129+
1) GCCcore/13.3.0 25) freetype/2.13.2-GCCcore-13.3.0
130+
2) zlib/1.3.1-GCCcore-13.3.0 26) expat/2.6.2-GCCcore-13.3.0
131+
3) binutils/2.42-GCCcore-13.3.0 27) util-linux/2.40-GCCcore-13.3.0
132+
4) GCC/13.3.0 28) fontconfig/2.15.0-GCCcore-13.3.0
133+
5) OpenBLAS/0.3.27-GCC-13.3.0 29) xorg-macros/1.20.1-GCCcore-13.3.0
134+
6) FlexiBLAS/3.4.4-GCC-13.3.0 30) libpciaccess/0.18.1-GCCcore-13.3.0
135+
7) FFTW/3.3.10-GCC-13.3.0 31) X11/20240607-GCCcore-13.3.0
136+
8) gfbf/2024a 32) Tk/8.6.14-GCCcore-13.3.0
137+
9) bzip2/1.0.8-GCCcore-13.3.0 33) Tkinter/3.12.3-GCCcore-13.3.0
138+
10) ncurses/6.5-GCCcore-13.3.0 34) NASM/2.16.03-GCCcore-13.3.0
139+
11) libreadline/8.2-GCCcore-13.3.0 35) libjpeg-turbo/3.0.1-GCCcore-13.3.0
140+
12) Tcl/8.6.14-GCCcore-13.3.0 36) jbigkit/2.1-GCCcore-13.3.0
141+
13) SQLite/3.45.3-GCCcore-13.3.0 37) gzip/1.13-GCCcore-13.3.0
142+
14) XZ/5.4.5-GCCcore-13.3.0 38) lz4/1.9.4-GCCcore-13.3.0
143+
15) libffi/3.4.5-GCCcore-13.3.0 39) zstd/1.5.6-GCCcore-13.3.0
144+
16) OpenSSL/3 40) libdeflate/1.20-GCCcore-13.3.0
145+
17) Python/3.12.3-GCCcore-13.3.0 41) LibTIFF/4.6.0-GCCcore-13.3.0
146+
18) cffi/1.16.0-GCCcore-13.3.0 42) giflib/5.2.1-GCCcore-13.3.0
147+
19) cryptography/42.0.8-GCCcore-13.3.0 43) libwebp/1.4.0-GCCcore-13.3.0
148+
20) virtualenv/20.26.2-GCCcore-13.3.0 44) OpenJPEG/2.5.2-GCCcore-13.3.0
149+
21) Python-bundle-PyPI/2024.06-GCCcore-13.3.0 45) LittleCMS/2.16-GCCcore-13.3.0
150+
22) SciPy-bundle/2024.05-gfbf-2024a 46) Pillow-SIMD/10.4.0-GCCcore-13.3.0
151+
23) libpng/1.6.43-GCCcore-13.3.0 47) Qhull/2020.2-GCCcore-13.3.0
152+
24) Brotli/1.1.0-GCCcore-13.3.0 48) matplotlib/3.9.2-gfbf-2024a
153+
154+
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
160+
161+
.. code-block:: console
162+
163+
----------------------------------------------------------------------------
164+
python_ML_packages:
165+
----------------------------------------------------------------------------
166+
Versions:
167+
python_ML_packages/3.9.5-cpu
168+
python_ML_packages/3.9.5-gpu
169+
python_ML_packages/3.11.8-cpu
170+
171+
----------------------------------------------------------------------------
172+
For detailed information about a specific "python_ML_packages" package (includ
173+
ing how to load the modules) use the module's full name.
174+
Note that names that have a trailing (E) are extensions provided by other modu
175+
les.
176+
For example:
177+
178+
$ module spider python_ML_packages/3.11.8-cpu
179+
----------------------------------------------------------------------------
180+
181+
We recommend the latest version, ``python_ML_packages/3.11.8-cpu``
182+
183+
For versions earlier than Python 3.8.x, ``module spider matplotlib`` outputs the following:
184+
185+
.. code-block:: console
186+
187+
----------------------------------------------------------------------------
188+
matplotlib:
189+
----------------------------------------------------------------------------
190+
Description:
191+
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.
171206

172207
.. tab:: NSC (Tetralith)
173208

@@ -329,22 +364,22 @@ A figure and a set of axes objects are created explicitly, usually with ``fig,ax
329364

330365
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.
331366

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*.)
333368

334369
.. solution:: Solution
335370
:class: dropdown
336371

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()
348383

349384

350385
.. admonition:: Subplots and Subplot Mosaics
@@ -409,9 +444,14 @@ The Matplotlib GUI has a typical save menu option (indicated by the usual floppy
409444
- ``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.
410445
- ``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.
411446

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
413453

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')``.
415455

416456

417457
Standard Available Plot Types

0 commit comments

Comments
 (0)