简体   繁体   English

matplotlib位图图与矢量文本

[英]matplotlib bitmap plot with vector text

So, I'm plotting a waveform (and other things) that result in a bigger vector file (PDF) than the corresponding raster file (PNG). 所以,我正在绘制一个波形(和其他东西),导致比相应的光栅文件(PNG)更大的矢量文件(PDF)。 I imagine this is because the dataset plotted is very large and there are millions of instructions in the vector file. 我想这是因为绘制的数据集非常大,矢量文件中有数百万条指令。 Other than being bigger, the PDF is also quite hard for the PDF reader to display. 除了更大,PDF也很难显示PDF阅读器。 On some, it takes a few seconds to load; 在某些情况下,加载需要几秒钟; on others, it doesn't load at all. 在其他人,它根本没有加载。

In pyplot, is it possible to have a bitmap plot with vector axes, labels and all other text? 在pyplot中,是否可以使用矢量轴,标签和所有其他文本的位图图?

My (very bad) solution at the moment is to generate the PDF, generate the PNG, open the PDF with inkscape and replace the plot with the PNG one. 我(非常糟糕)的解决方案是生成PDF,生成PNG,用inkscape打开PDF并用PNG替换图。 Obviously this is too manual and very time consuming if you realise you have to regenerate the plot. 显然,如果你意识到你必须重新生成情节,这太手动而且非常耗时。

It should be as simple as passing in rasterized=True to the plot command. 它应该像将rasterized=True传递给plot命令一样简单。

Eg 例如

import matplotlib.pyplot as plt

plt.plot(range(10), rasterized=True)
plt.savefig('test.pdf')

For me, this results in a pdf with a rasterized line (the resolution is controlled by the dpi you specified with savefig -- by default, it's 100) and vector text. 对我来说,这导致带有光栅化线的pdf(分辨率由您使用savefig指定的dpi控制 - 默认情况下为100)和矢量文本。

I use a dirty "fix" for this problem. 我使用脏的“修复”来解决这个问题。 I simply produce the plot twice. 我只是制作了两次情节。 Once I remove all the frames, titles, etc. and save as a png and in the other case, I remove the actual data and save all the components that I want as vector data in a pdf. 一旦我删除了所有的框架,标题等并保存为png,在另一种情况下,我删除实际数据并将我想要的所有组件保存为pdf中的矢量数据。 Then I use ImageMagick to convert the png into a pdf containing bitmap data and overlay the vector data from the pdf using pdftk. 然后我使用ImageMagick将png转换为包含位图数据的pdf,并使用pdftk覆盖pdf中的矢量数据。 Here is a pcolor example from the matplotlib page adapted in the way I just described. 这是matplotlib页面中的一个pcolor示例,它以我刚才描述的方式进行了调整。

import matplotlib.pyplot as plt
import numpy as np
import os

for case in ['frame','data']:

    # make these smaller to increase the resolution                                                                                                  
    dx, dy = 0.02, 0.02

    # generate 2 2d grids for the x & y bounds                                                                                                       
    y, x = np.mgrid[slice(-3, 3 + dy, dy),
                    slice(-3, 3 + dx, dx)]
    z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
    # x and y are bounds, so z should be the value *inside* those bounds.                                                                            
    # Therefore, remove the last value from the z array.                                                                                             
    z = z[:-1, :-1]
    z_min, z_max = -np.abs(z).max(), np.abs(z).max()


    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    im=plt.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
    plt.title('pcolor')
    # set the limits of the plot to the limits of the data                                                                                           
    plt.axis([x.min(), x.max(), y.min(), y.max()])

    if case is 'frame':
        im.remove()
        plt.savefig("frame.pdf",transparent=True)
    if case is 'data':
        ax.axison=False
        plt.title('')
        plt.savefig("data.png",transparent=True)



os.system('convert data.png data.pdf')
os.system('pdftk frame.pdf background data.pdf output final_plot.pdf')
os.system('rm data.png data.pdf frame.pdf')

Basically it is just an automatized version of what you are already doing... 基本上它只是你正在做的自动化版本......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM