I am trying to plot multiple different plots in one pdf using pylatex and matplotlib. The code below can generate two plots just fine, however, the second plot contains the data from the first plot when I just want two separate plots i.e. (x,y) in one plot and (x_1, y_1) in another.
The issue is that when i use plt.plot
for the second figure, it adds the data for the second graph into the first plot.
MWE code:
import matplotlib
from pylatex import Document, Section, Figure, NoEscape
matplotlib.use('Agg') # Not to use X server. For TravisCI.
import matplotlib.pyplot as plt # noq
if __name__ == '__main__':
x = [0, 1, 2, 3, 4, 5, 6]
y = [15, 2, 7, 1, 5, 6, 9]
plt.plot(x, y)
doc = Document('basic')
doc.append('Introduction.')
with doc.create(Section('I am a section')):
doc.append('Take a look at this beautiful plot:')
with doc.create(Figure(position='htbp')) as plot:
plot.add_plot(width=NoEscape(r'1\linewidth'), dpi=300)
plot.add_caption('I am a caption.')
if __name__ == '__main__':
x_1 = [0, 1, 2, 3, 4, 7, 6]
y_1 = [15, 2, 5, 1, 5, 6, 9]
plt.plot(x_1, y_1)
with doc.create(Section('I am a section')):
doc.append('Take a look at this beautiful plot:')
with doc.create(Figure(position='htbp')) as plot3:
plot2.add_plot(width=NoEscape(r'1\linewidth'), dpi=300)
plot2.add_caption('I am a caption.')
doc.append('Conclusion.')
doc.generate_pdf(clean_tex=False)
I think it has something to do with the plot object being the same for both figures. I am not too sure how to separate them since most searches for plotting separate plots in matplotlib suggest subfigures which is not really applicable here.
Thanks for your help.