-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgfutils.py
1063 lines (852 loc) · 37.4 KB
/
pgfutils.py
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Blair Bonnett
# SPDX-License-Identifier: BSD-3-Clause
"""Utilities for generating PGF plots from matplotlib.
From version 1.2, matplotlib has included a PGF backend. This exports figures as drawing
commands for the PGF drawing package for LaTeX, allowing LaTeX to typeset the image.
This module provides some utilities to simplify creating PGF figures from scripts (so
they can be processed via Makefiles) in order to get consistent-looking plots.
"""
__version__ = "1.8.0.dev0"
# We don't import Matplotlib here as this brings in NumPy. In turn, NumPy caches a
# reference to the io.open() method as part of its data loading functions. This means we
# can't (reliably) wrap all loading functions if we're asked to track opened files.
# Instead, the tracking is installed in setup_figure() before the first import of
# Matplotlib (assuming the user doesn't use our internal functions). Python's caching of
# imported modules means there should be minimal impact from importing Matplotlib
# separately in different functions.
import ast
import configparser
import importlib.abc
import importlib.util
import inspect
import io
import os
import pathlib
import re
import string
import sys
class DimensionError(ValueError):
"""A configuration entry could not be converted to a dimension."""
pass
class ColorError(ValueError):
"""A configuration entry could not be converted to a color."""
pass
class PgfutilsParser(configparser.ConfigParser):
"""Custom configuration parser with Matplotlib dimension and color support."""
# Regular expression to parse a dimension into size and (optional) unit.
_dimre = re.compile(r"^\s*(?P<size>\d+(?:\.\d*)?)\s*(?P<unit>.+?)?\s*$")
# Conversion factors (divisors) to go from the given unit to inches.
_dimconv = {
"cm": 2.54,
"centimetre": 2.54,
"centimeter": 2.54,
"centimetres": 2.54,
"centimeters": 2.54,
"mm": 25.4,
"millimetre": 25.4,
"millimeter": 25.4,
"millimetres": 25.4,
"millimeters": 25.4,
"in": 1,
"inch": 1,
"inches": 1,
"pt": 72.27, # Printers points, not the 1/72 Postscript/PDF points.
"point": 72.27,
"points": 72.27,
}
def read(self, filename, encoding=None):
"""Read configuration options from a file.
This is an extension of the standard ConfigParser.read() method to reject
unknown options. This provides an early indication to the user that they have
configured something incorrectly, rather than continuing and having their plot
generated differently to how they intended it.
"""
def get_options():
options = set()
for sect, opts in self._sections.items():
if sect == "rcParams":
continue
options.update(f"{sect}.{opt}" for opt in opts.keys())
return options
# Get the options before and after reading.
before = get_options()
result = super().read(filename, encoding)
after = get_options()
# If there's a difference, thats an error.
diff = after.difference(before)
if diff:
if len(diff) == 1:
raise KeyError(f"{filename}: unknown option {diff.pop()}")
raise KeyError(f"{filename}: unknown options {', '.join(diff)}")
# Otherwise we're OK to continue.
return result
def read_kwargs(self, **kwargs):
"""Read configuration options from keyword arguments."""
# Dictionary of values to load.
d = {}
# Option -> section lookup table.
lookup = {}
# Go through all existing options.
for section, options in self._sections.items():
# Can't specify rcParams through kwargs.
if section == "rcParams":
continue
# Add to our tables.
d[section] = {}
for option in options.keys():
lookup[option] = section
# Go through all given arguments.
for key, value in kwargs.items():
# Check this option already exists.
section = lookup.get(key, None)
if section is None:
raise KeyError(f"Unknown configuration option {key}.")
# Save it.
d[section][key] = value
# And then read the dictionary in.
return self.read_dict(d)
def parsedimension(self, dim):
"""Convert a dimension to inches.
The dimension should be in the format '<value><unit>', where the unit can be
'mm', 'cm', 'in', or 'pt'. If no unit is specified, it is assumed to be in
inches. Note that points refer to TeX points (72.27 per inch) rather than
Postscript points (72 per inch).
Parameters
----------
dim: string
The dimension to parse.
Returns
-------
float: The dimension in inches.
Raises
------
DimensionError:
The dimension is empty or not recognised.
"""
# Need a string.
if dim is None:
raise DimensionError("Cannot be set to an empty value.")
# Check for an empty string.
dim = dim.strip().lower()
if not dim:
raise DimensionError("Cannot be set to an empty value.")
# Try to parse it.
m = self._dimre.match(dim)
if not m:
raise DimensionError(f"Could not parse {dim} as a dimension.")
# Pull out the pieces.
groups = m.groupdict()
size = float(groups["size"])
unit = groups.get("unit", "") or ""
unit = unit.lower()
# No unit: already in inches.
if not unit:
return size
# Pick out the divisor to convert into inches.
factor = self._dimconv.get(unit, None)
# Unknown unit.
if factor is None:
raise DimensionError(f"Unknown unit {unit}.")
# Do the conversion.
return size / factor
def getdimension(self, section, option, **kwargs):
"""Return a configuration entry as a dimension in inches.
The dimension should be in the format '<value><unit>', where the unit can be
'mm', 'cm', 'in', or 'pt'. If no unit is specified, it is assumed to be in
inches. Note that points refer to TeX points (72.27 per inch) rather than
Postscript points (72 per inch).
Parameters
----------
section, option: string
The section and option keys to retrieve.
Returns
-------
float: The dimension in inches.
Raises
------
DimensionError:
The dimension is empty or not recognised.
"""
# Get the string version of the dimension.
dim = self.get(section, option, **kwargs)
# And parse it; modify any parsing exception to include
# the section and option we were parsing.
try:
return self.parsedimension(dim)
except DimensionError as e:
raise DimensionError(f"{section}.{option}: {e}") from None
def getcolor(self, section, option, **kwargs):
"""Return a configuration entry as a Matplotlib color.
Recognised color formats are:
* Named colors (red, yellow, etc.)
* Cycle colors (C1, C2 etc.)
* Tuples (r, g, b) or (r, g, b, a) with floating-point entries in [0, 1]
* A floating-point value in [0, 1] for grayscale
* 'none', 'transparent', or an empty value for transparent
Parameters
----------
section, option: string
The section and option keys to retrieve.
Returns
-------
matplotlib-compatible colour.
Raises
------
ColorError
The value could not be interpreted as a color.
"""
import matplotlib
# Retrieve the string value. Empty values are interpreted as none.
value = self.get(section, option, **kwargs).strip() or "none"
# Transparent.
if value in {"none", "transparent"}:
return "none"
# Single floating point number: grayscale.
try:
gray = float(value)
except ValueError:
pass
else:
if not (0 <= gray <= 1):
raise ColorError(
f"{section}.{option}: greyscale floats must be in [0, 1]."
)
# For historical reasons Matlotlib requires this to be a string.
return value
# Nth color in the cycle (i.e., C1, C2 etc), or a named color. Unfortunately,
# this returns True for grayscale values outside [0, 1] so we have to do our own
# check above.
if matplotlib.colors.is_color_like(value):
return value
# Anything else we accept is valid Python syntax, so parse it.
try:
parsed = ast.literal_eval(value)
except (SyntaxError, TypeError, ValueError):
raise ColorError(
f"{section}.{option}: could not interpret '{value}' as a color."
) from None
# Needs to be a list or tuple of channel values.
if not isinstance(parsed, (list, tuple)):
raise ColorError(
f"{section}.{option}: could not interpret '{value}' as a color."
)
# Filter out Booleans which Matplotlib would treat as 0 or 1.
if any(isinstance(entry, bool) for entry in parsed):
raise ColorError(
f"{section}.{option}: could not interpret '{value}' as a color."
)
# And get Matplotlib to convert to a color.
try:
return matplotlib.colors.to_rgba(parsed)
except ValueError as e:
raise ColorError(f"{section}.{option}: {e}.") from None
def in_tracking_dir(self, type, fn):
"""Check if a file is in a tracking directory.
Parameters
----------
type : {"data", "import"}
The type of file to check for.
fn : path-like
The filename to check.
Returns
-------
Boolean
True if the file is in one of the corresponding tracking directories
specified in the configuration.
"""
if type == "data":
paths = self.get("paths", "data").strip().splitlines()
elif type == "import":
paths = self.get("paths", "pythonpath").strip().splitlines()
paths.extend(self.get("paths", "extra_imports").strip().splitlines())
else:
raise ValueError(f"Unknown tracking type {type}.")
# If we can compute a relative path, it must be within the directory.
fn = pathlib.Path(fn).resolve()
for path in paths:
path = pathlib.Path(path).resolve()
try:
fn.relative_to(path)
except ValueError:
continue
return True
# Not in any of the directories.
return False
# The current configuration.
_config = PgfutilsParser()
def _config_reset():
"""Internal: reset the configuration to the default state."""
global _config
_config.clear()
_config.read_dict(
{
"tex": {
"engine": "xelatex",
"text_width": "345 points",
"text_height": "550 points",
"marginpar_width": "65 points",
"marginpar_sep": "11 points",
"num_columns": "1",
"columnsep": "10 points",
},
"pgfutils": {
"preamble": "",
"preamble_substitute": "false",
"font_family": "serif",
"font_name": "",
"font_size": "10",
"legend_font_size": "10",
"line_width": "1",
"axes_line_width": "0.6",
"legend_border_width": "0.6",
"legend_border_color": "(0.8, 0.8, 0.8)",
"legend_background": "(1, 1, 1)",
"legend_opacity": 0.8,
"figure_background": "",
"axes_background": "white",
"extra_tracking": "",
"environment": "",
},
"paths": {
"data": ".",
"pythonpath": "",
"extra_imports": "",
},
"rcParams": {},
"postprocessing": {
"fix_raster_paths": "true",
"tikzpicture": "false",
},
}
)
def _relative_if_subdir(fn):
"""Internal: get a relative or absolute path as appropriate.
Parameters
----------
fn : path-like
A path to convert.
Returns
-------
fn : str
A relative path if the file is underneath the top-level project directory, or an
absolute path otherwise.
"""
fn = pathlib.Path(fn).resolve()
try:
return fn.relative_to(pathlib.Path.cwd())
except ValueError:
return fn
def _file_tracker(to_wrap):
"""Internal: install an opened file tracker.
To be trackable, the given callable should return an instance of a subclass of
`io.IOBase`. If its `writable` method returns True, it is assumed to be an output
file, otherwise if its `readable` method returns True it is assumed to be an input.
The docstring, name etc. of the given callable are copied to the wrapper function
that performs the tracking.
The set of files that have been opened so far are stored in the `filenames`
attribute of this function. In general, use the _list_opened_files() method.
Parameters
----------
to_wrap: callable
The callable to install the tracker around.
Returns
-------
Wrapper function.
"""
global _config
import functools
@functools.wraps(to_wrap)
def wrapper(*args, **kwargs):
# Defer opening to the real function.
file = to_wrap(*args, **kwargs)
# Only attempt to track files implemented as standard IO objects.
if not isinstance(file, io.IOBase):
return file
# Integers indicate file objects that were created from descriptors (e.g.,
# opened with the low-level os.open() and then wrapped with os.fdopen()). There
# is no reliable way to retrieve the filename; on Linux, the command 'readlink
# /proc/self/fd/N' can be used provided it hasn't been renamed/deleted since.
# I'm leaving this as unimplemented until I find an actual usecase.
if isinstance(file.name, int):
return file
# If a file is writeable; this includes files opened in r+ or append modes. We
# assume these can't be dependencies.
if file.writable():
# Does it match the filename pattern used by the PGF backend for rasterised
# parts of the image being saved as PNGs?
if re.match(r"^.+-img\d+.png$", file.name):
_file_tracker.filenames.add(("w", _relative_if_subdir(file.name)))
# Should always be readable in this case, but check anyway.
elif file.readable() and _config.in_tracking_dir("data", file.name):
_file_tracker.filenames.add(("r", _relative_if_subdir(file.name)))
# Done.
return file
# Return the wrapper function.
return wrapper
# Initialise the set of opened files.
_file_tracker.filenames = set()
class ImportTracker(importlib.abc.MetaPathFinder):
"""Import finder which tracks imported files in configured paths."""
def __init__(self):
super().__init__()
self._avoid_recursion = set()
self.imported = set()
def find_spec(self, fullname, path, target=None):
# According to PEP451, this is mostly intended for a reload. I can't see a way
# (without calling importlib._bootstrap._find_spec, which should not be imported
# according to the note at the top of the module) to pass this information on.
# Hence, lets skip tracking in this case.
if target is not None: # pragma: no cover
return None
# We use importlib to find the actual spec, so we need to avoid recursing when
# this finder is called again.
if fullname in self._avoid_recursion:
return None
# Find the spec.
self._avoid_recursion.add(fullname)
spec = importlib.util._find_spec_from_path(fullname, path)
self._avoid_recursion.remove(fullname)
# If it has an origin in one of our tracked dirs, log it.
if spec is not None and spec.origin is not None and spec.origin != "built-in":
global _config
if _config.in_tracking_dir("import", spec.origin):
_file_tracker.filenames.add(("r", _relative_if_subdir(spec.origin)))
# And return the result.
return spec
def _install_standard_file_trackers():
"""Internal: install standard file trackers in likely locations.
This wraps the standard open() function as well as the io.open() function. This
seems to catch all uses of the standard NumPy `load` (for both .npy and .npz files)
and `loadtxt`, and should also work for most libraries which use Python functions to
open files. It won't work for anything which uses low-level methods, either through
the `os` module or at C level in compiled modules.
Note: this needs to be run prior to importing NumPy (and therefore prior to
importing Matplotlib) as NumPy's data loader module stores a reference to io.open
which wouldn't get wrapped.
"""
import builtins
import io
builtins.open = _file_tracker(builtins.open)
io.open = _file_tracker(io.open)
if hasattr(pathlib, "_normal_accessor"):
pathlib._normal_accessor.open = _file_tracker(pathlib._normal_accessor.open)
sys.meta_path.insert(0, ImportTracker())
def _install_extra_file_trackers(trackers):
"""Internal: install requested extra file trackers.
Parameters
----------
trackers : list
List of strings containing the names of the extra trackers to install. Names are
not case-sensitive. Currently only "netCDF4" is supported.
"""
for tracker in trackers:
tracker = tracker.strip().lower()
# netCDF4 data storage.
if tracker == "netcdf4":
import netCDF4
# Wrap the Dataset class to modify its initialiser to track read files. The
# class is part of the compiled extension so we can't just override the
# initialiser.
class PgfutilsTrackedDataset(netCDF4.Dataset):
def __init__(self, filename, mode="r", **kwargs):
super().__init__(filename, mode=mode, **kwargs)
if mode == "r":
_file_tracker.filenames.add(
("r", _relative_if_subdir(filename))
)
netCDF4.Dataset = PgfutilsTrackedDataset
# Same deal for the MFDataset (multiple files read as one dataset).
class PgfutilsTrackedMFDataset(netCDF4.MFDataset):
def __init__(self, files, *args, **kwargs):
super().__init__(files, *args, **kwargs)
# Single string: glob pattern to expand.
if isinstance(files, str):
import glob
files = glob.glob(files)
# And track them all.
for fn in files:
_file_tracker.filenames.add(("r", _relative_if_subdir(fn)))
netCDF4.MFDataset = PgfutilsTrackedMFDataset
else:
raise ValueError(f"Unknown extra tracker {tracker}.")
def add_dependencies(*args):
"""Add a file dependency for the current figure.
In most situations, the in-built file tracking should suffice. However, some files
can be missed (for example, if a library you are using has its file opening routines
in a compiled extension rather than using Python functions). This function can be
used to manually add these files to the dependencies of the current figure.
Parameters
----------
fn : path-like
The filename of the dependency, relative to the top-level directory of the
project.
"""
for fn in args:
_file_tracker.filenames.add(("r", pathlib.Path(fn)))
def _list_opened_files():
"""Internal: get project files which were opened by the script.
Returns
-------
list
A list of tuples (mode, filename) of files in or under the current directory
where each filename is a pathlib.Path instance. Entries with mode 'r' were
opened for reading and are assumed to be dependencies of the generated figure.
Entries with mode 'w' were opened for writing and are rasterised graphics
included in the final figure. The list is sorted by mode and then filename.
"""
return sorted(_file_tracker.filenames)
# If the script has been run in an interactive mode (currently, if it is running under
# IPython in a mode with an event loop) then we will display the figure in the save()
# call rather than saving it. Interactivity is tested each time setup_figure() is
# called. This global stores the latest result to avoid running the test twice (we need
# it in setup_figure() to avoid setting the backend if interactive, and then in save()).
_interactive = False
def setup_figure(
width=1.0, height=1.0, columns=None, margin=False, full_width=False, **kwargs
):
"""Set up matplotlib figures for PGF output.
This function should be imported and run before you import any matplotlib code.
Figure properties can be passed in as keyword arguments to override the project
configuration.
Parameters
----------
width, height : float or string
If a float, the fraction of the corresponding text width or height that the
figure should take up. If a string, a dimension in centimetres (cm), millimetres
(mm), inches (in) or points (pt). For example, '3in' or '2.5 cm'.
columns : integer, optional
The number of columns the figure should span. This should be between 1 and the
total number of columns in the document (as specified in the configuration). A
value of None corresponds to spanning all columns. Any other value results in a
ValueError being raised.
margin : Boolean, default False
If True, a margin figure (i.e., one to fit within the margin notes in the
document) is generated. If the width is a fraction, it is treated as a fraction
of the marginpar_width configuration setting. The height is still treated as a
fraction of the text height. The columns setting is ignored if this is True.
full_width : Boolean, default False
If True, a full-width figure, i.e., one spanning the main text, the margin
notes, and the separator between them, is generated. A fractional width is
treated relative to the full width. The height is still treated as a fraction of
the text height. The columns and margin parameters are ignored if this is True.
"""
global _config, _interactive
# Reset the configuration.
_config_reset()
# Load configuration from a local file if one exists.
cfg_path = pathlib.Path("pgfutils.cfg")
if cfg_path.exists():
if not cfg_path.is_file():
raise RuntimeError("pgfutils.cfg exists but is not a file.")
_config.read(cfg_path)
_file_tracker.filenames.add(("r", cfg_path))
# And anything given in the function call.
if kwargs:
_config.read_kwargs(**kwargs)
# Set environment variables specified in the configuration.
for line in _config["pgfutils"]["environment"].splitlines():
line = line.strip()
if not line:
continue
# Check the variables are formatted correctly.
if "=" not in line:
raise ValueError(
"Environment variables should be in the form NAME=VALUE. "
f"The line '{line}' does not match this."
)
# And set them.
key, value = line.split("=", 1)
os.environ[key.strip()] = value.strip()
# Install file trackers if desired. This must be done before anything which imports
# Matplotlib.
if "PGFUTILS_TRACK_FILES" in os.environ:
_install_standard_file_trackers()
extra = _config["pgfutils"]["extra_tracking"].strip()
if extra:
_install_extra_file_trackers(extra.split(","))
# Reset our interactive flag on each call.
_interactive = False
# If we're running under IPython, see if there is an event loop in use.
# Unfortunately, the matplotlib.rcParams['interactive'] (or equivalently,
# matplotlib.is_interactive()) don't appear to propagate when IPython runs a script
# so we can't test those.
try:
ipython = get_ipython()
except NameError:
pass
else:
if ipython.active_eventloop is not None: # pragma: no cover
_interactive = True
# Add any desired entries to sys.path.
for newpath in _config["paths"]["pythonpath"].splitlines():
newpath = newpath.strip()
if newpath:
sys.path.insert(0, newpath)
# We're now ready to start configuring Matplotlib.
import matplotlib
# Set the backend. We don't want to overwrite the current backend if this is an
# interactive run as the PGF backend does not implement a GUI.
if not _interactive: # pragma: no cover
matplotlib.use("pgf")
# Specify which TeX engine we are using.
matplotlib.rcParams["pgf.texsystem"] = _config["tex"]["engine"]
# Custom TeX preamble.
preamble = _config["pgfutils"]["preamble"]
if _config["pgfutils"].getboolean("preamble_substitute"):
preamble = string.Template(preamble).substitute(basedir=str(pathlib.Path.cwd()))
matplotlib.rcParams["pgf.preamble"] = preamble
# Clear the existing lists of specific font names.
matplotlib.rcParams["font.sans-serif"] = []
matplotlib.rcParams["font.serif"] = []
matplotlib.rcParams["font.cursive"] = []
matplotlib.rcParams["font.monospace"] = []
matplotlib.rcParams["font.fantasy"] = []
# Don't let the backend try to load fonts.
matplotlib.rcParams["pgf.rcfonts"] = False
# Set the font family in use.
matplotlib.rcParams["font.family"] = _config["pgfutils"]["font_family"]
# If a specific font was given, add it to the list of fonts for
# the chosen font family.
if _config["pgfutils"]["font_name"]:
k = f"font.{_config['pgfutils']['font_family']}"
matplotlib.rcParams[k].append(_config["pgfutils"]["font_name"])
# Set the font sizes.
matplotlib.rcParams["font.size"] = _config["pgfutils"].getfloat("font_size")
matplotlib.rcParams["legend.fontsize"] = _config["pgfutils"].getfloat(
"legend_font_size"
)
# Don't use Unicode in the figures. If this is not disabled, the PGF backend can
# replace some characters with unicode variants, and these don't always play nicely
# with some TeX engines and/or fonts.
matplotlib.rcParams["axes.unicode_minus"] = False
# Line widths.
matplotlib.rcParams["axes.linewidth"] = _config["pgfutils"].getfloat(
"axes_line_width"
)
matplotlib.rcParams["lines.linewidth"] = _config["pgfutils"].getfloat("line_width")
# Colours.
matplotlib.rcParams["figure.facecolor"] = _config["pgfutils"].getcolor(
"figure_background"
)
matplotlib.rcParams["savefig.facecolor"] = _config["pgfutils"].getcolor(
"figure_background"
)
matplotlib.rcParams["axes.facecolor"] = _config["pgfutils"].getcolor(
"axes_background"
)
# Now we need to figure out the total width available for the figure, i.e., the
# width corresponding to the figure parameter being 1. First, look up some document
# properties.
text_width = _config["tex"].getdimension("text_width")
margin_width = _config["tex"].getdimension("marginpar_width")
margin_sep = _config["tex"].getdimension("marginpar_sep")
num_columns = _config["tex"].getint("num_columns")
# Full-width figure.
if full_width:
available_width = text_width + margin_sep + margin_width
# Making a margin figure.
elif margin:
available_width = margin_width
# Columns not specified, or spanning all available.
elif columns is None or columns == num_columns:
available_width = text_width
# More columns than present in the document.
elif columns > num_columns:
raise ValueError(
f"Document has {num_columns} columns, but you asked for a figure spanning "
f"{columns} columns."
)
# Not sure what this would mean.
elif columns < 1:
raise ValueError("Number of columns must be at least one.")
# A number of columns less than the total.
else:
# Figure out the width of each column. N columns have N - 1 separators between
# them.
columnsep = _config["tex"].getdimension("columnsep")
total_columnsep = columnsep * (num_columns - 1)
total_columnw = text_width - total_columnsep
column_width = total_columnw / num_columns
# And the width (including the separators between them) of the requested number
# of columns.
available_width = (column_width * columns) + (columnsep * (columns - 1))
# And now we can calculate the actual figure width. If it is a float, it is a
# fraction of the total available width. Otherwise, assume it's an explicit
# dimension.
try:
w = float(width)
except ValueError:
w = _config.parsedimension(width)
else:
w *= available_width
# And the figure height.
try:
h = float(height)
except ValueError:
h = _config.parsedimension(height)
else:
h *= _config["tex"].getdimension("text_height")
# Set the figure size.
matplotlib.rcParams["figure.figsize"] = [w, h]
# Ask for a tight layout (i.e., minimal padding).
matplotlib.rcParams["figure.autolayout"] = True
# Copy any specific rcParams the user set.
matplotlib.rcParams.update(_config["rcParams"])
def save(figure=None):
"""Save the figure.
The filename is based on the name of the script which calls this function. For
example, if you call save() from a script named sine.py, then the saved figure will
be sine.pgf.
Parameters
----------
figure: matplotlib Figure object, optional
If not given, then the current figure as returned by matplotlib.pyplot.gcf()
will be saved.
"""
global _config, _interactive
from matplotlib import __version__ as mpl_version, pyplot as plt
# Get the current figure if needed.
if figure is None:
figure = plt.gcf()
# Go through and fix up a few little quirks on the axes within this figure.
for axes in figure.get_axes():
# There are no rcParams for the legend properties. Go through and set these
# directly before we save.
legend = axes.get_legend()
if legend:
frame = legend.get_frame()
frame.set_linewidth(_config["pgfutils"].getfloat("legend_border_width"))
frame.set_alpha(_config["pgfutils"].getfloat("legend_opacity"))
frame.set_ec(_config["pgfutils"].getcolor("legend_border_color"))
frame.set_fc(_config["pgfutils"].getcolor("legend_background"))
# Some PDF viewers show white lines through vector colorbars. This is a bug in
# the viewers, but can be worked around by forcing the edge of the patches in
# the colorbar to have the same colour as their faces. This doesn't work with
# partially transparent (alpha < 1) images. As of matplotlib v1.5.0, colorbars
# with many patches (> 50 by default) are rasterized and included as PNGs so
# this workaround is not needed in most cases.
#
# http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.colorbar
# https://github.com/matplotlib/matplotlib/issues/1188
# https://github.com/matplotlib/matplotlib/pull/4481
# https://github.com/matplotlib/matplotlib/commit/854f74a
# Gather a set of colorbars. This includes colorbars from images (imshow etc),
# collections (e.g., scatter plots), and contour plot lines (the roundabout
# _current_image way -- there doesn't appear to be any other reference from the
# axes to the QuadContourSet object).
colorbars = set()
colorbars.update(im.colorbar for im in axes.images if im.colorbar)
colorbars.update(coll.colorbar for coll in axes.collections if coll.colorbar)
if axes._current_image and axes._current_image.colorbar:
colorbars.add(axes._current_image.colorbar)
# And process them.
for cb in colorbars:
if not cb.solids:
continue
# Ignore rasterized or transparent colorbars.
# Note that alpha=None is the same as alpha=1.
if cb.solids.get_rasterized():
continue
if (cb.alpha or 1.0) < 1.0:
continue
cb.solids.set_edgecolor("face")
# Interactive mode: show the figure rather than saving.
if _interactive: # pragma: no cover
plt.show()
return
# Look at the next frame up for the name of the calling script.
script = pathlib.Path(inspect.getfile(sys._getframe(1)))
# The initial Matplotlib output file, and the final figure file.
mpname = script.with_suffix(".pgf")
figname = script.with_suffix(".pypgf")
# Get Matplotlib to save it.
figure.savefig(mpname)
# We have been tracking opened files and need to
# output our results.
if "PGFUTILS_TRACK_FILES" in os.environ:
# The files.
files = "\n".join([f"{mode}:{fn}" for mode, fn in _list_opened_files()])
# Figure out where to print it.
dest = os.environ.get("PGFUTILS_TRACK_FILES") or "1"
# stdout.
if dest == "1":
sys.stdout.write(files)
# stderr.
elif dest == "2":
sys.stderr.write(files)
# A named file.
else:
with open(dest, "w") as f:
f.write(files)
# List of all postprocessing functions we are running on this figure. Each should
# take in a single line as a string, and return the line with any required
# modifications.
pp_funcs = []
# Local cache of postprocessing options.
fix_raster_paths = _config["postprocessing"].getboolean("fix_raster_paths")
tikzpicture = _config["postprocessing"].getboolean("tikzpicture")
# Add the appropriate directory prefix to all raster images
# included via \pgfimage.
if fix_raster_paths:
figdir = figname.parent
# Only apply this if the figure is not in the top-level directory.
if not figdir.samefile("."):
prefix = figdir.relative_to(pathlib.Path.cwd())
expr = re.compile(r"(\\(?:pgfimage|includegraphics)(?:\[.+?\])?{)(.+?)}")
repl = rf"\1{prefix}/\2}}"
pp_funcs.append(lambda s: re.sub(expr, repl, s))
# Use the tikzpicture environment rather than pgfpicture.
if tikzpicture: