【问题标题】:Matplotlib greek symbols differ in show() and savefig()Matplotlib 希腊符号在 show() 和 savefig() 中有所不同
【发布时间】:2016-01-07 22:42:33
【问题描述】:

我正在尝试使用 matplotlib 在保存的图形中以罗马字体显示一个简单的希腊字母 mu。我试过两种方法:

  1. plt.xlabel(u'Wavelength (\u03bc m)')

当我执行show() 时,此方法可以正常工作,但是当我尝试使用savefig() 时,mu 字符在保存为 .png 时显示为矩形。如果我另存为 .pdf,则符号完全丢失。

  1. plt.xlabel(r'Wavelength ($\mathrm{\mu}$m)')

此方法使用show()savefig() 呈现一个希腊字母,但在每种情况下该字符仍为斜体,尽管请求罗马字体。

有什么诀窍?

【问题讨论】:

  • 我使用 LaTeX 包 upgreek 解决了同样的问题,它提供了我在 matplotlib 和文本中使用的宏 \upmu

标签: python matplotlib


【解决方案1】:

我在使用 LaTeX 排版 所有 文本(常规和数学)beeing 方面有很好的经验。只需在绘图前相应地设置您的 rc-settings:

import matplotlib.pyplot as plt

plt.rcParams['text.usetex'] = True #Let TeX do the typsetting
plt.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}',r'\sansmath']
#Force sans-serif math mode
plt.rcParams['font.family'] = 'sans-serif' # ... for regular text
plt.rcParams['font.sans-serif'] = 'Helvetica' # Choose a nice font here

然后你可以简单地说:

plt.xlabel('Wavelength ($\mu$)')

受到我自己的回答的启发: Type 1 fonts with log graphs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-15
    • 2012-10-21
    • 1970-01-01
    • 2011-01-09
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多