Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support placing legend symbols next to axis labels#16005

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
alexhartl wants to merge9 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromalexhartl:label-legends
Draft
Show file tree
Hide file tree
Changes fromall 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
2 changes: 2 additions & 0 deletionsdoc/api/axes_api.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -307,6 +307,8 @@ Axis Labels, title, and legend
Axes.get_xlabel
Axes.set_ylabel
Axes.get_ylabel
Axes.set_xlabel_legend
Axes.set_ylabel_legend

Axes.set_title
Axes.get_title
Expand Down
7 changes: 7 additions & 0 deletionsdoc/users/next_whats_new/2019-12-22-axis-label-legend.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
Support for axis label legends
------------------------------

Legends can now be placed next to axis labels, enabling more
space-efficient and intuitive ``twinx()`` plots. Such legends are created
using ``plt.xlabel_legend()`` and ``plt.ylabel_legend()``, which each
accept one artist handle as understood by ``plt.legend()``.
45 changes: 45 additions & 0 deletionslib/matplotlib/axes/_axes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -214,6 +214,21 @@ def set_xlabel(self, xlabel, fontdict=None, labelpad=None, **kwargs):
self.xaxis.labelpad = labelpad
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)

def set_xlabel_legend(self, handle, **kwargs):
"""
Place a legend next to the axis label.

Parameters
----------
handle: `.Artist`
An artist (e.g. lines, patches) to be shown as legend.

**kwargs: `.LegendConfig` properties
Additional properties to control legend appearance.
"""
label = self.xaxis.get_label()
label.set_legend_handle(handle, **kwargs)

def get_ylabel(self):
"""
Get the ylabel text string.
Expand DownExpand Up@@ -248,6 +263,36 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, **kwargs):
self.yaxis.labelpad = labelpad
return self.yaxis.set_label_text(ylabel, fontdict, **kwargs)

def set_ylabel_legend(self, handle, **kwargs):
"""
Place a legend next to the axis label.

Parameters
----------
handle: `.Artist`
An artist (e.g. lines, patches) to be shown as legend.

**kwargs: `.LegendConfig` properties
Additional properties to control legend appearance.

Examples
--------
Plot two lines on different axes with legends placed next to the
axis labels::

artist, = plt.plot([0, 1, 2], [0, 1, 2], "b-")
plt.ylabel('Density')
plt.ylabel_legend(artist)

plt.twinx()
artist, = plt.plot([0, 1, 2], [0, 3, 2], "r-")
plt.ylabel('Temperature')
plt.ylabel_legend(artist)
plt.show()
"""
label = self.yaxis.get_label()
label.set_legend_handle(handle, **kwargs)

def get_legend_handles_labels(self, legend_handler_map=None):
"""
Return handles and labels for legend
Expand Down
3 changes: 2 additions & 1 deletionlib/matplotlib/axis.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
import matplotlib.artist as martist
import matplotlib.cbook as cbook
import matplotlib.font_manager as font_manager
import matplotlib.legend as mlegend
import matplotlib.lines as mlines
import matplotlib.scale as mscale
import matplotlib.text as mtext
Expand DownExpand Up@@ -691,7 +692,7 @@ def __init__(self, axes, pickradius=15):
self._autolabelpos = True
self._smart_bounds = False # Deprecated in 3.2

self.label =mtext.Text(
self.label =mlegend.TextWithLegend(
np.nan, np.nan,
fontproperties=font_manager.FontProperties(
size=rcParams['axes.labelsize'],
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp