【问题标题】:How can I use LaTeX math expression with other fonts in matplotlib and ploty?如何在 matplotlib 和 plotly 中将 LaTeX 数学表达式与其他字体一起使用?
【发布时间】:2019-05-12 11:00:26
【问题描述】:

我知道任何字符串都可以在 matplotlib 和 plotly 中使用 LaTeX 渲染。例如,r$\sqrt{\dfrac{m\sigma^2}{\epsilon}}$ 将给出正确的如下图链接。

LaTeX rendering in plotly

但是,我想使用 Arial 等普通字体,而不是默认的 LaTeX 字体。有什么办法可以做到吗?

【问题讨论】:

    标签: python matplotlib plotly


    【解决方案1】:

    您可以尝试将字体关键字传递给标签函数或matplotlib.rc

    In [1]:
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    import numpy as np
    
    x = np.linspace(0, 4*np.pi)
    y = 2 * np.sin(np.exp(3/2*x))
    
    fontspecs = {'family': 'Arial',
                 'weight': 'bold'}
    mpl.rc('font', **fontspecs)  # To set font-specifications globally
    plt.plot(x, y)
    plt.xlabel(r'$2 * sin(e^{\frac{3}{2} * x})$')
    # plt.xlabel(r'$2 * sin(e^{\frac{3}{2} * x})$',  # To just use font settings for this label
    #            fontname=fontspecs['family'], fontweight=fontspecs['weight'])
    
    Out [1]:
    

    【讨论】:

    • 感谢您的回答。它按我的意愿工作。但是你知道这在情节上是如何实现的吗?
    • 对不起,我不使用plotly
    猜你喜欢
    • 1970-01-01
    • 2016-02-29
    • 2022-11-13
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2023-03-25
    相关资源
    最近更新 更多