【问题标题】:Change of the fontsize when saving an svg from matplotlib从 matplotlib 保存 svg 时更改字体大小
【发布时间】:2014-12-26 12:39:51
【问题描述】:

我有一些用 matplotlib 可视化的数据。我使用的字体是 Arial,字体大小应该是 10。我将图表保存为 svg,以便在 inkscape 中对其进行后处理。一切都很顺利,轴刻度、标签等的字体大小为 12,5 而不是 10。下面是我定义 rcParams 的代码:

mpl.rcParams['axes.labelsize'] = 10
mpl.rcParams['xtick.labelsize'] = 10
mpl.rcParams['ytick.labelsize'] = 10
mpl.rcParams['legend.fontsize'] = 10

mpl.rcParams['font.family'] = ['sans-serif']
mpl.rcParams['font.sans-serif'] = ['Arial']
mpl.rcParams['text.usetex'] = False

mpl.rcParams['svg.fonttype'] = 'none'

do some stuff: fig, plt.plot, etc.

fig.savefig('fig.svg',dpi=300, bbox_inches='tight',transparent=True)

肯定有比在inkscape中调整所有内容更好的方法:)

【问题讨论】:

  • 这闻起来像个虫子。您可以为 minimal 示例输入代码吗?你用的是什么版本的mpl?
  • 我认为问题可能在于 matplotlib 将像“10”这样的数字视为 10pt。浏览器中的 10pt 和 12.5px 几乎等同于同一件事。

标签: python svg matplotlib inkscape


【解决方案1】:

这是 Inkscape 的一个问题:它总是以像素(而不是点)为单位测量字体大小。字体大小的因子 1.25 是因为 inkscape 使用 90 像素/英寸,而您在 matplotlib 中以点为单位指定的字体大小假定为 72 点/英寸。

延伸阅读:http://www.inkscapeforum.com/viewtopic.php?f=6&t=5964

但是,如果您将 svg 在 inkscape 中保存为 pdf(后处理后),您将获得与以下示例相同的字体大小:

import matplotlib as mpl
import matplotlib.pyplot as plt
import os

mpl.rcParams['axes.labelsize'] = 10
mpl.rcParams['xtick.labelsize'] = 10
mpl.rcParams['ytick.labelsize'] = 10
mpl.rcParams['legend.fontsize'] = 10
mpl.rcParams['font.family'] = ['sans-serif']
mpl.rcParams['font.sans-serif'] = ['Arial']
mpl.rcParams['text.usetex'] = False
mpl.rcParams['svg.fonttype'] = 'none'


fig = plt.figure(figsize=(4,1))
for pos,ts in enumerate(range(8,16)):
    plt.text(pos,0.5,ts, fontsize=ts)
plt.plot([-1,pos+1],[0.5,0.5])
plt.gca().yaxis.set_visible(False)
plt.gca().xaxis.set_visible(False)

# save as svg and pdf
plt.title('svg')
fig.savefig('figure.svg',dpi=300, bbox_inches='tight',transparent=True)
plt.title('pdf')
fig.savefig('figure.pdf',dpi=300, bbox_inches='tight',transparent=True)

# use inkscape to convert svg to pdf
os.system("inkscape --export-area-page --export-dpi=300 " \
        "--export-pdf=figure2.pdf -f=figure.svg")

# concatenate pdfs for comparison and make png
os.system("pdfnup --nup 1x2 -o output.pdf figure.pdf figure2.pdf")
os.system("convert -density 300 output.pdf output.png")

【讨论】:

  • 非常感谢))只是为了确定。根据帖子,您提供了链接,inkscape 中没有选项可以像在其他软件中一样设置字体大小以在 pt 中显示。据我所知,情况仍然如此。对吗?
  • 不,很遗憾,这个选项不存在(至少到我正在使用的 0.48.3.1 版本)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 2021-03-02
  • 2011-04-23
相关资源
最近更新 更多