wedme-plots 2.0.1__tar.gz → 2.0.5__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {wedme-plots-2.0.1 → wedme_plots-2.0.5}/LICENSE +1 -1
- wedme_plots-2.0.5/MANIFEST.in +1 -0
- wedme_plots-2.0.5/PKG-INFO +186 -0
- wedme_plots-2.0.5/README.md +141 -0
- {wedme-plots-2.0.1 → wedme_plots-2.0.5}/pyproject.toml +2 -2
- {wedme-plots-2.0.1 → wedme_plots-2.0.5}/src/wedme/__init__.py +6 -1
- wedme_plots-2.0.5/src/wedme/apply/a0.py +3 -0
- wedme_plots-2.0.5/src/wedme/apply/dev.py +3 -0
- wedme_plots-2.0.5/src/wedme/apply/paper.py +3 -0
- wedme_plots-2.0.5/src/wedme/apply/slides.py +3 -0
- wedme_plots-2.0.5/src/wedme/apply/thesis.py +3 -0
- wedme_plots-2.0.5/src/wedme/const.py +68 -0
- wedme_plots-2.0.5/src/wedme/gif.py +155 -0
- wedme_plots-2.0.5/src/wedme/saving.py +41 -0
- wedme_plots-2.0.5/src/wedme/scale/__init__.py +1 -0
- wedme_plots-2.0.5/src/wedme/scale/custom_func_scale.py +56 -0
- wedme_plots-2.0.5/src/wedme/shorthands.py +88 -0
- wedme_plots-2.0.5/src/wedme/styledefs.py +82 -0
- {wedme-plots-2.0.1/src/wedme → wedme_plots-2.0.5/src/wedme/stylesheets}/a0.mplstyle +2 -2
- {wedme-plots-2.0.1/src/wedme → wedme_plots-2.0.5/src/wedme/stylesheets}/common.mplstyle +9 -1
- wedme_plots-2.0.5/src/wedme/stylesheets/elspaper.mplstyle +47 -0
- {wedme-plots-2.0.1/src/wedme → wedme_plots-2.0.5/src/wedme/stylesheets}/slides.mplstyle +5 -3
- wedme-plots-2.0.1/src/wedme/elspaper.mplstyle → wedme_plots-2.0.5/src/wedme/stylesheets/thesis.mplstyle +1 -1
- wedme_plots-2.0.5/src/wedme/util.py +125 -0
- wedme_plots-2.0.5/src/wedme_plots.egg-info/PKG-INFO +186 -0
- wedme_plots-2.0.5/src/wedme_plots.egg-info/SOURCES.txt +28 -0
- {wedme-plots-2.0.1 → wedme_plots-2.0.5}/src/wedme_plots.egg-info/dependency_links.txt +0 -0
- {wedme-plots-2.0.1 → wedme_plots-2.0.5}/src/wedme_plots.egg-info/requires.txt +0 -0
- {wedme-plots-2.0.1 → wedme_plots-2.0.5}/src/wedme_plots.egg-info/top_level.txt +0 -0
- wedme-plots-2.0.1/MANIFEST.in +0 -1
- wedme-plots-2.0.1/PKG-INFO +0 -144
- wedme-plots-2.0.1/README.md +0 -99
- wedme-plots-2.0.1/src/wedme/const.py +0 -45
- wedme-plots-2.0.1/src/wedme/styledefs.py +0 -98
- wedme-plots-2.0.1/src/wedme/util.py +0 -45
- wedme-plots-2.0.1/src/wedme_plots.egg-info/PKG-INFO +0 -144
- wedme-plots-2.0.1/src/wedme_plots.egg-info/SOURCES.txt +0 -17
- {wedme-plots-2.0.1 → wedme_plots-2.0.5}/setup.cfg +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
include src/wedme/stylesheets/*.mplstyle
|
@@ -0,0 +1,186 @@
|
|
1
|
+
Metadata-Version: 2.2
|
2
|
+
Name: wedme-plots
|
3
|
+
Version: 2.0.5
|
4
|
+
Summary: We Don't Make Embarrassing Plots. Matplotlib styles for papers, posters, presentations and theses.
|
5
|
+
Author-email: Martijn Ruijzendaal <martijn.ruijzendaal@gmail.com>
|
6
|
+
License: MIT License
|
7
|
+
|
8
|
+
Copyright (c) 2023 Martijn Ruijzendaal
|
9
|
+
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
12
|
+
in the Software without restriction, including without limitation the rights
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
15
|
+
furnished to do so, subject to the following conditions:
|
16
|
+
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
18
|
+
copies or substantial portions of the Software.
|
19
|
+
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
26
|
+
SOFTWARE.
|
27
|
+
|
28
|
+
Project-URL: repository, https://github.com/mruijzendaal/wedme-plots
|
29
|
+
Project-URL: documentation, https://github.com/mruijzendaal/wedme-plots
|
30
|
+
Keywords: matplotlib
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
32
|
+
Classifier: Programming Language :: Python
|
33
|
+
Classifier: Programming Language :: Python :: 3
|
34
|
+
Requires-Python: >=3.7
|
35
|
+
Description-Content-Type: text/markdown
|
36
|
+
License-File: LICENSE
|
37
|
+
Requires-Dist: matplotlib
|
38
|
+
Requires-Dist: importlib_resources
|
39
|
+
Provides-Extra: build
|
40
|
+
Requires-Dist: build; extra == "build"
|
41
|
+
Requires-Dist: twine; extra == "build"
|
42
|
+
Provides-Extra: dev
|
43
|
+
Requires-Dist: black; extra == "dev"
|
44
|
+
Requires-Dist: pytest; extra == "dev"
|
45
|
+
|
46
|
+
# 👗 wedme-plots: We Don't Make Embarrassing Plots
|
47
|
+
|
48
|
+
Matplotlib styles for papers, posters, presentations and theses. Tailored for academic use.
|
49
|
+
|
50
|
+
## Too long; didn't read
|
51
|
+
1. Pick a style from `paper`, `slide`, `a0`, `thesis`.
|
52
|
+
2. Decide the final size of the figure on the chosen medium.
|
53
|
+
For example, the column width (`cw`) and default height (`dh`) for the `paper` style.
|
54
|
+
3. Import `wedme`
|
55
|
+
4. Before using matplotlib, apply the style using `wedme.apply.[style]_[width]_[height]()`. In the above example, `wedme.apply.paper_cw_dh()`.
|
56
|
+
|
57
|
+
|
58
|
+
## Installation
|
59
|
+
Install from [PyPI](https://pypi.org/project/wedme-plots/) using PIP:
|
60
|
+
```
|
61
|
+
pip install wedme-plots
|
62
|
+
```
|
63
|
+
|
64
|
+
For Anaconda users:
|
65
|
+
- Open an Anaconda Prompt
|
66
|
+
- Optional: if you use an environment other than `base`, open it using `conda activate [my environment]`
|
67
|
+
- Install pip using `conda install pip`
|
68
|
+
- Install wedme-plots using `pip install wedme-plots`
|
69
|
+
|
70
|
+
## Styling
|
71
|
+
Wedme offers multiple styles:
|
72
|
+
- `paper`: compatible with most journals (Nature, Science, Elsevier).
|
73
|
+
- `slide`: compatible with a (16:9) Microsoft Powerpoint presentation.
|
74
|
+
- `a0`: A0 poster.
|
75
|
+
- `thesis`: (work-in-progress) similar to `paper` but for a single-column B5 page.
|
76
|
+
|
77
|
+
## Usage
|
78
|
+
There are two ways of applying a wedme style.
|
79
|
+
|
80
|
+
### 1. Global styling and sizing
|
81
|
+
|
82
|
+
Import the `wedme` module and apply the desired style, e.g. `wedme.apply.paper()`.
|
83
|
+
This applies the specified style and its default size to every subsequent figure.
|
84
|
+
|
85
|
+
Example:
|
86
|
+
```python
|
87
|
+
import wedme
|
88
|
+
|
89
|
+
# These commands apply a style to subsequent Matplotlib figures.
|
90
|
+
|
91
|
+
# Pick one:
|
92
|
+
wedme.apply.paper() # For Elsevier-compatible paper styles
|
93
|
+
wedme.apply.slide() # For a 16:9 Powerpoint slide
|
94
|
+
wedme.apply.a0() # For A0 posters
|
95
|
+
wedme.apply.thesis() # For B5-paper thesis
|
96
|
+
|
97
|
+
# Optionally, change the default size:
|
98
|
+
wedme.apply.slide_tw_hh()
|
99
|
+
|
100
|
+
# Proceed with plotting as usual
|
101
|
+
import matplotlib.pyplot as plt
|
102
|
+
|
103
|
+
plt.figure()
|
104
|
+
plt.plot([1, 2, 3, 4])
|
105
|
+
plt.show()
|
106
|
+
```
|
107
|
+
|
108
|
+
### 2. Local styling and sizing
|
109
|
+
|
110
|
+
Alternatively, one can open figures in a specified style and size using the `wedme` drop-in replacements for `plt.figure()` and `plt.subplots()` as follows:
|
111
|
+
|
112
|
+
```python
|
113
|
+
import wedme
|
114
|
+
import matplotlib.pyplot as plt
|
115
|
+
|
116
|
+
# Open a figure in the `a0` style,
|
117
|
+
# with a size corresponding to half-width and half-height
|
118
|
+
wedme.figure.a0_hw_hh()
|
119
|
+
plt.plot([1, 2, 3, 4])
|
120
|
+
|
121
|
+
# Open a figure with two subplots in the `slide` style,
|
122
|
+
# with the figure size corresponding to half-width and half-height
|
123
|
+
fig, (ax1, ax2) = wedme.subplots.slide_hw_hh(1, 2)
|
124
|
+
|
125
|
+
ax1.plot([1, 2, 3, 4])
|
126
|
+
ax2.plot([1, 2, 3, 4])
|
127
|
+
```
|
128
|
+
|
129
|
+
Any arguments passed to `wedme.figure.[style]()` and `wedme.subplots.[style]()` are passed on to the equivalent matplotlib functions.
|
130
|
+
|
131
|
+
## Sizing
|
132
|
+
Matching the matplotlib figure size to the final display size of the figure is critically important: scaling to a different height will change font sizes and line widths.
|
133
|
+
|
134
|
+
We include the following breakpoints, with respect to the available width and height (`H`) of the chosen medium:
|
135
|
+
| Breakpoint | Ratio |
|
136
|
+
| -- | -- |
|
137
|
+
| `F` | 100% _("full")_ |
|
138
|
+
| `TT` | 2/3 _("two-thirds")_ |
|
139
|
+
| `H` | 1/2 _("half")_ |
|
140
|
+
| `FT` | 5/12 _("five-twelfth")_ |
|
141
|
+
| `T` | 1/3 _("third")_ |
|
142
|
+
| `Q` | 1/4 _("quarter")_ |
|
143
|
+
| `R` | 1/5 |
|
144
|
+
| `S` | 1/6 |
|
145
|
+
|
146
|
+
To specify the size using a breakpoint, append `W` (width) or `H` (height). For example, `wedme.figure.a0_hw_hh()` specifies the `a0` style, with a size of half the A0 width and height.
|
147
|
+
|
148
|
+
In addition to the breakpoints, some styles include custom sizes:
|
149
|
+
- `paper`: `CW` (column width) for two-column papers. `GH` is the height that corresponds to the golden-ratio of `CW`.
|
150
|
+
- `thesis`: `LFW` and `LFH` for the landscape full-width and full-height.
|
151
|
+
The default width and height `DW` and `DH` correspond to `CW` and `GH` of the `paper` style
|
152
|
+
|
153
|
+
</br>
|
154
|
+
|
155
|
+
When no size is specified, the following defaults sizes are used:
|
156
|
+
- `paper`: `(wedme.PAPER_CW, wedme.paper_GH)`
|
157
|
+
- `slide`: `(wedme.SLIDE_TTW, wedme.slide_TTH)`
|
158
|
+
- `a0`: `(wedme.A0_TW, ...)`
|
159
|
+
- `thesis`: `(wedme.THESIS_DW, wedme.paper_DH)`
|
160
|
+
|
161
|
+
|
162
|
+
## Powerpoint
|
163
|
+
Powerpoint automatically resizes artwork to a size that is different from the export size. To undo this:
|
164
|
+
- Insert the figure in Powerpoint.
|
165
|
+
- Picture Format > Reset Picture > Reset Picture & Size.
|
166
|
+
|
167
|
+
## Fonts
|
168
|
+
Sans-serif fonts are the standards for figures because they remain readable even when small or pixelated. Helvetica or Arial fonts are preferred by Nature, Science and Elsevier.
|
169
|
+
|
170
|
+
We try to find Helvetica, Arial, Verdana, Inter, Nimbus Sans on your system, in that order. The first font found will be applied to the figures.
|
171
|
+
|
172
|
+
## Exporting
|
173
|
+
`wedme` sets the `pdf.fonttype` parameter to `42` as recommended by Nature. This ensures that the text is editable even after exporting a pdf. `wedme` also changes the parameters `figure.autolayout` and `savefig.bbox` such that the specified sizes are respected.
|
174
|
+
|
175
|
+
For saving figures, consider using [pypdfplot](https://github.com/dcmvdbekerom/pypdfplot) to maintain the ability to later change how data is displayed. Of course, we don't make embarassing plots to begin with.
|
176
|
+
|
177
|
+
## Examples
|
178
|
+
<img src="https://github.com/mruijzendaal/wedme-plots/blob/main/img/calibration_curve_rot.png?raw=true" width="512">
|
179
|
+
|
180
|
+
## Utilities
|
181
|
+
Other than style sheets, wedme also includes utilities for commonly used operations.
|
182
|
+
- Creating animated GIF images from multiple plots. See `wedme.gif`
|
183
|
+
- Creating a legend only for unique entries. See `wedme.util.unique_legend()`
|
184
|
+
- Specifying the colorbar label easily. See `wedme.util.colorbar()`
|
185
|
+
- Specifying the minimum and maximum of the colorbar as a function of percentiles of the shown field. See `wedme.util.imshow()`
|
186
|
+
- Making plot colors dependent on some variable. See `weme.util.get_colormap_norm()`
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# 👗 wedme-plots: We Don't Make Embarrassing Plots
|
2
|
+
|
3
|
+
Matplotlib styles for papers, posters, presentations and theses. Tailored for academic use.
|
4
|
+
|
5
|
+
## Too long; didn't read
|
6
|
+
1. Pick a style from `paper`, `slide`, `a0`, `thesis`.
|
7
|
+
2. Decide the final size of the figure on the chosen medium.
|
8
|
+
For example, the column width (`cw`) and default height (`dh`) for the `paper` style.
|
9
|
+
3. Import `wedme`
|
10
|
+
4. Before using matplotlib, apply the style using `wedme.apply.[style]_[width]_[height]()`. In the above example, `wedme.apply.paper_cw_dh()`.
|
11
|
+
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
Install from [PyPI](https://pypi.org/project/wedme-plots/) using PIP:
|
15
|
+
```
|
16
|
+
pip install wedme-plots
|
17
|
+
```
|
18
|
+
|
19
|
+
For Anaconda users:
|
20
|
+
- Open an Anaconda Prompt
|
21
|
+
- Optional: if you use an environment other than `base`, open it using `conda activate [my environment]`
|
22
|
+
- Install pip using `conda install pip`
|
23
|
+
- Install wedme-plots using `pip install wedme-plots`
|
24
|
+
|
25
|
+
## Styling
|
26
|
+
Wedme offers multiple styles:
|
27
|
+
- `paper`: compatible with most journals (Nature, Science, Elsevier).
|
28
|
+
- `slide`: compatible with a (16:9) Microsoft Powerpoint presentation.
|
29
|
+
- `a0`: A0 poster.
|
30
|
+
- `thesis`: (work-in-progress) similar to `paper` but for a single-column B5 page.
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
There are two ways of applying a wedme style.
|
34
|
+
|
35
|
+
### 1. Global styling and sizing
|
36
|
+
|
37
|
+
Import the `wedme` module and apply the desired style, e.g. `wedme.apply.paper()`.
|
38
|
+
This applies the specified style and its default size to every subsequent figure.
|
39
|
+
|
40
|
+
Example:
|
41
|
+
```python
|
42
|
+
import wedme
|
43
|
+
|
44
|
+
# These commands apply a style to subsequent Matplotlib figures.
|
45
|
+
|
46
|
+
# Pick one:
|
47
|
+
wedme.apply.paper() # For Elsevier-compatible paper styles
|
48
|
+
wedme.apply.slide() # For a 16:9 Powerpoint slide
|
49
|
+
wedme.apply.a0() # For A0 posters
|
50
|
+
wedme.apply.thesis() # For B5-paper thesis
|
51
|
+
|
52
|
+
# Optionally, change the default size:
|
53
|
+
wedme.apply.slide_tw_hh()
|
54
|
+
|
55
|
+
# Proceed with plotting as usual
|
56
|
+
import matplotlib.pyplot as plt
|
57
|
+
|
58
|
+
plt.figure()
|
59
|
+
plt.plot([1, 2, 3, 4])
|
60
|
+
plt.show()
|
61
|
+
```
|
62
|
+
|
63
|
+
### 2. Local styling and sizing
|
64
|
+
|
65
|
+
Alternatively, one can open figures in a specified style and size using the `wedme` drop-in replacements for `plt.figure()` and `plt.subplots()` as follows:
|
66
|
+
|
67
|
+
```python
|
68
|
+
import wedme
|
69
|
+
import matplotlib.pyplot as plt
|
70
|
+
|
71
|
+
# Open a figure in the `a0` style,
|
72
|
+
# with a size corresponding to half-width and half-height
|
73
|
+
wedme.figure.a0_hw_hh()
|
74
|
+
plt.plot([1, 2, 3, 4])
|
75
|
+
|
76
|
+
# Open a figure with two subplots in the `slide` style,
|
77
|
+
# with the figure size corresponding to half-width and half-height
|
78
|
+
fig, (ax1, ax2) = wedme.subplots.slide_hw_hh(1, 2)
|
79
|
+
|
80
|
+
ax1.plot([1, 2, 3, 4])
|
81
|
+
ax2.plot([1, 2, 3, 4])
|
82
|
+
```
|
83
|
+
|
84
|
+
Any arguments passed to `wedme.figure.[style]()` and `wedme.subplots.[style]()` are passed on to the equivalent matplotlib functions.
|
85
|
+
|
86
|
+
## Sizing
|
87
|
+
Matching the matplotlib figure size to the final display size of the figure is critically important: scaling to a different height will change font sizes and line widths.
|
88
|
+
|
89
|
+
We include the following breakpoints, with respect to the available width and height (`H`) of the chosen medium:
|
90
|
+
| Breakpoint | Ratio |
|
91
|
+
| -- | -- |
|
92
|
+
| `F` | 100% _("full")_ |
|
93
|
+
| `TT` | 2/3 _("two-thirds")_ |
|
94
|
+
| `H` | 1/2 _("half")_ |
|
95
|
+
| `FT` | 5/12 _("five-twelfth")_ |
|
96
|
+
| `T` | 1/3 _("third")_ |
|
97
|
+
| `Q` | 1/4 _("quarter")_ |
|
98
|
+
| `R` | 1/5 |
|
99
|
+
| `S` | 1/6 |
|
100
|
+
|
101
|
+
To specify the size using a breakpoint, append `W` (width) or `H` (height). For example, `wedme.figure.a0_hw_hh()` specifies the `a0` style, with a size of half the A0 width and height.
|
102
|
+
|
103
|
+
In addition to the breakpoints, some styles include custom sizes:
|
104
|
+
- `paper`: `CW` (column width) for two-column papers. `GH` is the height that corresponds to the golden-ratio of `CW`.
|
105
|
+
- `thesis`: `LFW` and `LFH` for the landscape full-width and full-height.
|
106
|
+
The default width and height `DW` and `DH` correspond to `CW` and `GH` of the `paper` style
|
107
|
+
|
108
|
+
</br>
|
109
|
+
|
110
|
+
When no size is specified, the following defaults sizes are used:
|
111
|
+
- `paper`: `(wedme.PAPER_CW, wedme.paper_GH)`
|
112
|
+
- `slide`: `(wedme.SLIDE_TTW, wedme.slide_TTH)`
|
113
|
+
- `a0`: `(wedme.A0_TW, ...)`
|
114
|
+
- `thesis`: `(wedme.THESIS_DW, wedme.paper_DH)`
|
115
|
+
|
116
|
+
|
117
|
+
## Powerpoint
|
118
|
+
Powerpoint automatically resizes artwork to a size that is different from the export size. To undo this:
|
119
|
+
- Insert the figure in Powerpoint.
|
120
|
+
- Picture Format > Reset Picture > Reset Picture & Size.
|
121
|
+
|
122
|
+
## Fonts
|
123
|
+
Sans-serif fonts are the standards for figures because they remain readable even when small or pixelated. Helvetica or Arial fonts are preferred by Nature, Science and Elsevier.
|
124
|
+
|
125
|
+
We try to find Helvetica, Arial, Verdana, Inter, Nimbus Sans on your system, in that order. The first font found will be applied to the figures.
|
126
|
+
|
127
|
+
## Exporting
|
128
|
+
`wedme` sets the `pdf.fonttype` parameter to `42` as recommended by Nature. This ensures that the text is editable even after exporting a pdf. `wedme` also changes the parameters `figure.autolayout` and `savefig.bbox` such that the specified sizes are respected.
|
129
|
+
|
130
|
+
For saving figures, consider using [pypdfplot](https://github.com/dcmvdbekerom/pypdfplot) to maintain the ability to later change how data is displayed. Of course, we don't make embarassing plots to begin with.
|
131
|
+
|
132
|
+
## Examples
|
133
|
+
<img src="https://github.com/mruijzendaal/wedme-plots/blob/main/img/calibration_curve_rot.png?raw=true" width="512">
|
134
|
+
|
135
|
+
## Utilities
|
136
|
+
Other than style sheets, wedme also includes utilities for commonly used operations.
|
137
|
+
- Creating animated GIF images from multiple plots. See `wedme.gif`
|
138
|
+
- Creating a legend only for unique entries. See `wedme.util.unique_legend()`
|
139
|
+
- Specifying the colorbar label easily. See `wedme.util.colorbar()`
|
140
|
+
- Specifying the minimum and maximum of the colorbar as a function of percentiles of the shown field. See `wedme.util.imshow()`
|
141
|
+
- Making plot colors dependent on some variable. See `weme.util.get_colormap_norm()`
|
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = "wedme-plots"
|
7
|
-
version = "2.0.
|
8
|
-
description = "We Don't Make Embarrassing Plots. Matplotlib styles for papers, posters and
|
7
|
+
version = "2.0.5"
|
8
|
+
description = "We Don't Make Embarrassing Plots. Matplotlib styles for papers, posters, presentations and theses."
|
9
9
|
readme = "README.md"
|
10
10
|
authors = [{ name = "Martijn Ruijzendaal", email = "martijn.ruijzendaal@gmail.com" }]
|
11
11
|
license = { file = "LICENSE" }
|
@@ -8,6 +8,11 @@ Import the `wedme` module to apply styles:
|
|
8
8
|
"""
|
9
9
|
|
10
10
|
# Standard library imports
|
11
|
+
import wedme.util as util
|
11
12
|
from wedme.util import imshow, colorbar, get_colormap_norm, unique_legend
|
12
|
-
from wedme.styledefs import dev, paper, poster, slide,
|
13
|
+
from wedme.styledefs import reset, dev, paper, poster, slide, thesis, a0
|
14
|
+
from wedme.shorthands import figure, subplots, apply
|
13
15
|
from wedme.const import *
|
16
|
+
from wedme.gif import GifMaker
|
17
|
+
from wedme import scale
|
18
|
+
from wedme.saving import savefig
|
@@ -0,0 +1,68 @@
|
|
1
|
+
## Size definitions
|
2
|
+
_MM_TO_INCH = 0.0393700787
|
3
|
+
_PT_TO_MM = 0.3515
|
4
|
+
_PT_TO_INCH = _PT_TO_MM * _MM_TO_INCH
|
5
|
+
GOLDEN_RATIO = 1.61803398875
|
6
|
+
|
7
|
+
SIZE_SM = 30 * _MM_TO_INCH
|
8
|
+
SIZE_10 = 90 * _MM_TO_INCH
|
9
|
+
SIZE_15 = 140 * _MM_TO_INCH
|
10
|
+
SIZE_20 = 190 * _MM_TO_INCH
|
11
|
+
|
12
|
+
# Papers (Science, Nature, Elsevier) # TODO: split
|
13
|
+
# Width
|
14
|
+
PAPER_SCW = 121.095 * _MM_TO_INCH
|
15
|
+
PAPER_HSCW = PAPER_SCW / 2
|
16
|
+
PAPER_CW = 90 * _MM_TO_INCH
|
17
|
+
PAPER_FW = 190 * _MM_TO_INCH
|
18
|
+
# Height
|
19
|
+
PAPER_DH = PAPER_CW / GOLDEN_RATIO
|
20
|
+
PAPER_GH = PAPER_CW / GOLDEN_RATIO
|
21
|
+
PAPER_PH = 550 * _PT_TO_INCH
|
22
|
+
PAPER_PW = 345 * _PT_TO_INCH
|
23
|
+
|
24
|
+
# Thesis
|
25
|
+
THESIS_PW = 6.9 # page width
|
26
|
+
THESIS_PH = 9.8 # page height
|
27
|
+
THESIS_DW = 90 * _MM_TO_INCH # approximately half the page
|
28
|
+
THESIS_DH = THESIS_DW / GOLDEN_RATIO
|
29
|
+
# Landscape
|
30
|
+
THESIS_LFW = THESIS_PH # TODO: subtract margins
|
31
|
+
THESIS_LFH = THESIS_PW
|
32
|
+
|
33
|
+
# 16:9 slides
|
34
|
+
# Width
|
35
|
+
SLIDE_FW = 10
|
36
|
+
SLIDE_TTW = SLIDE_FW * 2 / 3
|
37
|
+
SLIDE_HW = SLIDE_FW / 2
|
38
|
+
SLIDE_FTW = SLIDE_FW * 5 / 12
|
39
|
+
SLIDE_TW = SLIDE_FW / 3
|
40
|
+
SLIDE_QW = SLIDE_FW / 4
|
41
|
+
SLIDE_RW = SLIDE_FW / 5
|
42
|
+
SLIDE_SW = SLIDE_FW / 6
|
43
|
+
# Height
|
44
|
+
SLIDE_FH = SLIDE_FW * 9 / 16
|
45
|
+
SLIDE_HH = SLIDE_FH / 2
|
46
|
+
SLIDE_TTH = SLIDE_FH * 2 / 3
|
47
|
+
SLIDE_FTH = SLIDE_FH * 5 / 12
|
48
|
+
SLIDE_TH = SLIDE_FH / 3
|
49
|
+
SLIDE_QH = SLIDE_FH / 4
|
50
|
+
SLIDE_RH = SLIDE_FH / 5
|
51
|
+
SLIDE_SH = SLIDE_FH / 6
|
52
|
+
|
53
|
+
|
54
|
+
# A0 poster
|
55
|
+
# Height
|
56
|
+
A0_FH = 46.8
|
57
|
+
A0_HH = A0_FH / 2
|
58
|
+
A0_TH = A0_FH / 3
|
59
|
+
A0_QH = A0_FH / 4
|
60
|
+
A0_RH = A0_FH / 5
|
61
|
+
A0_SH = A0_FH / 6
|
62
|
+
# Width
|
63
|
+
A0_FW = 33.1
|
64
|
+
A0_HW = A0_FW / 2
|
65
|
+
A0_TW = A0_FW / 3
|
66
|
+
A0_QW = A0_FW / 4
|
67
|
+
A0_RW = A0_FW / 5
|
68
|
+
A0_SW = A0_FW / 6
|
@@ -0,0 +1,155 @@
|
|
1
|
+
import tempfile
|
2
|
+
import subprocess
|
3
|
+
import matplotlib.pyplot as plt
|
4
|
+
|
5
|
+
|
6
|
+
def _run_command(command, silent=False, **kwargs):
|
7
|
+
"""Run a command while printing the live output"""
|
8
|
+
try:
|
9
|
+
process = subprocess.Popen(
|
10
|
+
command,
|
11
|
+
stdout=subprocess.PIPE,
|
12
|
+
stderr=subprocess.STDOUT,
|
13
|
+
**kwargs,
|
14
|
+
)
|
15
|
+
while True:
|
16
|
+
line = process.stdout.readline()
|
17
|
+
if not line and process.poll() is not None:
|
18
|
+
break
|
19
|
+
if not silent:
|
20
|
+
print(line.decode(), end="")
|
21
|
+
except Exception as e:
|
22
|
+
return None
|
23
|
+
return process.returncode
|
24
|
+
|
25
|
+
|
26
|
+
def create_gif(frames, outname, resize_fac=1.0, frametime_ms=800):
|
27
|
+
"""
|
28
|
+
Create a GIF from a list of frames.
|
29
|
+
|
30
|
+
Args:
|
31
|
+
frames (str): The path to the frames to be included in the GIF.
|
32
|
+
outname (str): The path and filename of the output GIF.
|
33
|
+
resize_fac (float, optional): The resize factor for the frames. Defaults to 1.0.
|
34
|
+
frametime_ms (int, optional): The time (in milliseconds) between frames. Defaults to 800.
|
35
|
+
|
36
|
+
Returns:
|
37
|
+
None
|
38
|
+
"""
|
39
|
+
|
40
|
+
delay_ms = round(frametime_ms / 10)
|
41
|
+
_run_command(
|
42
|
+
[
|
43
|
+
"convert",
|
44
|
+
"-resize",
|
45
|
+
f"{resize_fac*100:.0f}%",
|
46
|
+
"-delay",
|
47
|
+
f"{delay_ms:.0f}",
|
48
|
+
"-loop",
|
49
|
+
"0",
|
50
|
+
frames,
|
51
|
+
outname,
|
52
|
+
]
|
53
|
+
)
|
54
|
+
|
55
|
+
|
56
|
+
def _check_imagemagick():
|
57
|
+
return _run_command(["convert", "-version"], silent=True) == 0
|
58
|
+
|
59
|
+
|
60
|
+
class GifMaker(object):
|
61
|
+
"""Class for creating GIFs from matplotlib figures using ImageMagick.
|
62
|
+
|
63
|
+
Example usage:
|
64
|
+
```python
|
65
|
+
import wedme
|
66
|
+
import matplotlib.pyplot as plt
|
67
|
+
|
68
|
+
gif = wedme.GifMaker()
|
69
|
+
for i in range(10):
|
70
|
+
plt.plot(range(10), [i]*10)
|
71
|
+
gif.save_frame()
|
72
|
+
|
73
|
+
gif.close("test.gif")
|
74
|
+
```
|
75
|
+
"""
|
76
|
+
|
77
|
+
_is_open = False
|
78
|
+
_frames = []
|
79
|
+
_tempdir = None
|
80
|
+
|
81
|
+
def __init__(self):
|
82
|
+
self.clean()
|
83
|
+
if not _check_imagemagick():
|
84
|
+
raise RuntimeError(
|
85
|
+
"Cannot create GIFs without ImageMagick installed. Download from https://imagemagick.org/script/download.php"
|
86
|
+
)
|
87
|
+
|
88
|
+
def clean(self):
|
89
|
+
"""Removes any currently saved frames and resets the object to a clean state"""
|
90
|
+
if self._tempdir:
|
91
|
+
self._tempdir.cleanup()
|
92
|
+
else:
|
93
|
+
self._tempdir = tempfile.TemporaryDirectory()
|
94
|
+
|
95
|
+
self._frames = []
|
96
|
+
self._is_open = True
|
97
|
+
|
98
|
+
def save_frame(self, fig=None, name=None):
|
99
|
+
"""
|
100
|
+
Save a frame of the given figure as a PNG image.
|
101
|
+
|
102
|
+
Args:
|
103
|
+
fig (matplotlib.figure.Figure, optional): The figure to save. If not provided, the current figure will be used.
|
104
|
+
name (str, optional): The name of the saved image file. This will be used for ordering the frames in the GIF. If not provided, the order in which the frames are saved will be used.
|
105
|
+
|
106
|
+
Returns:
|
107
|
+
None
|
108
|
+
|
109
|
+
Raises:
|
110
|
+
None
|
111
|
+
"""
|
112
|
+
if fig is None:
|
113
|
+
fig = plt.gcf()
|
114
|
+
|
115
|
+
if name is None:
|
116
|
+
name = f"{len(self._frames):06d}"
|
117
|
+
name += ".png"
|
118
|
+
fig.savefig(f"{self._tempdir.name}/{name}")
|
119
|
+
self._frames.append(f"{self._tempdir.name}/{name}")
|
120
|
+
|
121
|
+
def save(self, outname, frametime_ms=800, resize_fac=1.0):
|
122
|
+
"""
|
123
|
+
Save the GIF animation. NB: This does not close the object, so you can continue adding frames after saving.
|
124
|
+
If this behaviour is not desired, call `close` instead.
|
125
|
+
|
126
|
+
Args:
|
127
|
+
outname (str): The output filename for the GIF.
|
128
|
+
frametime_ms (int, optional): The time in milliseconds between frames. Defaults to 800.
|
129
|
+
resize_fac (float, optional): The factor by which to resize the frames. Defaults to 1.0.
|
130
|
+
"""
|
131
|
+
create_gif(f"{self._tempdir.name}/*.png", outname, resize_fac, frametime_ms)
|
132
|
+
|
133
|
+
def close(self, outname):
|
134
|
+
"""
|
135
|
+
Closes the GIF file by saving it and performing any necessary cleanup.
|
136
|
+
|
137
|
+
Args:
|
138
|
+
outname (str): The name of the output file.
|
139
|
+
|
140
|
+
Returns:
|
141
|
+
None
|
142
|
+
"""
|
143
|
+
self.save(outname)
|
144
|
+
self.clean()
|
145
|
+
|
146
|
+
|
147
|
+
if __name__ == "__main__":
|
148
|
+
import matplotlib.pyplot as plt
|
149
|
+
|
150
|
+
gif = GifMaker()
|
151
|
+
for i in range(10):
|
152
|
+
plt.plot(range(10), [i] * 10)
|
153
|
+
gif.save_frame(plt)
|
154
|
+
|
155
|
+
gif.close("test.gif")
|
@@ -0,0 +1,41 @@
|
|
1
|
+
from typing import Union
|
2
|
+
import matplotlib.pyplot as plt
|
3
|
+
from matplotlib.figure import Figure
|
4
|
+
from pathlib import Path
|
5
|
+
from datetime import datetime
|
6
|
+
|
7
|
+
|
8
|
+
def savefig(
|
9
|
+
pathstub,
|
10
|
+
fig: Union[Figure, None] = None,
|
11
|
+
format=[".png", ".pdf"],
|
12
|
+
dpi=1000,
|
13
|
+
history=False,
|
14
|
+
):
|
15
|
+
# If multiple formats are given, recursively call this function for each
|
16
|
+
if type(format) == list:
|
17
|
+
for f in format:
|
18
|
+
savefig(pathstub, fig, f, dpi, history)
|
19
|
+
return
|
20
|
+
|
21
|
+
# If a figure handle is given, make sure it is displayed
|
22
|
+
if fig is not None:
|
23
|
+
fig.show()
|
24
|
+
|
25
|
+
path = Path(pathstub).with_suffix(format)
|
26
|
+
path.parent.mkdir(exist_ok=True)
|
27
|
+
|
28
|
+
# If history is enabled, save a copy of the figure in the history directory
|
29
|
+
if history:
|
30
|
+
dir_history = path.parent / "history"
|
31
|
+
|
32
|
+
savefig(
|
33
|
+
dir_history / f"{path.stem}_{datetime.now().strftime(r'%Y%m%d-%H%M%S')}",
|
34
|
+
fig,
|
35
|
+
format,
|
36
|
+
dpi,
|
37
|
+
history=False,
|
38
|
+
)
|
39
|
+
|
40
|
+
# Save the figure
|
41
|
+
plt.savefig(path, dpi=dpi, facecolor="#00f0", pad_inches=0)
|
@@ -0,0 +1 @@
|
|
1
|
+
from wedme.scale.custom_func_scale import CustomFuncScale
|