【问题标题】:Matplotlib setting title bold while using "Times New Roman"Matplotlib 在使用“Times New Roman”时设置标题粗体
【发布时间】:2013-09-23 14:34:56
【问题描述】:

最初我可以通过以下方式将图形标题设置为粗体:

import Matplotlib.pyplot as plt

plt.title("Test",fontweight="bold")

但是一旦我使用fontname="Times New Roman"fontweight="bold" 根本不会导致任何变化:

import Matplotlib.pyplot as plt

plt.title("Test",fontname="Times New Roman",fontweight="bold")

如何将图形标题设置为粗体?

【问题讨论】:

  • 嗯.....这对我有用。

标签: python matplotlib


【解决方案1】:

fontweight 是 fig.suptitle() 函数的参数。 所以这应该对你有用, fig.suptitle("Test",fontweight='bold')

【讨论】:

    【解决方案2】:

    假设它已安装在您的系统上,它有自己的粗体时间字体:

    plt.title("Test", fontname="Times New Roman Bold")
    

    您可以在此处找到系统上的字体列表:How to get a list of all the fonts currently available for Matplotlib?

    我有:

    >>> [i for i in matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
    if 'times' in i.lower()]
    ['/Library/Fonts/Times New Roman.ttf',
     '/Library/Fonts/Times New Roman Italic.ttf',
     '/Library/Fonts/Times New Roman Bold Italic.ttf',
     '/Library/Fonts/Times New Roman Bold.ttf']
    

    【讨论】:

    • 虽然我有所有这些字体(除了一个省略了所有路径):['/usr/share/fonts/truetype/msttcorefonts/timesbd.ttf', 'Times_New_Roman_Bold.ttf', 'Times_New_Roman .ttf'、'timesi.ttf'、'Times_New_Roman_Bold_Italic.ttf'、'timesbi.ttf'、'times.ttf'、'Times_New_Roman_Italic.ttf'] 我收到此错误:font_manager.py:1241: UserWarning: findfont: Font找不到家庭 ['Times New Roman Bold']。回到 DejaVu Sans。 (prop.get_family(), self.defaultFamily[fontext]))
    【解决方案3】:

    在 linux 和 mac 上,TImes New Roman 的粗体字体似乎比普通字体更受欢迎。这就是为什么您看不到将普通字体设置为粗体的更改,它已经是粗体了。

    我知道这个问题很老了,但它仍然是一个问题,至少在我的 Mac 上,当你想使用普通的时候。我找到了这个问题的一个非常简单的解决方案,由 azag0 在 github 上发布

    del matplotlib.font_manager.weight_dict['roman']
    matplotlib.font_manager._rebuild()
    

    https://github.com/matplotlib/matplotlib/issues/5574

    如果您这样做,您将在设置 fontweight=bold 时看到更改,因为正确选择了标准。

    【讨论】:

    • 我也看到了这个解决方案,但似乎无法让它发挥作用。我必须把那个 sn-p 放在哪里?
    • 在加载 matplot.lib 之后应该没问题,必须在尝试调整字体粗细之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2018-10-24
    • 1970-01-01
    • 2011-05-27
    相关资源
    最近更新 更多