简体   繁体   中英

Turning figure or axes title off

Via ax.xaxis.grid(False) one can 'turn off/on' the grid.

Is there an equivalently pythonic way for a figure suptitle or an axes title? Something in the style of ax.title(False)

I want to write a function which is supposed to take an argument to determine whether the figure produced has a title or not (see example).

Of course, inside the function I could use a conditional statement, however, that seems rather unpythonic.

Minimal example:

def f(title=False, xgrid=False):
    fig, ax = plt.subplots()

    if title == False:    # I consider this unpythonic
        ax.set_title("")
    else:
        ax.set_title("<default_title>")

    ax.xaxis.grid(xgrid)    # I want this, only for titles

    plt.show()

You can use

ax.set_title("default title") # always set a default title
ax.title.set_visible(title) # set on or off

To toggle, you could do

ax.title.set_visible(not ax.title.get_visible())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM