简体   繁体   English

如何将 matplotlib 生成的多个图发送到 pptx 而没有任何重叠?

[英]How to send multiple plots generated by matplotlib to a pptx without any overlapping?

I am working on a project where I am generating hundreds of plots using the matplotlib module in Python.我正在一个项目中使用 Python 中的 matplotlib 模块生成数百个图。 I want to put these plots in a pptx using the python-pptx module, let's say four plots on a slide without storing these plots on the local disk.我想使用 python-pptx 模块将这些图放在 pptx 中,假设幻灯片上有四个图,而不将这些图存储在本地磁盘上。 To overcome the storing problem I am using the BytesIO python module, which stores the plots inside the buffer, and then send these plots to the pptx.为了克服存储问题,我使用了 BytesIO python 模块,它将绘图存储在缓冲区内,然后将这些绘图发送到 pptx。 The major issue that I am facing is the overlapping of the plots.我面临的主要问题是情节重叠。

Question is how to send these plots serially to pptx so that we can avoid the overlapping?问题是如何将这些图串行发送到 pptx 以避免重叠?

Screenshot of pptx generated生成的pptx截图

结果

I have added a screenshot of the pptx, where I am trying to add the two plots Plot 1 (Age vs Name), Plot 2 (Height vs Name), but if you see the Plot 2 the data of plot 1 and plot 2 are getting overlapped.我添加了 pptx 的屏幕截图,我试图在其中添加两个图Plot 1 (Age vs Name)、 Plot 2 (Height vs Name),但是如果您看到Plot 2 ,则 plot 1 和 plot 2 的数据是重叠。 I want to avoid this overlapping.我想避免这种重叠。

You need to clear the axes between each plot, there are several ways to do that:您需要清除每个图之间的轴,有几种方法可以做到这一点:

  • plt.clf() : clears the current figure plt.clf() :清除当前图形
  • plt.cla() : clears the current axes plt.cla() :清除当前轴

So instead of eg所以而不是例如

plt.scatter(x, y1)
# save first plot
plt.scatter(x, y2)
# save second plot

You do你做

plt.scatter(x, y1)
# save first plot
plt.clf()
plt.scatter(x, y2)
# save second plot

And the two scatter plots will be drawn separately.并且两个散点图将分别绘制。 This is probably the most 'blunt' way to approach this, but it should work fairly well.这可能是解决这个问题的最“直率”的方法,但它应该可以很好地工作。 It is by no means the best way to do it for any specific case.对于任何特定情况,这绝不是最好的方法。

The figure is also cleared when plt.show() is called - but I expect that is undesirable in this case.plt.show()被调用时,这个数字也会被清除 - 但我希望在这种情况下这是不可取的。

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

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