Pandas plot dataframe by index, how it works?
<p>Given the data:</p>

<pre><code>Column1; Column2; Column3
1; 4; 6
2; 2; 6
3; 3; 8
4; 1; 1
5; 4; 2
</code></pre>

<p>With the following code I get the following graphic:</p>

<pre><code>import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('test0.csv',delimiter='; ', engine='python')
df.plot(0,0)
plt.savefig('fig0.png')
</code></pre>

<p><a href="http://i.stack.imgur.com/tEQ6A.png" rel="nofollow"><img src="http://i.stack.imgur.com/tEQ6A.png" alt="enter image description here"></a></p>

<p>And, with the following code I get the following graphic:</p>

<pre><code>import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('test0.csv',delimiter='; ', engine='python')
df.plot(0,1)
plt.savefig('fig1.png')
</code></pre>

<p><a href="http://i.stack.imgur.com/dAtwA.png" rel="nofollow"><img src="http://i.stack.imgur.com/dAtwA.png" alt="enter image description here"></a></p>

<p>What's the logic in <code>df.plot(m,n)</code>? Let's say I want to plot <code>Column2 X Column3</code>  what's <code>m</code> and <code>n</code>(<code>df.plot(2,3)</code>) ? </p>

<p>When you specify <code>x</code> and <code>y</code> by ordinal position, you'll reach <a href="https://github.com/pydata/pandas/blob/master/pandas/tools/plotting.py#L2449" rel="nofollow">this bit of code</a> when calling <code>df.plot(x, y)</code>:</p>

<pre><code>    if isinstance(data, DataFrame):
        if x is not None:
            if com.is_integer(x) and not data.columns.holds_integer():
                x = data.columns[x]
            data = data.set_index(x)
</code></pre>

<p>This converts <code>x</code> from an ordinal value (e.g. 0) to a column label name (e.g. <code>'Column1'</code>).
Notice that <code>data.set_index(x)</code> is called. So if <code>x</code> equals 0, the first column gets set as the index. Next, when <code>y</code> is similarly converted into a column label,
the 0th column is now <code>'Column2'</code>.  Hence, <code>df.plot(0,0)</code> plots Column2 versus Column1.</p>

<p>To plot Column3 versus Column2 you would therefore use <code>df.plot(1,1)</code>, since <code>data.columns[1]</code> is <code>'Column2'</code>, and then once it is set as the index, <code>data.columns[1]</code> is then <code>'Column3'</code>.</p>

<hr>

<p>As <a href="http://stackoverflow.com/questions/39936983/pandas-plot-dataframe-by-index-how-it-works/39937159#comment67156153_39936983">BrenBarn points out</a> in the comments, a much less error-prone way of specifying the columns is by label name. 
Or, if you really want to use ordinal values, pass them to <code>df.columns</code> explicitly:</p>

<pre><code>df.plot(x=df.columns[x], y=df.columns[y])
</code></pre>

<p>Since <code>df.columns[x]</code> and <code>df.columns[y]</code> are column label names, their meaning is not affected by <code>set_index</code>, so there is less confusion.</p>

python python-2.7 pandas matplotlib dataframe