In matplotlib 2.0, how do I return the colorbar behavior to match matplotlib 1.5?

I just upgraded to matplotlib 2.0 and overall, I am very pleased with the new default standards. The only thing I would like to return to behavior 1.5 is that plt.colorbar , in particular yticks. In the old matplotlib, only large mites were applied to my colorimeters; in the new matplotlib, small and basic ticks are drawn, which I don’t want.

Below is a comparison of the behavior of mode 1.5 (left) and 2.0 (right) using the same color marks and logarithmic ticks.

Colorbar yticks comparison

What default values ​​do I need to set in matplotlibrc in order to return to the 1.5 shown on the left? If there is no way to do this with matplotlibrc , what other options are available to change this globally outside of the downgrade to matplotlib 1.5?

I tried just setting cbar.ax.minorticks_off() after each instance of cbar = plt.colorbar(mesh) , but this does not solve the problem.

+5
source share
1 answer

Simply set the colorbar locator to the LogLocator from matplotlib.ticker , and then call update_ticks() on the colorbar instance.

For example, consider this minimal example, which creates a color panel that you see with minor ticks:

 import matplotlib.pyplot as plt import matplotlib.ticker as ticker import matplotlib.colors as colors import numpy as np fig, ax = plt.subplots(1) # Some random data in your range 1e-26 to 1e-19 data = 10**(-26. + 7. * np.random.rand(10, 10)) p = ax.imshow(data, norm=colors.LogNorm(vmin=1e-26, vmax=1e-19)) cb = fig.colorbar(p, ax=ax) plt.show() 

enter image description here

If we now add the following two lines before calling plt.show() , we will remove the minor ticks:

 cb.locator = ticker.LogLocator() cb.update_ticks() 

enter image description here

Alternatively, to achieve the same, you can use ticks kwarg when creating a color panel and set it to LogLocator()

 cb = fig.colorbar(p, ax=ax, ticks=ticker.LogLocator()) 
+5
source

Source: https://habr.com/ru/post/1266323/


All Articles