Python 用于Corel绘图的Matplotlib输出为PDF
更新:字体问题实际上是通过使用rc(“pdf”,fonttype=42)解决的,但不幸的是,它与另一个组合在一起-无论何时使用我尝试的任何类型的标记,CorelDraw都会出现错误“文件已损坏” 当我将图表从Matplotlib输出到PDF时,我无法在Corel Draw中打开它。我高度怀疑主要问题可能与文本/字体有关 我需要更新的简单代码示例,以便在Corel Draw中正确导入带有文本和标记的PDF:Python 用于Corel绘图的Matplotlib输出为PDF,python,pdf,matplotlib,pdf-generation,coreldraw,Python,Pdf,Matplotlib,Pdf Generation,Coreldraw,更新:字体问题实际上是通过使用rc(“pdf”,fonttype=42)解决的,但不幸的是,它与另一个组合在一起-无论何时使用我尝试的任何类型的标记,CorelDraw都会出现错误“文件已损坏” 当我将图表从Matplotlib输出到PDF时,我无法在Corel Draw中打开它。我高度怀疑主要问题可能与文本/字体有关 我需要更新的简单代码示例,以便在Corel Draw中正确导入带有文本和标记的PDF: from matplotlib.backends.backend_pdf import P
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)和marker时Corel与Matplotlib/PDF阅读器的对比示例。如果使用的标记无法打开PDF,CorelDraw会说“文件已损坏”。
事实证明,有两个重要问题破坏了Matplotlib生成的PDF导入CorelDraw的过程
rc(“pdf”,fonttype=3)
到rc(“pdf”,fonttype=42)
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()
您需要提供更多信息,例如生成不需要的行为的完整工作示例。如果您希望使用Corel Draw打开/后处理图形,为什么不将它们保存为
svg
specifyingmatplotlib.rcParams['svg.fonttype']='path'
,我用Inkscape对它进行了测试,它确实将字符作为路径而不是字体嵌入其中。@Bart我只在需要一些额外调整的某些情况下进行后期处理。但最终产品总是PDF格式,因为图表是以PDF格式提供给客户的更大研究的一部分。对我来说,最好的解决方案是实际使用文本,而不是曲线,因为尺寸更小,编辑更容易,但我尝试了所有我能找到的设置rcParams的技巧,但没有成功,所以我现在赌的是曲线…请查看您更新的问题;你写了“CorelDraw出现错误并说”然后什么都没有?@Bart谢谢,修复了CorelDraw出现错误“文件已损坏”。如果你最终使用此解决方案,请确保在plt.text
中设置ha='center',va='center'
,否则,与使用plt.plot(marker='+')绘制的标记相比,+
标记将关闭。
@Bart谢谢,是的,我实际上正在使用这些标记。但昨天我发现,只要线上只有一个标记,就可以使用标记(图例也是如此)。因此,使用循环来分别绘制每个点,而不是plt.plot(points_x,points_y,marker=“o”)实际上会生成可由CorelDraw读取的PDF。我将更新我的答案,一旦我测试它多一点。