Exponential axis of movement in matplotlib

I want to create graphs with axial marks both at the vertices and at the bottom (or left and right) of my subviews. Other posts have described in detail how I can move axis labels (e.g. here ).

However, I have a problem with exhibitors from scientific notation. A minimal working example illustrates a better problem:

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 gives: enter image description here

As you can see, the exponents did not move 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 do not seem to have an effect.

+6
source share
1 answer

This is the bug Joe Klington reported: https://github.com/matplotlib/matplotlib/issues/4476 If you look at this link, there seems to be a workaround to shift the text where you want, but it's not easy .

+3
source

Source: https://habr.com/ru/post/987941/


All Articles