【问题标题】:How do I change the font type of the text in a Matplotlib table?如何更改 Matplotlib 表中文本的字体类型?
【发布时间】:2021-03-25 14:34:28
【问题描述】:

我在下面的 matplotlib 中有一个示例表:

import numpy as np
import matplotlib.pyplot as plt

collabel=("col 1", "col 2", "col 3")
new_data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

the_table = plt.table(cellText=new_data,colLabels=collabel,loc='center')
plt.axis("off")

plt.show()

我只想更改表格中显示的文本的字体类型(即 Arial、Courier 等)。我在 table() 方法中找不到字体属性,我应该尝试抓取表格中的文本项吗?

【问题讨论】:

    标签: python matplotlib text


    【解决方案1】:

    您可以在绘制表格之前在plt.rcParams 中设置字体系列:

    plt.rcParams['font.family'] = 'Serif'
    the_table = plt.table(cellText=new_data, colLabels=collabel, loc='center')
    

    【讨论】:

      【解决方案2】:

      正如 tdy 刚才提到的:

      import numpy as np
      import matplotlib.pyplot as plt
      
      collabel = ("col 1", "col 2", "col 3")
      new_data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
      
      plt.rcParams['font.family'] = 'Serif'
      the_table = plt.table(cellText=new_data, colLabels=collabel, loc='center')
      plt.axis("off")
      plt.show()
      

      您可以在这里找到文档:https://matplotlib.org/stable/api/matplotlib_configuration_api.html?highlight=rc%20params#matplotlib.rc

      【讨论】:

      • 这个答案与 tdy 的有什么不同?
      • 刚刚添加了文档链接,在他的回复下发表评论会更好吗? ://
      • 是的,这样会更好。
      • 没有足够的声望(需要 50)... :/ 但是谢谢 :)
      猜你喜欢
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多