pyplot: Add graph to existing plot
<p>In order to draw two graphs in one plot with python's matplotlib.pyplot, one would typically do this:</p>

<pre><code>import matplotlib.pyplot as plt

xdata = [0, 1, 2, 3, 4]
ydata1 = [0, 1, 2, 3, 4]
ydata2 = [0, 0.5, 2, 4.5, 8]

plt.plot(xdata, ydata1, 'r-', xdata, ydata2, 'b--')
plt.show()
</code></pre>

<p>However, I would like to draw the second dataset only under certain circumstances, like this:</p>

<pre><code>plt.plot(xdata, ydata1, 'r-')

if DrawSecondDataset:
    plt.plot(data, ydata2, 'b--')
</code></pre>

<p>Unfortunately, calling <code>plot</code> for the second time means that the first dataset is erased.</p>

<p>How can you add a graph to an already existing plot?</p>

<p>EDIT:</p>

<p>As the answers pointed out correctly, the dataset is only erased if <code>plt.show()</code> has been called between the two <code>plt.plot()</code> commands. Thus, the example above actually shows both datasets.</p>

<p>For completeness: Is there an option to add a graph to an existing plot on which <code>plt.show()</code> has already been called? Such as</p>

<pre><code>plt.plot(xdata, ydata1, 'r-')
plt.show()

...

plt.plot(data, ydata2, 'b--')
plt.show()
</code></pre>