【问题标题】:How do I write text in subscript in the axis labels and the legend?如何在轴标签和图例中用下标编写文本?
【发布时间】:2023-03-18 18:20:01
【问题描述】:

我有以下轴标签和图例。

plt.ylabel("ratio_2")
plt.xlabel("n_1")
plt.legend(('alpha_1','alpha_2' ), loc = 'best',shadow = True)   

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    在公式周围加上美元符号:plt.xlabel("$n_1$")

    【讨论】:

    • 如果下标长于一个字符,例如n_10,然后可以写:plt.xlabel("$n_{10}$")
    【解决方案2】:

    我知道的最简单的方法是为 matplotlib 启用 TeX 模式,

    来自http://www.scipy.org/Cookbook/Matplotlib/UsingTex

    from matplotlib import rc
    rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
    rc('text', usetex=True)
    

    【讨论】:

    • 它的作用是通过 TeX 运行您的所有文本,同时编写各种文件。 Matplotlib 有自己的类似 TeX 的引擎,速度更快 - 只需在公式周围加上美元符号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多