Matplotlib colorbar formatting

I was python code to draw 4 digits with color panels. Since I use Texfonts, matplotlib makes the minus sign too wide. So I have a written formatting function to replace the minus sign with a hyphen. However, for some reason I cannot apply the formatter to my color. I get an error message:

cb.ax.set_major_formatter(ticker.FuncFormatter(myfmt)) AttributeError: 'AxesSubplot' object has no attribute 'set_major_formatter' 

So, below is a snippet of my code where it breaks: Do you have an idea how to get colorbar to use my formatting function?

 #!/usr/bin/env python3 import re import numpy as np import matplotlib.pyplot as plt from matplotlib import rc from matplotlib.ticker import * import matplotlib.ticker as ticker import matplotlib as mpl import matplotlib.gridspec as gridspec from matplotlib.patches import Ellipse from list2nparr import list2nparr from matplotlib.ticker import ScalarFormatter plt.rcParams['text.usetex'] = True plt.rcParams['font.family'] = 'serif' plt.rcParams['font.serif'] = 'cm' plt.rcParams['font.size'] = 16 #plt.rcParams['font.weight'] = 'heavy' plt.rcParams['axes.unicode_minus'] = False #----------------------------------------------------- def myfmt(x, pos=None): rv = format(x) if mpl.rcParams["text.usetex"]: rv = re.sub('$-$', r'\mhyphen', rv) return rv fig,(ax1,ax2,ax3,ax4) = plt.subplots(nrows=4,figsize=(6,11),sharex = True, sharey=False) data = list2nparr('radiant.txt') lm = data[:,14] bet = data[:,15] v = data[:,16] b = data[:,18] ejep = data[:,20] fig.subplots_adjust(hspace=0.1) cm = plt.cm.get_cmap('jet') sc = ax1.scatter(lm, bet, c=ejep, s=10, cmap=cm, edgecolor='none',rasterized=True) cb=plt.colorbar(sc,ax = ax1,aspect=10) cb.formatter.set_powerlimits((0, 0)) cb.ax.set_major_formatter(ticker.FuncFormatter(myfmt)) cb.update_ticks() 
+5
source share
2 answers

You need to specify xaxis :

 cb.ax.xaxis.set_major_formatter(plt.FuncFormatter(myfmt)) 

or yaxis :

 cb.ax.yaxis.set_major_formatter(plt.FuncFormatter(myfmt)) 

when setting formatting.

+2
source

I found the answer in the following post: in the next post .

The solution is that instead of LogFormatter you need to change the function to ScalarFormatter and define \ mhyphen as:

 plt.rcParams["text.latex.preamble"].append(r'\mathchardef\mhyphen="2D') 

So, the following class definition worked for me:

 class Myfmt(mpl.ticker.ScalarFormatter): def __call__(self, x, pos=None): # call the original LogFormatter rv =mpl.ticker.ScalarFormatter.__call__(self, x, pos) # check if we really use TeX if mpl.rcParams["text.usetex"]: # if we have the string ^{- there is a negative exponent # where the minus sign is replaced by the short hyphen rv = re.sub('-', r'\mhyphen', rv) return rv 

Then I just changed the line:

 cb=plt.colorbar(sc,ax = ax1,aspect=10) 

to

 cb=plt.colorbar(sc,ax = ax1,aspect=10,format=Myfmt()) 

and I also included a line to place one exhibitor at the top of the color panel:

 cb.formatter.set_powerlimits((0, 0)) 

Thanks for helping me find the answer!

0
source

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


All Articles