Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panel label #90

Merged
merged 7 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# Next Release

- (#90)[https://github.com/IAMconsortium/pyam/pull/90] Adding a function to `set_panel_label()` as part of the plotting library
- (#87)[https://github.com/IAMconsortium/pyam/pull/87] Extending `rename()` to work with model and scenario names
- (#85)[https://github.com/IAMconsortium/pyam/pull/85] Improved functionality for importing metadata and bugfix for filtering for strings if `nan` values exist in metadata
- (#83)[https://github.com/IAMconsortium/pyam/pull/83] Extending `filter_by_meta()` to work with non-matching indices between `df` and `data
Expand Down
23 changes: 23 additions & 0 deletions pyam/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,26 @@ def _add_legend(ax, handles, labels, legend):
MAX_LEGEND_LABELS))
legend = {} if legend in [True, None] else legend
ax.legend(handles, labels, **legend)


def set_panel_label(label, ax=None, x=0.05, y=0.9):
"""Add a panel label to the figure/axes, by default in the top-left corner

Parameters
----------
label : str
text to be added as panel label
ax : matplotlib.Axes, optional
panel to which to add the panel label
x : number, default 0.05
relative location of label to x-axis
y : number, default 0.9
relative location of label to y-axis
"""
def _lim_loc(lim, loc):
return lim[0] + (lim[1] - lim[0]) * loc

if ax is not None:
ax.text(_lim_loc(ax.get_xlim(), x), _lim_loc(ax.get_ylim(), y), label)
else:
plt.text(_lim_loc(plt.xlim(), x), _lim_loc(plt.ylim(), y), label)
Binary file added tests/expected_figs/test_add_panel_label.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,10 @@ def test_stack_plot_other(plot_df):
.stack_plot(ax=ax, stack='scenario', cmap='viridis', title='foo')
)
return fig


@pytest.mark.mpl_image_compare(**MPL_KWARGS)
def test_add_panel_label(plot_df):
fig, ax = plt.subplots(figsize=(8, 8))
plotting.set_panel_label('test', ax=ax, x=0.5, y=0.5)
return fig