def test_figure_savefig(): """ Check if the arguments being passed to psconvert are correct. """ kwargs_saved = [] def mock_psconvert(*args, **kwargs): # pylint: disable=unused-argument """ Just record the arguments. """ kwargs_saved.append(kwargs) fig = Figure() fig.psconvert = mock_psconvert prefix = "test_figure_savefig" fname = ".".join([prefix, "png"]) fig.savefig(fname) assert kwargs_saved[-1] == dict(prefix=prefix, fmt="g", crop=True, Qt=2, Qg=2) fname = ".".join([prefix, "pdf"]) fig.savefig(fname) assert kwargs_saved[-1] == dict(prefix=prefix, fmt="f", crop=True, Qt=2, Qg=2) fname = ".".join([prefix, "png"]) fig.savefig(fname, transparent=True) assert kwargs_saved[-1] == dict(prefix=prefix, fmt="G", crop=True, Qt=2, Qg=2) fname = ".".join([prefix, "eps"]) fig.savefig(fname) assert kwargs_saved[-1] == dict(prefix=prefix, fmt="e", crop=True, Qt=2, Qg=2) fname = ".".join([prefix, "kml"]) fig.savefig(fname) assert kwargs_saved[-1] == dict(prefix=prefix, fmt="g", crop=True, Qt=2, Qg=2, W="+k")
def test_figure_savefig_filename_with_spaces(): """ Check if savefig (or psconvert) supports filenames with spaces. """ fig = Figure() fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True) with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile: fig.savefig(imgfile.name) assert os.path.exists(imgfile.name)
def test_figure_savefig_ps_extension(): """ Check that an error is raised when .ps extension is specified. """ fig = Figure() fig.basemap(region="10/70/-300/800", projection="X3c/5c", frame="af") fname = "test_figure_savefig_ps_extension.ps" with pytest.raises(GMTInvalidInput, match="Extension '.ps' is not supported."): fig.savefig(fname)
def test_figure_savefig_exists(): "Make sure the saved figure has the right name" fig = Figure() fig.basemap(region="10/70/-300/800", projection="X3i/5i", frame="af") prefix = "test_figure_savefig_exists" for fmt in "png pdf jpg bmp eps tif".split(): fname = ".".join([prefix, fmt]) fig.savefig(fname) assert os.path.exists(fname) os.remove(fname)
def test_figure_savefig_unknown_extension(): """ Check that an error is raised when an unknown extension is passed. """ fig = Figure() fig.basemap(region="10/70/-300/800", projection="X3i/5i", frame="af") prefix = "test_figure_savefig_unknown_extension" fmt = "test" fname = ".".join([prefix, fmt]) with pytest.raises(GMTInvalidInput): fig.savefig(fname)
def test_figure_savefig_transparent(): "Check if fails when transparency is not supported" fig = Figure() fig.basemap(region="10/70/-300/800", projection="X3i/5i", frame="af") prefix = "test_figure_savefig_transparent" for fmt in "pdf jpg bmp eps tif".split(): fname = ".".join([prefix, fmt]) with pytest.raises(GMTInvalidInput): fig.savefig(fname, transparent=True) # png should not raise an error fname = ".".join([prefix, "png"]) fig.savefig(fname, transparent=True) assert os.path.exists(fname) os.remove(fname)
C=True, verbose=True, L="1p/black") if mt_colorbar: fig.colorbar(position="n.95/.825+w-2c/.45c", frame='af+l"depth [km]"') # Scalebar, need to use config to set the pen thickness of the scale with config(MAP_TICK_PEN_PRIMARY=1.5): fig.basemap(region=region, projection=projection, #frame=["WSne", "gfa"], map_scale=map_scale) # Landmark text fig.text(textfiles=landmarks_fid) # Plot the whole of Alaska as an inset to give context if inset: with fig.inset(position="n0/.75+w5c/4c", margin=0): # Plot the whole of Alaska as the inset fig.coast(region=[-170, -135, 53, 72], projection="L-150/62/63/64/3.5c", land="gray", water="white", shorelines="0p,black", frame=False, resolution="h", area_thresh=100) # !!! THIS DOESNT WORK Put a box in the inset around Northern Alaska rectangle = [region[0], region[2], region[1], region[3]] fig.plot(data=rectangle, style="r+s", pen="1p,red") # Plot the plate coupling / slip rate deficit with a no-label colorbar # Save and show fig.savefig(output, transparent=bg_transparent) fig.show(method="external")