2

I am working on a project where I am generating hundreds of plots using the matplotlib module in Python. 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. 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. 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?

Screenshot of pptx generated

result

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. I want to avoid this overlapping.

William Miller
  • 9,839
  • 3
  • 25
  • 46
Sud_Hashira
  • 29
  • 1
  • 3
  • 1
    Show the code that you are using to send the plots using python-pptx, see [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Diziet Asahi Jan 22 '20 at 12:42

1 Answers1

1

You need to clear the axes between each plot, there are several ways to do that:

  • plt.clf(): clears the current figure
  • plt.cla(): clears the current axes

So instead of e.g.

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.

William Miller
  • 9,839
  • 3
  • 25
  • 46