Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving axis exponential in matplotlib

I want to produce plots with axis labels both at the tops and bottoms (or left and right) of my subfigures. Other posts have detailed how I can move the axis labels (for example, here).

However, I'm having a problem with the exponentials from scientific notation. A minimal-ish working example illustrates the problem best:

import numpy as np
import matplotlib.pyplot as plt

vars = 4
length = 10000

array = np.zeros((length, vars))
array[:,0] = np.random.normal(0.0, 1.0e6, (length))
array[:,1] = 1.0e6 * np.tanh(array[:,0]/1.0e6)
array[:,2] = 1.0e5 * np.random.random((length))
array[:,3] = array[:,1] * array[:,2] / 1.0e5

labels = ["var1", "var2", "var3", "var4"]

plt.figure(figsize=(15, 15))

for i in range(vars):
    for j in range(0, vars):
        ax = plt.subplot(vars, vars, vars*i + j + 1)
        ax.xaxis.get_major_formatter().set_powerlimits((-2, 3))
        ax.yaxis.get_major_formatter().set_powerlimits((-2, 3))
        ax.locator_params(nbins=5)

        if (j == i):
            weights = np.ones_like(array[:,i])/len(array[:,i])
            ax.hist(array[:,i], 25, weights=weights)
        else:
            ax.plot(array[:,j], array[:,i], '.', markersize=0.5)

        if ((j == 0) & (i % 2 == 0)):
            ax.yaxis.tick_left()
            ax.xaxis.set_ticks_position("both")
            ax.yaxis.set_label_position("left")
            ax.get_yaxis().get_offset_text().offset_text_position = "left"
            ax.set_ylabel(labels[i])
        elif ((j == vars - 1) & (i % 2 == 1)):
            ax.yaxis.tick_right()
            ax.xaxis.set_ticks_position("both")
            ax.yaxis.set_label_position("right")
            ax.get_yaxis().get_offset_text().offset_text_position = "right"
            ax.set_ylabel(labels[i])
        else:
            ax.get_yaxis().set_ticklabels([])

        if ((i == 0) & (j % 2 == 1)):
            ax.xaxis.tick_top()
            ax.xaxis.set_ticks_position("both")
            ax.xaxis.set_label_position("top")
            ax.get_xaxis().get_offset_text().offset_text_position = "top"
            ax.set_xlabel(labels[j])
        elif ((i == vars - 1) & (j % 2 == 0)):
            ax.xaxis.tick_bottom()
            ax.xaxis.set_ticks_position("both")
            ax.xaxis.set_label_position("bottom")
            ax.get_xaxis().get_offset_text().offset_text_position = "bottom"
            ax.set_xlabel(labels[j])
        else:
            ax.get_xaxis().set_ticklabels([])

plt.savefig("test.png")

This produces: enter image description here

As you can see, the exponentials have not moved with the rest of the ticks. I tried

ax.get_yaxis().get_offset_text().offset_text_position = "right"

and

ax.get_xaxis().get_offset_text().offset_text_position = "top"

but they seem to have no effect.

like image 484
Jack Yates Avatar asked May 26 '15 13:05

Jack Yates


1 Answers

This is the bug that Joe Klington reported: https://github.com/matplotlib/matplotlib/issues/4476 If you look at that link, there seems to be a workaround to position to offset text where you want, but it is not simple.

like image 140
Ramon Crehuet Avatar answered Oct 06 '22 12:10

Ramon Crehuet