pythonmatplotlibsignificant-digits

How to plot the string with power of ten as the exponential?


I want to plot a scientific title or legend label which is e^(x*10**b).

Here's an example:

import matplotlib.pyplot as plt

data = 5.55e10

plt.title('e$^{%.2e}$'%(data))

And, the actual output is: real

The preferred output is: answer


Solution

  • Split the formatted string and format them again:

    >>> val = 5.55e10
    >>> base, power = f'{val:.2e}'.split('e')
    >>> f'e$^{{{base}*10^{{{power}}}}}$'
    'e$^{5.55*10^{+10}}$'
    

    Output in matplotlib:

    enter image description here