I need a literature-style scientific notation on both axes in matplotlib with specific font.
In my search I found this question with an answer for literature-style scientific notation from ImportanceOfBeingErnest:Show decimal places and scientific notation on the axis
There are no details about changing font though.
My code:
import numpy as npimport matplotlib.pyplot as pltimport matplotlib.ticker as mtickerfig,ax=plt.subplots(1,1,figsize=[7,7])x=np.linspace(0,0.1,10**3)y=x**2ax.plot(x,y)# code from ImportanceOfBeingErnestclass MathTextSciFormatter(mticker.Formatter): def __init__(self, fmt="%1.2e"): self.fmt = fmt def __call__(self, x, pos=None): s = self.fmt % x decimal_point = '.' positive_sign = '+' tup = s.split('e') significand = tup[0].rstrip(decimal_point) sign = tup[1][0].replace(positive_sign, '') exponent = tup[1][1:].lstrip('0') if exponent: exponent = '10^{%s%s}' % (sign, exponent) if significand and exponent: s = r'%s{\times}%s' % (significand, exponent) else: s = r'%s%s' % (significand, exponent) return "${}$".format(s)# Format with 2 decimal placesplt.gca().xaxis.set_major_formatter(MathTextSciFormatter("%1.2e"))plt.gca().yaxis.set_major_formatter(MathTextSciFormatter("%1.2e"))# end of code from ImportanceOfBeingErnestplt.xticks(rotation=45,font="Arial",fontsize=20)for tick in ax.get_yticklabels(): tick.set_fontname("Arial")
Result:Graph from code
The literature-style scientific notation is implemented but none of the two methods of changing font work. Interestingly though, the fontsize can be changed.
Could anyone provide a solution for changing font with this notation?
matplotlib 3.5.3