Python 从matplotlib保存svg时fontsize的更改

Python 从matplotlib保存svg时fontsize的更改,python,svg,matplotlib,inkscape,Python,Svg,Matplotlib,Inkscape,我有一些数据可以用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

我有一些数据可以用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中,应该有一个更好的方法来调整所有内容的大小:)

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

进一步阅读:

但是如果将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")

这闻起来像虫子。你能为一个简单的例子输入代码吗?您使用的是什么版本的mpl?我认为问题可能在于matplotlib将“10”这样的数字视为10pt。浏览器中的10pt和12.5px几乎等同于相同的东西。非常感谢)只是为了确定一下。根据帖子,您提供了指向的链接,inkscape中没有设置fontsize以pt显示的选项,就像在其他软件中一样。就我所知,情况仍然如此。对吗?不,很遗憾,此选项不存在(至少在我使用的版本0.48.3.1之前)。