1

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

zxdawn
  • 825
  • 1
  • 9
  • 19

3 Answers3

3

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

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
2

Using a regex:

import matplotlib.pyplot as plt
import re

data = 5.55e10

s = re.sub(r'e([+-]\d+)', r'\\cdot{}10^{\1}', f'e$^{{{data:.2e}}}$')

plt.title(s)

output: enter image description here

mozway
  • 194,879
  • 13
  • 39
  • 75
  • For scientific counting, I think "\times"(×) is better than "\cdot"(·) – Mechanic Pig Jun 02 '22 at 12:46
  • @MechanicPig I guess this is a matter of personal preference, I like the cdot. I use × for things like `100×`, `2×3 cm` ;) – mozway Jun 02 '22 at 12:47
  • 1
    It may be caused by differences in education. Chinese primary school textbooks will mention that the multiplication sign should not be replaced by a dot in pure numeric expressions :) – Mechanic Pig Jun 02 '22 at 12:53
0

you can do that by extracting the base and exp:

import matplotlib.pyplot as plt
from math import log10, floor



def find_exp_base(number):
    exp = floor(log10(abs(number)))
    return round(number/10**exp, 2), exp 

    
data = 5.55e10
base, exp = find_exp_base(data)

plt.title('e$^{' + str(base) + '*10^{'+ str(exp) + '}}$')
plt.show()

output :

enter image description here

mrCopiCat
  • 899
  • 3
  • 15