【问题标题】:Python matplotlib savefig: Change font sizePython matplotlib savefig:更改字体大小
【发布时间】:2018-10-31 21:28:21
【问题描述】:

我有一些使用 matplotlib 绘制的图形,但是当我在乳胶文档中使用它时,字体太小了。我尝试使用以下代码更改字体大小:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.transforms
from matplotlib.colors import LogNorm

plt.rcParams['figure.figsize'] = (10.0, 7.0) # set default size of plots
font = {'family' : 'sans',
        'weight' : 'normal',
        'size'   : 40}
matplotlib.rc('font', **font)

#================= Draw histogram =====================
fig, ax = plt.subplots()
ax.hist2d(flow[:, 0], flow[:, 1], bins=400, norm = LogNorm(), cmap='viridis')
for xc in gt_points[:, 0]:
    ax.axvline(x=xc, linestyle=':', c='r')
    print(xc)
ax.axhline(0, linestyle=':', c='r')
ax.set_xlabel('vx [pixels per second]')
ax.set_ylabel('vy [pixels per second]')
ax.set_xlim([k_means_x_min, k_means_x_max])
ax.set_ylim([k_means_y_min, k_means_y_max])
fig.tight_layout()
plt.show()
fig.savefig('histogram.pdf', bbox_inches='tight', transparent=True)

除了更改 'size' : 40 参数不会改变任何内容,并且生成的 pdf 周围有大量空白,而且根本不“紧密”(见图)。谁能告诉我我做错了什么?

非常感谢!

编辑:添加线条 matplotlib.rc('xtick', 标签大小=20) matplotlib.rc('ytick', 标签大小=20) 至少可以使 x 和 y 轴上的数字更大。然而,正如提到的matplotlib.rc('font', **font) 什么都不做......

【问题讨论】:

  • 您可以尝试更改 figsize 。 fig size 越小字体大小增加得越多
  • 我无法使用 matplotlib 3.0.0 重现
  • 这很奇怪。该代码(除了由于缺少数据而无法运行的部分)应该可以正常工作。至少它对我有用,here is a screenshot of the pdf 我明白了。

标签: python matplotlib


【解决方案1】:

根据文档,这是可行的:

font = {'family' : 'normal',
        'weight' : 'normal',
        'size'   : 22}
matplotlib.rc('font', **font)

我猜问题是我的 python 无法识别'family' : 'sans'

【讨论】:

    【解决方案2】:

    您可以使用以下方法直接更改坐标轴标签:

    ax.set_ylabel('vx [pixels per second]', fontsize=40)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2011-04-23
      • 2021-10-30
      • 1970-01-01
      • 2016-07-02
      相关资源
      最近更新 更多