用于Corel Draw的Matplotlib输出为PDF

时间:2016-05-31 06:10:34

标签: python pdf matplotlib pdf-generation coreldraw

更新:字体问题是通过使用rc(“pdf”,fonttype = 42)来解决的,但不幸的是它与另一个结合 - 每当使用任何类型的标记我试过CorelDraw自带错误“文件已损坏”。

当我将Matplotlib中的图表输出为PDF时,我无法在Corel Draw中打开它。我非常怀疑主要问题可能是文本/字体。

我需要更新的简单代码示例,以便在Corel Draw中正确导入带有文本和标记的PDF:

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
from matplotlib import rc
rc("pdf", fonttype=42)

with PdfPages("simple.pdf") as pdf:
  points_y = points_x = [1,2,3]
  plt.plot(points_x, points_y, marker="o")
  pdf.savefig()
  plt.close()

未使用rc(“pdf”,fonttype = 42)和标记时Corel与Matplotlib / PDF阅读器的示例。如果标记使用的PDF没有打开,CorelDraw说“文件已损坏”。 Example of Corel vs Matplotlib / PDF Reader

1 个答案:

答案 0 :(得分:3)

事实证明,有两个重要问题导致Matplotlib导入的PDF导入到CorelDraw中。

  1. 设置字体类型,从默认rc("pdf", fonttype=3)rc("pdf", fonttype=42)
  2. 不使用多个标记。每个地块只允许一个标记!可以用文字替换。(没有pyplot.scatter,不在pyplot.plot等)。当每个图表使用2个或更多的任何标记时,CorelDraw会发现PDF已损坏并且根本不会打开它。
  3. 重写代码,每个情节只绘制一个标记,在图例中只绘制一个标记:

    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    from matplotlib import rc
    rc("pdf", fonttype=42)
    
    with PdfPages("simple.pdf") as pdf:
        points_y = points_x = [1,2,3]
        plt.plot(points_x, points_y,color="r")
        # plot points one be one otherwise "File is corrupted" in CorelDraw
        # also plot first point out of loop to make appropriate legend
        plt.plot(points_x[0], points_y[0],marker="o",color="r",markerfacecolor="b",label="Legend label")
        for i in range(1,len(points_x)):
            plt.plot(points_x[i], points_y[i],marker="o",markerfacecolor="b")
        plt.legend(numpoints=1) #Only 1 point in legend because in CorelDraw "File is corrupted" if default two or more 
        pdf.savefig()
        plt.close()
    

    作为点(标记)的可能替代品,可以使用pyplot.text,对于我的例子,更新的代码如下所示:

    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    from matplotlib import rc
    rc("pdf", fonttype=42)
    
    with PdfPages("simple.pdf") as pdf:
        points_y = points_x = [1,2,3]
        plt.plot(points_x, points_y)
        # print points as + symbol
        for i in range(len(points_x)):
            plt.text(points_x[i], points_y[i],"+",ha="center", va="center")
        pdf.savefig()
        plt.close()