Skip to content

Default plotting parameters #43

Closed
Closed

Description

@austin-hoover

A few suggested changes to default plotting parameters that I make in the majority of my plotting scripts. Of course these are just my personal preferences.

Discrete colormaps

The "cmap.discrete" parameter is currently true by default. I like the convenience of calling ax.pcolormesh(image, discrete=True) and think discrete colormaps can be very useful, but usually I just want the regular behavior of pcolormesh, which is to use a continuous colormap.

Grid lines

The "axes.grid" parameter is currently true by default. I think grid lines can be useful but can also be unnecessary or distracting; for example when plotting an image. Maybe I'm alone here.

Figure padding

Ultraplot figures have significant padding when labels are added to the axes. I think the default should be to have less padding; for example when making a figure for a paper in two-column format. I'm not sure which parameter this corresponds to. I tried changing "subplots.outerpad", but that cuts off the top part of plot.

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(3.0, 3.0), constrained_layout=True)
values = np.arange(16).reshape(4, 4)
ax.pcolormesh(values)
ax.set_xlabel("xlabel")
ax.set_ylabel("ylabel")
plt.savefig("test1.png", dpi=200)


import ultraplot as uplt

uplt.rc["grid"] = False
uplt.rc["cmap.discrete"] = False
uplt.rc["cmap.sequential"] = "viridis"

fig, ax = uplt.subplots(figsize=(3.0, 3.0))
ax.pcolormesh(values)
ax.format(xlabel="xlabel", ylabel="ylabel")
plt.savefig("test2.png", dpi=200)


uplt.rc["subplots.outerpad"] = 0.0

fig, ax = uplt.subplots(figsize=(3.0, 3.0))
ax.pcolormesh(values)
ax.format(xlabel="xlabel", ylabel="ylabel")
plt.savefig("test3.png", dpi=200)

matplotlib==3.9.2
ultraplot==1.0.3.post14

Activity

  1. cvanelteren commented on Jan 29, 2025

    @cvanelteren
    Collaborator

    Hi there!

    I am not too against changing the default standards that are currently there. However, I do feel like most of the issues you addressed could be fixed by a custom RC which falls out of scope for the direction of ultraplot.

    That being said, I could be persuaded to make pcolormesh have continuous as default, but I am less in favor of the grid lines being turned off. I am also in favor for more compact plotting. I usually use journal = "nat2" for controlling it -- not sure if this would solve your issue.

    Then again, I think this can also be controlled by an RC file. Would you be against using a custom RC over having to change these things explicitly?

  2. austin-hoover commented on Jan 30, 2025

    @austin-hoover
    Author

    I'm not against customizing with rc files; these are just my suggestions.

    The third point could be some kind of bug, though. When I call plt.show(), the figure padding looks great. The padding changes only for the output of plt.savefig. Here is the difference:

    The first figure was saved using plt.savefig(), and the second figure was saved using plt.show() and hitting the save figure button in the popup window.

  3. cvanelteren commented on Jan 30, 2025

    @cvanelteren
    Collaborator

    I will have a look into the margins not being set correctly. If I vaguely recall there UltraPlot does act and change some things on draw; after the renderer is acquired.

    You have made me think of potentially allowing a way for users to modify the default functions on import. That could potentially be a quality of life improvement without altering the main codebase. I will have a look at the style module and get back to you.

  4. cvanelteren commented on Jan 30, 2025

    @cvanelteren
    Collaborator

    @austin-hoover you are right that it is controlled by outerpad. The related code is found https://github.com/Ultraplot/ultraplot/blob/e7bf24032ac1d33e4c72c5570584bf3e3a19288d/ultraplot/gridspec.py#L846.

    I can also confirm there is a noticeable difference in the padding between the saved and shown image (e.g. in a repl). I can't recall if this was already an issue in proplot or this is somehow caused by changes on the mpl side, do you know by any chance? The savefig internally used for ultraplot/proplot merely wraps the existing method and seems not to modify the behavior

  5. cvanelteren commented on Jan 30, 2025

    @cvanelteren
    Collaborator

    Update!

    inserting a figure.canvas.draw() in the savefig fixes the issue. Not sure what to do here. Any thoughts @beckermr?

  6. cvanelteren commented on Jan 30, 2025

    @cvanelteren
    Collaborator

    In particular, it would modify savefig as:

        @docstring._concatenate_inherited
        @docstring._snippet_manager
        def savefig(self, filename, **kwargs):
            """
            %(figure.save)s
            """
            # Automatically expand the user name. Undocumented because we
            # do not want to overwrite the matplotlib docstring.
            if isinstance(filename, str):
                filename = os.path.expanduser(filename)
            self.canvas.draw() #forcing draw
            super().savefig(filename, **kwargs)
  7. austin-hoover commented on Jan 30, 2025

    @austin-hoover
    Author

    I don't think this issue showed up in proplot.

  8. cvanelteren commented on Jan 31, 2025

    @cvanelteren
    Collaborator

    Margins are fixed and a new tag 1.04 is available and can be updated from pip or conda. Will close for now as the remaining could be fixed with a local rc file. In addition, I think Ultraplot has plenty of tools to allow custom styling (see the style module).

    However, if you do feel like these issues need addressing we could create a sub-issue to reopen this again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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