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

[Bug]: Colorbar is displaced when saving as .pdf with bbox_inches='tight' and by using inset_axes #27143

Closed
@Hexanders

Description

@Hexanders

Bug summary

I useimshow() withcolorbar andinset_axes to zoom into the image. After saving the pdf withsavefigure() andbbox_inches='tight' the colorbar is shifted.
Very similar to bug#25176, which has already been fixed.

The problem only occurs when using thebbox_inches='tight' option in combination withinset_axes.

Code for reproduction

importmatplotlib.pyplotaspltimportnumpyasnpimportmatplotlib.cbookascbookimportmatplotlib.cmascmfrommatplotlib.patchesimportPathPatchfrommatplotlib.pathimportPathnp.random.seed(19680801)delta=0.025x=y=np.arange(-3.0,3.0,delta)X,Y=np.meshgrid(x,y)Z1=np.exp(-X**2-Y**2)Z2=np.exp(-(X-1)**2- (Y-1)**2)Z= (Z1-Z2)*2fig,ax=plt.subplots()im=ax.imshow(Z,interpolation='bilinear',cmap=cm.RdYlGn,origin='lower',extent=[-3,3,-3,3],vmax=abs(Z).max(),vmin=-abs(Z).max())cb=plt.colorbar(mappable=im)plt.show()axins=inset_axes(ax,width='20%',height='20%',loc='upper right')Z2=axins.imshow(Z,interpolation='bilinear',cmap=cm.RdYlGn,origin='lower',extent=[-3,3,-3,3],vmax=abs(Z).max(),vmin=-abs(Z).max())x1,x2,y1,y2=-1,0,0,1axins.set_xlim(x1,x2)axins.set_ylim(y1,y2)rectpatch,connects=ax.indicate_inset_zoom(axins,edgecolor='k',alpha=1,lw=1)connects[0].set_visible(False)connects[1].set_visible(True)connects[2].set_visible(True)connects[3].set_visible(False)fig.savefig("temp.pdf",bbox_inches='tight',transparent=True,format="pdf")

Actual outcome

temp.pdf

Expected outcome

Somthing like this:

temp.pdf

but with tight layout

Additional information

Python version :
Matplotlib version:

Operating system

OS: Linux Fedora 37 (Workstation Edition)

Matplotlib Version

3.8.0

Matplotlib Backend

module://ipympl.backend_nbagg

Python version

3.11.5

Jupyter version

module://ipympl.backend_nbagg

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp