zoukankan      html  css  js  c++  java
  • 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(omegaphi)}$',
           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()

    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(figsize=(4, 3))
    plt.title('Losses')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    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

  • 相关阅读:
    【HAOI2014】贴海报
    【HAOI2016】食物链
    【NOI2003】银河英雄传
    【HAOI2013】花卉节
    【BZOJ1702】[usaco2007margold]队列平衡
    【网络流24】餐巾
    洛谷 [P1265] 公路修建
    全排列与 康托展开
    洛谷 [P1403] 约数研究
    高精度模板
  • 原文地址:https://www.cnblogs.com/emanlee/p/14401039.html
Copyright © 2011-2022 走看看