How modules know each other
Question: I can plot data from a CSV file with the following 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()
    

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

    
    
    plt = something(df)
    

I have to mention I'm just learning Python.


Answer: Matplotlib has two "interfaces": a [Matlab-style
interface](http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html) and
an [object-oriented
interface](http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut2.html).

Plotting with the Matlab-style interface looks like this:

    
    
    import matplotlib.pyplot as plt
    plt.plot(x, y)
    plt.show()
    

The call to `plt.plot` implicitly creates a figure and an axes on which to
draw. The call to `plt.show` displays all figures.

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

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

    
    
    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()
    

Here the axes, `ax`, is explicitly created and passed to `df.plot`, which then
calls `ax.plot` under the hood.

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

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

