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

Adding colorbar without affecting the plotting area size. #15010

Closed
@andrzejnovak

Description

@andrzejnovak

I would like to have a.mplstyle that by default produces square plots, per discussion started in#15001 and I still think "square" plots would be a reasonable thing to be able to set as default.

tl;dr Want square plots of the same size with or without colorbar. What should change is the xdim of the figure.

Anyway, for a single plot this is easy settingfigsize either live or in the style. This doesn't let me specify the box size other than manually, but I can live with that

x = np.linspace(0,1,51)y = np.random.uniform(0,5,51)z = np.random.normal(0,1,51)fig = plt.figure(figsize=(5,5))ax = fig.add_subplot()ax.scatter(x, y, c = z);

image

It gets trickier if I want to add a colorbar as well, but I would like the main box size to not change. The simplest method as far as I can tell is withfig.colorbar()

fig = plt.figure(figsize=(5,5))ax = fig.add_subplot()sc = ax.scatter(x, y, c = z);fig.colorbar(sc, ax=ax);

However, this modifies the main box to fit within figure and changes the aspect.
image

The other option is to usemake_axes_locatable Which if I understand correctly better splits the current axes and some reason this results in a less screwed up aspect than the above, but still changes it.

from mpl_toolkits.axes_grid1 import make_axes_locatablefig = plt.figure(figsize=(5,5))ax = fig.add_subplot()divider = make_axes_locatable(ax)cax = divider.append_axes('right', size="7%", pad=0.2,)sc = ax.scatter(x, y, c = z);cbar = fig.colorbar(sc, cax=cax);

image

Current best solution I found is resizing figure afterwards based on a set axes size with@ImportanceOfBeingErnest 's answerhttps://stackoverflow.com/a/44971177. However, this is sub optimal because based on ticks/labels etc it doesn't always produce the same size output figure, which can be a problem when formatting a tex document later for example.

One solutions would be to have custom function(s) presetting the sizes manually likemake_rgb_axes inhttps://matplotlib.org/3.1.1/gallery/axes_grid1/demo_axes_rgb.html which would get rid of the trial and error of setting the fig/ax size until it's just right. The obvious drawback of that is someone then wants a different size cbar or different padding it will screw up the layout and the fact that it needs to be packages somewhere else, which makes it more complicated for user to look up syntax/problems.

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