Positioning the exponent of tick labels when using scientific notation in matplotlib
<p>I am looking for a way to change the position of the exponent on an axis when using scientific notation. I got stuck at this problem already a few times. I know already that the default formatter is the <code>ScalarFormatter</code> but it has no option to access the exponent somehow.</p>

<p>There is a <a href="http://werthmuller.org/blog/2014/move-scientific-notation/" rel="nofollow">workaround</a> but I don't like it, since it also manipulates the existing ticklabels. So far I found out, that the list returned by the axes' <code>get_xmajorticklabels()</code> method contains one text object more if scientific notation is used. For example:</p>

<pre><code>import matplotlib.pyplot as plt
import numpy as np

fig= plt.figure(figsize = plt.figaspect(0.5))
ax1= fig.add_subplot(121)
ax1.plot(np.arange(146), np.random.rand(146))
print(ax1.get_xmajorticklabels())

ax2= fig.add_subplot(122)
ax2.plot(np.arange(146)*1e-6, np.random.rand(146))
print(ax2.get_xmajorticklabels())
</code></pre>

<p><img src="http://i.stack.imgur.com/iq4iQ.png" alt="Image with the resulting plots"></p>

<p>The prints give:  <code>&lt;a list of 9 Text xticklabel objects&gt;</code> and <code>&lt;a list of 10 Text xticklabel objects&gt;</code>
So I thought the additional list item could be the text object for the exponent. But it's empty when I print the text.</p>

<p>Is there any way to access this exponent as a text object? Then it should be possible to set its position, isn't it?</p>

<p>To access the list of <a href="http://matplotlib.org/api/text_api.html#matplotlib.text.Text" rel="nofollow">Text objects</a> you can use a method of that class, e.g. <code>get_text()</code>:</p>

<pre><code>print([s.get_text() for s in ax2.get_xmajorticklabels()])
</code></pre>

<p>However, the result of that is</p>

<pre><code>&lt;a list of 9 Text xticklabel objects&gt;
[u'', u'', u'', u'', u'', u'', u'', u'', u'']
&lt;a list of 10 Text xticklabel objects&gt;
[u'', u'', u'', u'', u'', u'', u'', u'', u'', u'']
</code></pre>

<p>After running <code>fig.tight_layout()</code>, the output of these <code>Text xticklabel objects</code> can now be enumerated:</p>

<pre><code>&lt;a list of 9 Text xticklabel objects&gt;
[(0.0, 0), (20.0, 0), (40.0, 0), (60.0, 0), (80.0, 0), (100.0, 0), (120.0, 0), (140.0, 0), (160.0, 0)]

&lt;a list of 10 Text xticklabel objects&gt;
[(0.0, 0), (2.0000000000000002e-05, 0), (4.0000000000000003e-05, 0), (6.0000000000000008e-05, 0), (8.0000000000000007e-05, 0), (0.0001, 0), (0.00012000000000000002, 0), (0.00014000000000000001, 0), (0.00016000000000000001, 0), (0, 0)]
</code></pre>

<p>For an exponent like <code>-7</code>, there is actually the same amount of objects in both lists.</p>

<p>The closest method I've found for positioning the label is detailed <a href="http://stackoverflow.com/a/30772807/6779606">here by Scott</a>. Sadly, it will only work horizontally for the x-axis and vertically for the y-axis, so you can't really arbitrarily position the label on the graph.</p>

<pre><code>ax2.get_xaxis().get_offset_text().set_position((0.5,0))
</code></pre>

<p><a href="http://i.stack.imgur.com/9mGT9.png" rel="nofollow"><img src="http://i.stack.imgur.com/9mGT9.png" alt="exponent label moved horizontally"></a></p>

python matplotlib