首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得emf/wmf格式的matplotlib图?

如何获得emf/wmf格式的matplotlib图?
EN

Stack Overflow用户
提问于 2018-11-07 18:36:52
回答 2查看 7.8K关注 0票数 8

我如何才能获得emf或wmf文件的matplotlib图,这些文件在MS Office (Word和PowerPoint)中可用作矢量图形?

我尝试过导出到svg,并使用Inkscape和LibreOffice绘图转换为emf,但这两个选项似乎都会导致图像质量的下降,从而导致光栅图像。

我也尝试过导出到pdf,并转换到emf/wmf,但这也有同样的问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-14 00:42:05

下面是我创建WMF和SVG的解决方案。您可以安装Inkscape并使用以下类,“SaveAndClosePlot”创建SVG,然后使用它转换为WMF的Inkscape。TestPlot函数可以根据您的需要定制。

代码语言:javascript
复制
import os
from pathlib import Path
from ConfigParserM import logging
import subprocess
from matplotlib import pyplot as plt


class SVG_WMF_Plot:

    def __init__(self):
        self.__folderNameGraph = 'Graphs'
        self.__WMF_SVGSaving = True
        self.__inkScapePath = "C://Program Files//inkscape//inkscape.exe"
        self.__figureDPI = 500

    def getRootDirectory(self):
        try:
            return Path(os.path.dirname(os.path.realpath('__file__')))

        except Exception as e:
            logging.exception(e)
            raise

    def getAddressTo(self, Main=None, FolderName=None, FileName=None, Extension=None):
        try:
            if Main is None:
                Main = self.getRootDirectory()
            if FolderName:
                Path1 = Path(Main) / Path(FolderName)
            else:
                Path1 = Path(Main)

            if not os.path.exists(Path1):
                os.makedirs(Path1)
            if FileName:
                if Extension:
                    File_Address = Path1 / Path(FileName + "." + Extension)
                else:
                    File_Address = Path1 / Path(FileName)
            else:
                File_Address = Path1
            return File_Address

        except Exception as e:
            logging.exception(e)
            raise

    def TestPlot(self):
        try:

            fig, ax1 = plt.subplots()
            x = [1, 2]
            y = [1, 2]
            F1 = 'test'
            ax1.plot(x, y)
            self.SaveAndClosePlot(folderName=self.__folderNameGraph, fileName=F1)


        except Exception as e:
            logging.exception(e)
            raise

    def SaveAndClosePlot(self, folderName, fileName):
        try:
            Address = self.getAddressTo(FolderName=self.__folderNameGraph + f"\{folderName}", FileName=fileName, Extension="jpg")
            plt.savefig(Address, format='jpg', dpi=self.__figureDPI, bbox_inches='tight')

            if self.__WMF_SVGSaving:
                Address = self.getAddressTo(FolderName=self.__folderNameGraph + f"\{folderName}", FileName=fileName, Extension="svg")
                plt.savefig(Address, format='svg', dpi=self.__figureDPI, bbox_inches='tight')
                # add removing SVG if needed

                AddressWMF = self.getAddressTo(FolderName=self.__folderNameGraph + f"\{folderName}", FileName=fileName, Extension="wmf")
                subprocess.call([self.__inkScapePath, str(Address.resolve()), '--export-wmf', str(AddressWMF.resolve())])

            plt.clf()
            plt.close()
        except Exception as e:
            logging.exception(e)
            raise
票数 1
EN

Stack Overflow用户

发布于 2021-03-03 03:36:17

若要使用Linux将数字保存为matplotlib中的.emf文件,请尝试以下操作:

  1. 安装Inkscape (我有在Ubuntu中安装Inkscape 0.92.4 16.04 )。其他版本应该一样工作)
  2. matplotlib中,将图形保存为.svg,然后通过Inkscape子进程调用将其转换为.emf。例如:
代码语言:javascript
复制
    import numpy as np
    import subprocess
    import matplotlib.pyplot as plt
    
    x = np.arange(2,50,step=2)
    y = x**2
    plt.plot(x,y)
    plt.savefig('y_is_x^2.svg', format='svg', bbox_inches='tight')
    subprocess.call('inkscape y_is_x^2.svg -M y_is_x^2.emf',shell=True)

然后,您可以将.emf图作为图片插入MS Word或PowerPoint中。质量接近.svg。不过,请注意,大型.svg文件可能无法工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53195714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档