如何让Python绘图保存为SVG文件后文本可编辑?
Great question—let's break down solutions for both SVG and PGF formats to get you the editable, searchable text you need, without switching to TikZ.
Your Current Setup & Issue
First, a quick recap of your code and problem:
You're saving an SVG with this code, but text renders as uneditable shapes in Inkscape:
import matplotlib as mpl mpl.use('svg') import matplotlib.pyplot as plt import numpy as np plt.figure() x = np.linspace(0,2*np.pi) y = np.sin(x) plt.plot(x, y) plt.xlabel('Phase $\phi$') plt.ylabel('Signal') plt.savefig('SineSignal.svg', format = 'svg')
Switching to PGF (mpl.use('pgf')) fixes PDF text search, but you still can't modify fonts/sizes directly:
plt.savefig('SineSignal.pgf')
Fix for Editable Text in SVG
Matplotlib defaults to converting text to vector paths in SVGs (to ensure consistent rendering across systems), which locks you out of editing. To fix this, just tweak one configuration parameter before saving:
Add this line right after importing matplotlib to force SVG to use actual editable text elements:
mpl.rcParams['svg.fonttype'] = 'svgfont'
Your updated SVG code will look like this:
import matplotlib as mpl mpl.use('svg') # Enable editable text in SVG output mpl.rcParams['svg.fonttype'] = 'svgfont' import matplotlib.pyplot as plt import numpy as np plt.figure() x = np.linspace(0,2*np.pi) y = np.sin(x) plt.plot(x, y) plt.xlabel('Phase $\phi$') plt.ylabel('Signal') plt.savefig('SineSignal.svg', format = 'svg')
Now when you open the SVG in Inkscape, you can select, modify, and re-style every text element—font, size, content, all editable. The only tradeoff: text rendering will depend on the fonts installed on the system opening the SVG (but that's the cost of editability).
Fix for Font Control in PGF
For PGF files, you can't edit text directly in Inkscape, but you can fully control fonts and sizes through your LaTeX document. Here's how to sync Matplotlib's PGF output with your LaTeX setup:
- Configure Matplotlib to use your desired LaTeX fonts in the PGF export:
import matplotlib as mpl mpl.use('pgf') # Set up PGF to match your LaTeX document's fonts mpl.rcParams['pgf.texsystem'] = 'pdflatex' # Or 'xelatex' if using system fonts mpl.rcParams['font.family'] = 'serif' mpl.rcParams['text.usetex'] = True # Add LaTeX preamble commands to load your preferred font mpl.rcParams['pgf.preamble'] = [ r'\usepackage{amsmath}', r'\usepackage{fontspec}', # Required for XeLaTeX/LuaLaTeX r'\setmainfont{Times New Roman}' # Replace with your font of choice ]
- When including the PGF in your LaTeX document, mirror the same font setup:
\documentclass{article} \usepackage{pgf} \usepackage{fontspec} \setmainfont{Times New Roman} \begin{document} \input{SineSignal.pgf} \end{document}
This way, the plot's text will match your document's font perfectly, and you can adjust sizes/styles directly in your LaTeX preamble. The text remains searchable in the final PDF too.
Bonus: Hybrid Workflow for Maximum Flexibility
If you want both Inkscape editability and clean LaTeX integration:
- Save the plot as an editable SVG using the first method
- Open it in Inkscape to tweak text fonts, sizes, or content
- Export the modified SVG to PDF directly from Inkscape
- Include the PDF in your LaTeX document with
\includegraphics
This gives you full visual control in Inkscape while maintaining a professional, searchable PDF output.
内容的提问来源于stack exchange,提问作者Forrest Thumb




