其他分享
首页 > 其他分享> > Matplotlib Superscript format in matplotlib plot legend

Matplotlib Superscript format in matplotlib plot legend

作者:互联网

 

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set(title=r'This is an expression $e^{\sin(\omega\phi)}$',
       xlabel='meters $10^1$', ylabel=r'Hertz $(\frac{1}{s})$')
plt.show()

 

 

 

 

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

x_data = np.linspace(0.05,1,101)
y_data = 1/x_data
noise = np.random.normal(0, 1, y_data.shape)
y_data2 = y_data + noise

def func_power(x, a, b):
    return a*x**b

popt, pcov= curve_fit(func_power, x_data, y_data2)

plt.figure()
plt.scatter(x_data, y_data2, label = 'data')
plt.plot(x_data, popt[0] * x_data ** popt[1], label = ("$y = {{{}}}x^{{{}}}$").format(round(popt[0],2), round(popt[1],2)))
plt.plot(x_data, x_data**3, label = '$x^3$')
plt.legend()
plt.show()

 

 

REF

https://stackoverflow.com/questions/53781815/superscript-format-in-matplotlib-plot-legend

 https://stackoverflow.com/questions/21226868/superscript-in-python-plots

标签:plot,plt,format,matplotlib,popt,import,data
来源: https://www.cnblogs.com/emanlee/p/14401039.html