Skip to content

Commit

Permalink
Panel label (#90)
Browse files Browse the repository at this point in the history
* add function to set a panel label

* add test for panel label

* redo add panel label (#5)

* define auxiliary `_lim_loc` within `set_panel_label()`

as suggested by @gidden

* remove skipping plotting-tests in Windows

* adding release notes
  • Loading branch information
danielhuppmann authored and gidden committed Sep 18, 2018
1 parent 2b6ba99 commit 5044232
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
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

0 comments on commit 5044232

Please sign in to comment.