0

I'm trying to change the output of an exponential string formatting. I've fitted a curve to some data and would like to write these into the plot. Problem is, the x e-y format doesn't look nice, is there a posibility to change it so x * 10^-y?

Here is an example of the code I'm working with:

plt.plot(x, y, 'x', label = '$f(t) = {a:.0f} \cdot x + {b:.2e}$'.format(a = para[0], b = para[1]) 

a is some number bigger than 10000, that's why there is no need for decimalnumbers, but b is around 10^-7. Writing it as a float is spacewasting for the leading 0s, but the e^-7 doesn't look nice for publishing, that why I would like to change the output of the e-formatter, so it gives \cdot 10^{-7} (the \cdot {} for LaTeX) back.

Thanks

C W
  • 13
  • 4
  • Possible duplicate of [How can I format a float using matplotlib's LaTeX formatter?](https://stackoverflow.com/questions/17306755/how-can-i-format-a-float-using-matplotlibs-latex-formatter) – taras Jun 26 '18 at 15:42

2 Answers2

0

I would suggest you write an ad-hoc function to format the number in the output you want, something like this:

import numpy as np

def myformat(x):
    myexp = np.floor(np.log10(x))

    xout = x*10**(-myexp)

    strout = "{:.2f} \times 10^{}".format(xout, myexp)

    return strout
0

All, that need to be formated in LaTex style, must be wrapped with $ sign:

label='$f(t)$ = $x$ $\cdot$10$^-$$^7$'

Sign ^ makes aftergoing char as superscript (x$^5$ will give x to the power of 5) and _ is using for subscript (x$_5$ will give you x with index 5)