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

ENH: Align titles#27952

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

Merged
rcomer merged 5 commits intomatplotlib:mainfromtrananso:align-titles
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
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
PrevPrevious commit
NextNext commit
Add documentation for align_titles
-Add align_titles to api doc
  • Loading branch information
@trananso
trananso committedApr 4, 2024
commit2ff72be64c65750b4f42706136e66f590f33e097
2 changes: 2 additions & 0 deletionsdoc/api/figure_api.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -71,6 +71,7 @@ Annotating
Figure.align_labels
Figure.align_xlabels
Figure.align_ylabels
Figure.align_titles
Figure.autofmt_xdate


Expand DownExpand Up@@ -264,6 +265,7 @@ Annotating
SubFigure.align_labels
SubFigure.align_xlabels
SubFigure.align_ylabels
SubFigure.align_titles

Adding and getting Artists
--------------------------
Expand Down
7 changes: 7 additions & 0 deletionsdoc/users/next_whats_new/figure_align_titles.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
subplot titles can now be automatically aligned
-----------------------------------------------

Subplot axes titles can be misaligned vertically if tick labels or
xlabels are placed at the top of one subplot. The new method on the
`.Figure` class: `.Figure.align_titles` will now align the titles
vertically.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
"""
===============
Aligning Labels
===============
==========================
Aligning Labels and Titles
==========================

Aligning xlabelandylabel using `.Figure.align_xlabels` and
`.Figure.align_ylabels`
Aligning xlabel, ylabel,andtitle using `.Figure.align_xlabels`,
`.Figure.align_ylabels`, and `.Figure.align_titles`.

`.Figure.align_labels` wrapsthese two functions.
`.Figure.align_labels` wrapsthe x and y label functions.

Note that the xlabel "XLabel1 1" would normally be much closer to the
x-axis,and "YLabel1 0" would be much closer to the y-axis of their
respective axes.
x-axis,"YLabel0 0" would be much closer to the y-axis, and title
"Title0 0" would be much closer to the top of theirrespective axes.
"""
import matplotlib.pyplot as plt
import numpy as np

import matplotlib.gridspec as gridspec
fig, axs = plt.subplots(2, 2, layout='tight')

fig = plt.figure(tight_layout=True)
gs = gridspec.GridSpec(2, 2)

ax = fig.add_subplot(gs[0, :])
ax = axs[0][0]
ax.plot(np.arange(0, 1e6, 1000))
ax.set_ylabel('YLabel0')
ax.set_xlabel('XLabel0')
ax.set_title('Title0 0')
ax.set_ylabel('YLabel0 0')

ax = axs[0][1]
ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
ax.set_title('Title0 1')
ax.xaxis.tick_top()
ax.tick_params(axis='x', rotation=55)


for i in range(2):
ax =fig.add_subplot(gs[1, i])
ax =axs[1][i]
ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
ax.set_ylabel('YLabel1 %d' % i)
ax.set_xlabel('XLabel1 %d' % i)
if i == 0:
ax.tick_params(axis='x', rotation=55)

fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels()
fig.align_titles()

plt.show()
3 changes: 1 addition & 2 deletionslib/matplotlib/figure.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -132,8 +132,7 @@ def __init__(self, **kwargs):
self._supxlabel = None
self._supylabel = None

# groupers to keep track of x, y labels and title we want to
# align.
# groupers to keep track of x, y labels and title we want to align.
# see self.align_xlabels, self.align_ylabels,
# self.align_titles, and axis._get_tick_boxes_siblings
self._align_label_groups = {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp