Beispiel #1
0
def test_save_current_figure(tmp_path: Path, monkeypatch):
    # create test figure
    fig = create_figure(width='8cm', aspect_ratio=1)
    x = np.linspace(0, 10, 100)
    plt.plot(x, np.sin(x))
    plt.title('test plot')

    # setup environment variables
    monkeypatch.setenv('FIG_TOOLS_WORKSPACE_ROOT', '')
    monkeypatch.setenv('FIG_TOOLS_IMAGE_PATH', '')

    filename = tmp_path / 'a/b/test.py'
    save_figure(filename)  # by default png-files are created

    # make sure the file exists
    assert filename.with_suffix('.png').exists()
Beispiel #2
0
def test_add_commit_metadata(tmp_path: Path, monkeypatch):

    # create test figure
    fig = create_figure(width='8cm', aspect_ratio=1)
    x = np.linspace(0, 10, 100)
    plt.plot(x, np.sin(x))
    plt.title('test plot')
    plt.tight_layout()

    # setup environment variables
    monkeypatch.setenv('FIG_TOOLS_WORKSPACE_ROOT', '')
    monkeypatch.setenv('FIG_TOOLS_IMAGE_PATH', '')

    # save figure
    filename = tmp_path / Path(__file__).name
    save_figure(filename)

    # load image metdata and compare
    info = load_image_metadata(filename.with_suffix('.png'))
    assert info['script-filename'] == filename.name
    assert info['git-commit'] == fts._get_git_commit_hash()
# %%
apply_style()

# %%
fig = create_figure(width='1x')

# ... put your own plotting logic here
# ...
# ...
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.sin(2.3 * x), '-')
plt.xlabel('$t$ / s')
plt.ylabel('amplitude')
plt.title('awesome figure title')
# ...
# ...
# ...

plt.tight_layout()

# Save the current figure in "png" format. The image will have the same
# name as the python script, except for another suffix.
save_figure(__file__)

# %%
print_image_metadata(__file__)

# %%