【问题标题】:Export matplotlib as emf and avoid black bacground fill将 matplotlib 导出为 emf 并避免黑色背景填充
【发布时间】:2021-09-09 09:14:12
【问题描述】:

前面的讨论 [1] 帮助我们将 matplotlib 图导出为 .emf 文件。然而,奇怪的错误是输出文件有节点填充黑色背景,如下所示。

如何获得与 .svg 完全相同的 .emf?

import matplotlib.pyplot as plt
import subprocess, os

# Graph with 4 nodes - its coordinates
x = [0, 5, 5, 0]
y = [0, 0, 5, 5]

fig, ax = plt.subplots()

# Plot the vertices
for i in range(len(x)):
        ax.plot(x[i], y[i], 'ro',
                 markersize = 12,
                 fillstyle = 'none',
                 label = 'A')

# Plot the edges
plt.plot(x,y)

# Legend
handles, labels = plt.gca().get_legend_handles_labels()
by_label = dict(zip(labels, handles))
plt.legend(by_label.values(), by_label.keys())

# File export
inkscape_path = "C://Program Files//Inkscape//bin//inkscape.exe"
out_file = "C:/Users/banbar/Desktop/out1.emf"

path, filename = os.path.split(out_file)
filename, extension = os.path.splitext(filename)

svg_filepath = os.path.join(path, filename+'.svg')
emf_filepath = os.path.join(path, filename+'.emf')

fig.savefig(svg_filepath, format='svg')

subprocess.call([inkscape_path, svg_filepath, '--export-filename', emf_filepath])   

【问题讨论】:

    标签: python matplotlib svg inkscape


    【解决方案1】:

    这只是答案的一半,即 SVG 的一半:

    当您在 Inkscape 中选择其中一个方块时,您可能会注意到它在填充指示器所在的左下方显示“未设置填充”。

    那是因为 matplotlib 完全忽略了 fill 属性。 SVG 中的有效值是“无”或不透明度 = 0 的填充颜色。

    我在这里找到了一个关于如何确定填充颜色的链接: https://matplotlib.org/2.1.1/api/_as_gen/matplotlib.pyplot.plot.html

    希望你能从那里找出其余部分。

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2020-04-03
      相关资源
      最近更新 更多