<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="plt.title(&quot;title&quot;)
plt.gca().twiny().set_xlabel(&quot;xlabel&quot;)
plt.tight_layout()"><pre class="notranslate"><code class="notranslate">plt.title("title")
plt.gca().twiny().set_xlabel("xlabel")
plt.tight_layout()
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/1322974/11135816/ad60bf1e-895d-11e5-8090-2c93230aaf19.png"><img src="https://cloud.githubusercontent.com/assets/1322974/11135816/ad60bf1e-895d-11e5-8090-2c93230aaf19.png" alt="tight_twiny" style="max-width: 100%;"></a><br>
matplotlib 1.5.0.</p>
<p dir="auto">This is an issue that came up in <a href="http://trac.sagemath.org/13625" rel="nofollow">http://trac.sagemath.org/13625</a></p>
<p dir="auto">When <code class="notranslate">subplot.xaxis.tick_top()</code> is used, then the position of the plot title is not recomputed. Ideally, it should be shifted further up. Example code is as follows</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from matplotlib.figure import Figure
figure = Figure()
subplot = figure.add_subplot(111)
subplot.plot(range(10),range(10))
subplot.xaxis.tick_top()
subplot.set_title('a title')
from matplotlib.backends.backend_agg import FigureCanvasAgg
figure.set_canvas(FigureCanvasAgg(figure))
figure.savefig('/tmp/a.png')"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">figure</span> <span class="pl-k">import</span> <span class="pl-v">Figure</span>
<span class="pl-s1">figure</span> <span class="pl-c1">=</span> <span class="pl-v">Figure</span>()
<span class="pl-s1">subplot</span> <span class="pl-c1">=</span> <span class="pl-s1">figure</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">111</span>)
<span class="pl-s1">subplot</span>.<span class="pl-en">plot</span>(<span class="pl-en">range</span>(<span class="pl-c1">10</span>),<span class="pl-en">range</span>(<span class="pl-c1">10</span>))
<span class="pl-s1">subplot</span>.<span class="pl-s1">xaxis</span>.<span class="pl-en">tick_top</span>()
<span class="pl-s1">subplot</span>.<span class="pl-en">set_title</span>(<span class="pl-s">'a title'</span>)
<span class="pl-k">from</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">backends</span>.<span class="pl-s1">backend_agg</span> <span class="pl-k">import</span> <span class="pl-v">FigureCanvasAgg</span>
<span class="pl-s1">figure</span>.<span class="pl-en">set_canvas</span>(<span class="pl-v">FigureCanvasAgg</span>(<span class="pl-s1">figure</span>))
<span class="pl-s1">figure</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'/tmp/a.png'</span>)</pre></div>