|
|
|||||||||||||||||||||||||||||
|
matplotlib pyplots_Examples 14_auto_subplots_adjust. |
H.Kamifuji . |
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
def on_draw(event):
bboxes = []
for label in labels:
bbox = label.get_window_extent()
# the figure transform goes from relative coords->pixels and we
# want the inverse of that
bboxi = bbox.inverse_transformed(fig.transFigure)
bboxes.append(bboxi)
# this is the bbox that bounds all the bboxes, again in relative
# figure coords
bbox = mtransforms.Bbox.union(bboxes)
if fig.subplotpars.left < bbox.width:
# we need to move it over
fig.subplots_adjust(left=1.1*bbox.width) # pad a little
fig.canvas.draw()
return False
fig.canvas.mpl_connect('draw_event', on_draw)
plt.show()
|
![]() Python 3.11.6 (matplotlib 3.7.1) では、下記のような警告があるが、実行できる。ただし、subplots_adjust が機能していない。
Traceback (most recent call last):
File "C:\Users\______\AppData\Local\Programs\Python\Python311\Lib
\site-packages\matplotlib\cbook\__init__.py", line 304, in process
func(*args, **kwargs)
File "M:\______\auto_subplots_adjust.py", line 15, in on_draw
bboxi = bbox.inverse_transformed(fig.transFigure)
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Bbox' object has no attribute 'inverse_transformed'
Python 3.12.0 (matplotlib 3.8.1) では、下記のような警告があるが、実行できる。ただし、subplots_adjust が機能していない。
Traceback (most recent call last):
File "C:\Program Files\Python312\Lib\site-packages\matplotlib\cbook.py",
line 298, in process
func(*args, **kwargs)
File "E:\______\auto_subplots_adjust.py", line 15, in on_draw
bboxi = bbox.inverse_transformed(fig.transFigure)
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Bbox' object has no attribute 'inverse_transformed'
下図参照。![]() Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(subplots-axes-and-figures-auto-subplots-adjust-py) を見つけ、下記のコードで、正常に実行できました。
"""
===============================================
Programmatically controlling subplot adjustment
===============================================
.. note::
This example is primarily intended to show some advanced concepts in
Matplotlib.
If you are only looking for having enough space for your labels, it is
almost always simpler and good enough to either set the subplot parameters
manually using `.Figure.subplots_adjust`, or use one of the automatic
layout mechanisms
(:ref:`constrainedlayout_guide` or
:ref:`tight_layout_guide`).
This example describes a user-defined way to read out Artist sizes and
set the subplot parameters accordingly. Its main purpose is to illustrate
some advanced concepts like reading out text positions, working with
bounding boxes and transforms and using
:ref:`events
Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行です。bboxi = bbox.inverse_transformed(fig.transFigure) が bbox_fig = bbox_px.transformed(fig.transFigure.inverted()) に、変更されている。 ![]() |
|
pyplots_Examples code: auto_subplots_adjust.py subplots-axes-and-figures-auto-subplots-adjust-py |
|