matplotlib: generating vector plot

前端 未结 4 496
一整个雨季
一整个雨季 2021-02-05 10:37

I want to generate a vector plot with matplotlib. I tried hard - but the output is a raster image. Here\'s what I use:

import matplotlib
matplotlib.use(\'Agg\')
         


        
相关标签:
4条回答
  • 2021-02-05 10:49

    Try exporting as a pdf or svg as described in http://neuroscience.telenczuk.pl/?p=331 If you need an eps the pdf2ps command works great.

    0 讨论(0)
  • 2021-02-05 10:57

    If you need emf files as output format, e.g. to insert high quality plots into ms word/powerpoint and you are willing to use inkscape as converter you can apply this solution:

    from matplotlib import pyplot as plt
    import subprocess, os
    
    def plot_as_emf(figure, **kwargs):
        inkscape_path = kwargs.get('inkscape', "C://Program Files//Inkscape//inkscape.exe")
        filepath = kwargs.get('filename', None)
    
        if filepath is not None:
            path, filename = os.path.split(filepath)
            filename, extension = os.path.splitext(filename)
    
            svg_filepath = os.path.join(path, filename+'.svg')
            emf_filepath = os.path.join(path, filename+'.emf')
    
            figure.savefig(svg_filepath, format='svg')
    
            subprocess.call([inkscape_path, svg_filepath, '--export-emf', emf_filepath])
            os.remove(svg_filepath)
    

    In order to test this function you can run a simple example:

    plt.plot([1,2], [4,5])
    fig = plt.gcf()
    plot_as_emf(fig, filename="C:/test.emf")
    
    0 讨论(0)
  • 2021-02-05 11:02

    I use the following code:

    from matplotlib import pyplot as plt
    
    fig, ax = plt.subplots() # or 
    fig.savefig('filename.eps', format='eps')
    
    0 讨论(0)
  • 2021-02-05 11:13

    Can try for svg format:

    plt.savefig("filepath.svg", format = 'svg', dpi=300)
    
    0 讨论(0)
提交回复
热议问题