Plot multiple series with non-overlapping x-axis
<p>I have two dataframes, one contains a subset of samples of the other.</p>

<pre><code>df = pd.DataFrame(data= {'A' : [1,2,3,4,3,2,1]}
               ,index = [1,2,3,4,5,6,7])
df2 = pd.DataFrame(data = {'B' : [0.7, 1.4]}
               ,index = [2,6])
</code></pre>

<p>I'd like to plot these such that the complete dataset is a line plot, while the other is a bar plot.</p>

<p>But trying</p>

<pre><code>ax = df.plot()
df2.plot(kind='bar', ax=ax)
</code></pre>

<p>Gives me</p>

<p><a href="http://i.stack.imgur.com/JO73q.png" rel="nofollow"><img src="http://i.stack.imgur.com/JO73q.png" alt="My ugly graph"></a></p>

<p>What am I missing?</p>

<p><strong>Edit</strong></p>

<p>I just discovered this works</p>

<pre><code>plt.plot(df.index, df['A'])
plt.bar(df2.index, df2['B'])
</code></pre>

<p>What's the functional difference / what fundamentals should I go read about?</p>

<p>You should do it the following way.   </p>

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

matplotlib.style.use('ggplot')

df = pd.DataFrame(data= {'A' : [1,2,3,4,3,2,1]}
                       ,index = [1,2,3,4,5,6,7])
df2 = pd.DataFrame(data = {'B' : [0.7, 1.4]}
                       ,index = [2,6])

df_final = pd.concat([df,df2], axis=2)

plt.figure()

ax = df_final['A'].plot(kind='line', color='y', legend='A')

df_final['B'].plot(kind='bar', color='r', legend='B')

plt.show()
</code></pre>

<p>In your previous method, you miss following points :</p>

<ul>
<li>An index(passed as a number) does not have a numerical significance in terms of ordering. Hence when you have two dataframes separated, and then plot them one after the other, there will be no way for Pandas to know how to align the plots together.</li>
<li>If you used strings as indices, in that case also Pandas would not have automatically aligned the plots together and filled in for the missing values. This is a sensible and expected behavior because different plots with different meanings and contexts might have similar indices. What you see in your original result, is just an overlap of two plots on the same figure with no regard for any kind of alignment.</li>
</ul>

<p>Always align different dataframes together when you want to plot them at the same time in the same figure and upon the same set of axes.</p>

<p><strong>EDITED :
I saw the edit in your question later on and hence adding this.</strong></p>

<p>Your edited one works because now you do not use the Pandas wrapper and directly use matplotlib. Pandas <code>plot()</code> is just a wrapper around matplotlib. In this case, matplotlib does not make any assumption since it is not specifically careful about datasets, and just aligns your column values alongside the appropriate indices. However I would recommend using Pandas for significant plotting applications involving datasets.
See <a href="http://pandas.pydata.org/pandas-docs/stable/visualization.html" rel="nofollow" title="Pandas Documentation">Pandas documentation on visualization</a></p>

python pandas plot