def test_setup_both_dimensions(self): """setup_figure() with both sizes specific dimensions...""" # Dimensions and their size in inches. dims = { '1in': 1, '2.5 inch': 2.5, '3 inches': 3, '2.54cm': 1, '1 centimetre': 0.3937, '8 centimetres': 3.14961, '1.0 centimeter': 0.3937, '8.0 centimeters': 3.14961, '80mm': 3.14961, '200 millimetre': 7.8740, '123.4 millimetres': 4.8583, '200 millimeter': 7.8740, '123.4 millimeters': 4.8583, '340pt': 4.7046, '120point': 1.6604, '960 points': 13.2835, } # Check each combination. for wstr, winch in dims.items(): for hstr, hinch in dims.items(): _config_reset() setup_figure(width=wstr, height=hstr) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(winch, rel=1e-3) assert h == approx(hinch, rel=1e-3)
def test_setup_height_fraction(self): """setup_figure() with specific width and height as a fraction...""" # Dimensions and their size in inches. dims = { '1in': 1, '2.5 inch': 2.5, '3 inches': 3, '2.54cm': 1, '1 centimetre': 0.3937, '8 centimetres': 3.14961, '1.0 centimeter': 0.3937, '8.0 centimeters': 3.14961, '80mm': 3.14961, '200 millimetre': 7.8740, '123.4 millimetres': 4.8583, '200 millimeter': 7.8740, '123.4 millimeters': 4.8583, '340pt': 4.7046, '120point': 1.6604, '960 points': 13.2835, } # Check various combinations. for wstr, winch in dims.items(): for n in np.linspace(0.3, 2.5, 17): _config_reset() setup_figure(width=wstr, height=n) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(winch, rel=1e-3) assert h == approx(n * _config['tex'].getdimension('text_height'))
def test_save_with_figure(self): """Test save() works when given an explicit figure instance...""" setup_figure(width=1, height=1) from matplotlib import pyplot as plt fig = plt.figure() save(fig) os.unlink("tests/test_misc.pypgf")
def test_setup_arg_priority(self): """Test priority of columns/margin/full_width arguments to setup_figure()...""" setup_figure() text = _config['tex'].getdimension('text_width') sep = _config['tex'].getdimension('marginpar_sep') margin = _config['tex'].getdimension('marginpar_width') full = text + sep + margin height = _config['tex'].getdimension('text_height') # All three: full width should take priority. _config_reset() setup_figure(width=1, height=0.4, columns=1, margin=True, full_width=True) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(full) assert h == approx(0.4 * height) # Margin and full width: full width should take priority. _config_reset() setup_figure(width=1, height=0.4, margin=True, full_width=True) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(full) assert h == approx(0.4 * height) # Margin and columns: margin should take priority. _config_reset() setup_figure(width=1, height=0.4, columns=1, margin=True) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(margin) assert h == approx(0.4 * height)
def test_setup_both_fractions(self): """setup_figure() with both sizes fractions...""" # Simple case. _config_reset() setup_figure(width=1, height=1) w, h = matplotlib.rcParams['figure.figsize'] assert w == _config['tex'].getdimension('text_width') assert h == _config['tex'].getdimension('text_height') # More complicated fractions. for m in np.linspace(0.1, 2.1, 13): for n in np.linspace(0.1, 2.1, 13): _config_reset() setup_figure(width=m, height=n) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(m * _config['tex'].getdimension('text_width')) assert h == approx(n * _config['tex'].getdimension('text_height'))
def test_setup_margin(self): """Test setup_figure() generates margin figures with margin=True...""" setup_figure() margin = _config['tex'].getdimension('marginpar_width') height = _config['tex'].getdimension('text_height') # Fractional tests. for w_in in {1.0, 0.5, 1.2}: for h_in in {0.3, 0.25, 0.5}: _config_reset() setup_figure(width=w_in, height=h_in, margin=True) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(w_in * margin) assert h == approx(h_in * height) # Specific size. _config_reset() setup_figure(width="1.8in", height="1.2in", margin=True) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(1.8) assert h == approx(1.2)
def test_setup_full_width(self): """Test setup_figure() generates full-width figures with full_width=True...""" setup_figure() text = _config['tex'].getdimension('text_width') sep = _config['tex'].getdimension('marginpar_sep') margin = _config['tex'].getdimension('marginpar_width') full = text + sep + margin height = _config['tex'].getdimension('text_height') # Fractional tests. for w_in in {1.0, 0.75, 1.1}: for h_in in {0.4, 0.35, 0.15}: _config_reset() setup_figure(width=w_in, height=h_in, full_width=True) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(w_in * full) assert h == approx(h_in * height) # Specific size. _config_reset() setup_figure(width="5.5in", height="3.6in", full_width=True) w, h = matplotlib.rcParams['figure.figsize'] assert w == approx(5.5) assert h == approx(3.6)
def test_save_with_nonfigure_fails(self): """Test save() fails when given a non-figure object...""" setup_figure(width=1, height=1) with pytest.raises(AttributeError): save(self)
from pgfutils import setup_figure, add_dependencies, save setup_figure(width=1, height=1) import numpy as np from matplotlib import pyplot as plt t = np.linspace(0, 10, 201) s = np.sin(2 * np.pi * 0.5 * t) add_dependencies("data.file", "another.file") plt.plot(t, s) save()
# Set up the figure environment. from pgfutils import setup_figure, save setup_figure(width=0.9, height=0.4) import numpy as np from matplotlib import pyplot as plt # Generate square wave from a few terms of its Fourier series. f = 3 t = np.linspace(0, 1, 501) square = np.zeros(t.shape) for n in range(1, 12, 2): # Create this harmonic and plot it as a dashed # partially-transparent line. component = np.sin(2 * n * np.pi * f * t) / n plt.plot(t, component, '--', alpha=0.4) # Add it to the overall signal. square += component # Scale the final sum. square *= 4 / np.pi # Plot and label the figure. plt.plot(t, square, 'C0') plt.xlim(0, 1) plt.ylim(-1.2, 1.2) plt.xlabel("Time (s)") plt.ylabel("Amplitude (V)") # Save as a PGF image.
from pgfutils import setup_figure, save setup_figure(width=1, height=1, figure_background='red') save()
from pgfutils import setup_figure, save setup_figure() save()
from pgfutils import setup_figure, save setup_figure(width=1, height=0.4, extra_tracking="netCDF4") import netCDF4 import numpy as np import matplotlib.pyplot as plt t = np.linspace(0, 1, 101) s = np.sin(2 * np.pi * 3 * t) ds = netCDF4.Dataset("test_output.nc", "w") samples = ds.createDimension("sample", len(t)) times = ds.createVariable("time", "f8", ("sample", )) times[:] = t voltage = ds.createVariable("voltage", "f8", ("sample", )) voltage[:] = s ds.close() plt.plot(t, s) save()
from pgfutils import setup_figure, save, _config setup_figure( width=0.5, height=0.4, preamble_substitute=True, preamble= "\\usepackage{fontspec}\n\\setmainfont{CothamSans}[Path=${basedir}/../Cotham/,Extension=.otf]" ) from matplotlib import pyplot as plt import numpy as np t = np.linspace(-4, 4, 201) plt.plot(t, 2 * np.sin(2 * np.pi * 2.5 * t)) save()
def test_kwargs_rejects_unknown(self): """Test setup_figure() rejects unknown configuration options...""" with raises(KeyError): setup_figure(width=1, height=1, border_width=3)
from pgfutils import setup_figure, save setup_figure(width=1, height=0.4, columns=3) import numpy as np from matplotlib import pyplot as plt t = np.linspace(0, 10, 400) s = 0.3 * t + 2.5 * np.cos(2 * np.pi * 0.85 * t) - 0.8 plt.plot(t, s) plt.xlim(0, 10) plt.grid(True) save()
from pgfutils import setup_figure, save setup_figure(height=0.3) from custom_lib import get_data from matplotlib import pyplot as plt t, s = get_data() plt.plot(t, s) save()
def test_two_columns(): """Check behaviour for document with two columns...""" params = {'text_width': 8.0, 'text_height': 4, 'num_columns': 2, 'columnsep': 1} # Check some simple cases. setup_figure(width=1, height=1, columns=None, **params) assert rcParams['figure.figsize'] == [8, 4], "Wrong width for columns=None, width=1." setup_figure(width=1, height=1, columns=1, **params) assert rcParams['figure.figsize'] == [3.5, 4], "Wrong width for one column, width=1." setup_figure(width=1, height=1, columns=2, **params) assert rcParams['figure.figsize'] == [8, 4], "Wrong width for two columns, width=1." # And now with some fractional widths. setup_figure(width=0.6, height=1, columns=None, **params) assert rcParams['figure.figsize'] == [4.8, 4], "Wrong width for columns=None, width=0.6." setup_figure(width=0.5, height=1, columns=1, **params) assert rcParams['figure.figsize'] == [1.75, 4], "Wrong width for one column, width=0.5." setup_figure(width=0.85, height=1, columns=2, **params) assert rcParams['figure.figsize'] == [6.8, 4], "Wrong width for two columns, width=0.85." # And some error conditions. with pytest.raises(ValueError): setup_figure(width=1, height=1, columns=-1, **params) with pytest.raises(ValueError): setup_figure(width=1, height=1, columns=0, **params) with pytest.raises(ValueError): setup_figure(width=1, height=1, columns=3, **params)