How modules know each other
<p>I can plot data from a CSV file with the following code:</p>

<pre><code>import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('test0.csv',delimiter='; ', engine='python')
df.plot(x='Column1', y='Column3')
plt.show()
</code></pre>

<p>But I don't understand one thing. How <code>plt.show()</code> knows about <code>df</code>? I'll make more sense to me seeing, somewhere, an expression like:</p>

<pre><code>plt = something(df)
</code></pre>

<p>I have to mention I'm just learning Python.</p>

<p>Matplotlib has two "interfaces": a <a href="http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html" rel="nofollow">Matlab-style interface</a> and an <a href="http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut2.html" rel="nofollow">object-oriented interface</a>. </p>

<p>Plotting with the Matlab-style interface looks like this:</p>

<pre><code>import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()
</code></pre>

<p>The call to <code>plt.plot</code> implicitly creates a figure and an axes on which to draw.
The call to <code>plt.show</code> displays all figures.</p>

<p>Pandas is supporting the Matlab-style interface by implicitly creating a figure and axes for you when <code>df.plot(x='Column1', y='Column3')</code> is called.</p>

<p>Pandas can also use the more flexible object-oriented interface, in which case
your code would look like this:</p>

<pre><code>import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('test0.csv',delimiter='; ', engine='python')
fig, ax = plt.subplots()
df.plot(ax=ax, x='Column1', y='Column3')
plt.show()
</code></pre>

<p>Here the axes, <code>ax</code>, is explicitly created and passed to <code>df.plot</code>, which then
calls <code>ax.plot</code> under the hood.</p>

<p>One case where the object-oriented interface is useful is when you wish to use
<code>df.plot</code> more than once while still drawing on the same axes:</p>

<pre><code>fig, ax = plt.subplots()
df.plot(ax=ax, x='Column1', y='Column3')
df2.plot(ax=ax, x='Column2', y='Column4')
plt.show()
</code></pre>

python python-2.7