How to prevent the left x axis from extending to the right x axis in matplotlib?
<p>I'm trying to create two histograms next to each other. My problem is the <code>x</code> labels for the left one is extending to the one on the right as shown below:
<a href="http://i.stack.imgur.com/oiosk.png" rel="nofollow"><img src="http://i.stack.imgur.com/oiosk.png" alt="enter image description here"></a></p>

<p>Here is how I'm setting up the plot</p>

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

fig = plt.figure(figsize=(16,8))
ax1 = fig.add_subplot(1,1,1)
ax1.set_xlim([min(df1["Age"]),max(df1["Age"])])
ax1 = df1["Age"].hist(color="cornflowerblue")
ax2 = fig.add_subplot(1,2,2)
ax2.set_xlim([min(df2["Age"]),max(df2["Age"])])
ax2 = df2["Age"].hist(color="seagreen")
plt.show()
</code></pre>

<p>I want plot one x axis to show for each <code>subplot</code> so the first one will include the ages from <code>min(df1["Age"])</code> to <code>max(df1["Age"])</code> and another x axis. The second one will include the ages from <code>min(df2["Age"])</code> to <code>max(df2["Age"])</code>. How can I do that</p>

<p>The issue is that your first subplot added with <code>ax1 = fig.add_subplot(1, 1, 1)</code> will fill the entire figure.  The second subplot <code>ax2 = fig.add_subplot(1, 2, 2)</code> will span the right hand side, as if there was a first subplot to the left (which there isn't). What you should do if you want to have two subplots, half figure-size in width, is to use</p>

<pre><code>fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)  # first subplot, to the left
ax2 = fig.add_subplot(1, 2, 2)  # second subplot, to the right
</code></pre>

<p>Another neater way to do it is to use the <code>plt.subplots</code>-function.  That creates the figure and the two axes with one call, as</p>

<pre><code>fig, (ax1, ax2) = plt.subplots(1, 2)
</code></pre>

<p>Below are three images showing what goes wrong. First figure is the result after adding ax1 in your code (not in my code!).  Then you add ax2, giving the second figure, where it is obvious that half of the original ax1 is covered by the new ax2.  However the third figure shows two axes, side by side, which is what you what I guess.</p>

<p>Clarification edit:  when calling <code>plt.add_subplot(rows, cols, num)</code>, the <code>num</code>-parameter tells which subplot to add.  I.e. if <code>rows = cols = 2</code>, <code>num = 1</code> corresponds to the upper left, <code>num = 2</code> to the upper right, <code>num = 3</code> to the bottom left and <code>num = 4</code> to the bottom right.  This means that you can add i.e. the top right subplot (in a 2 x 2 grid) with <code>fig.add_subplot(2, 2, 2)</code>, see figure 4 below.</p>

<p><a href="http://i.stack.imgur.com/YXe81.png" rel="nofollow"><img src="http://i.stack.imgur.com/YXe81.png" alt="Only ax1 added"></a>
(Fig1: Only ax1 added from your code)
<a href="http://i.stack.imgur.com/G5T0W.png" rel="nofollow"><img src="http://i.stack.imgur.com/G5T0W.png" alt="enter image description here"></a>
(Fig2: Adding ax2 as well, from your code)
<a href="http://i.stack.imgur.com/G7blA.png" rel="nofollow"><img src="http://i.stack.imgur.com/G7blA.png" alt="enter image description here"></a>
(Fig3: Adding ax1 and ax2 in the proper way, side by side)
<a href="http://i.stack.imgur.com/26OFN.png" rel="nofollow"><img src="http://i.stack.imgur.com/26OFN.png" alt="enter image description here"></a>
(Fig4: Only upper right subplot added, with <code>fig.add_subplot(2, 2, 2)</code>.  There are still 3 more spots empty in the 2 x 2 grid.)</p>

python matplotlib histogram