简体   繁体   中英

Matplotlib, globally set number of ticks. X-axis, Y-axis, colorbar

For the font size I like to use I've found that 5 ticks is the most visually pleasing number of ticks across pretty much every axis in matplotlib. I also like to prune the smallest tick along the x-axis to avoid overlapping tick lables. So for almost every plot I make I find myself using the following code.

from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator

plt.imshow( np.random.random(100,100) )
plt.gca().xaxis.set_major_locator( MaxNLocator(nbins = 7, prune = 'lower') )
plt.gca().yaxis.set_major_locator( MaxNLocator(nbins = 6) )
cbar = plt.colorbar()
cbar.locator = MaxNLocator( nbins = 6)
plt.show()

Is there an rc setting I can use so that the default locator for my x-axis, y-axis, and colorbar is by default the above MaxNLocator with the prune option on the x axis?

Why don't you just write a custom module myplotlib that sets these defaults as you like them?

import myplt
myplt.setmydefaults()

A global rc setting might break other applications that rely on these settings to be not modified.

The matplotlib.ticker.MaxNLocator class has an attribute that can be used to set defaults:

default_params = dict(nbins = 10,
                      steps = None,
                      trim = True,
                      integer = False,
                      symmetric = False,
                      prune = None)

For example, this line at the beginning of your script will create 5 ticks everytime MaxNLocator is used by an axis object.

from matplotlib.ticker import *
MaxNLocator.default_params['nbins']=5

However, the default locator is matplotlib.ticker.AutoLocator , basically calling MaxNLocator with hard-wired parameters, so that the above will have no global effect without further hacks.

To change the default locator to MaxNLocator , the best I could find was to overwrite matplotlib.scale.LinearScale.set_default_locators_and_formatters with a custom method:

import matplotlib.axis, matplotlib.scale 
def set_my_locators_and_formatters(self, axis):
    # choose the default locator and additional parameters
    if isinstance(axis, matplotlib.axis.XAxis):
        axis.set_major_locator(MaxNLocator(prune='lower'))
    elif isinstance(axis, matplotlib.axis.YAxis):
        axis.set_major_locator(MaxNLocator())
    # copy & paste from the original method
    axis.set_major_formatter(ScalarFormatter())
    axis.set_minor_locator(NullLocator())
    axis.set_minor_formatter(NullFormatter())
# override original method
matplotlib.scale.LinearScale.set_default_locators_and_formatters = set_my_locators_and_formatters

This has the nice side effect of being able to specify different options for both X and Y ticks.

As suggested by Anony-Mousse

Make a file myplt.py

#!/usr/bin/env python
# File: myplt.py

from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator

plt.imshow( np.random.random(100,100) )
plt.gca().xaxis.set_major_locator( MaxNLocator(nbins = 7, prune = 'lower') )
plt.gca().yaxis.set_major_locator( MaxNLocator(nbins = 6) )
cbar = plt.colorbar()
cbar.locator = MaxNLocator( nbins = 6)
plt.show()

In your code or ipython session

import myplt

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