我想在我的子图的顶部和底部(或左侧和右侧)生成带有轴标签的图。其他帖子详细说明了我如何移动轴标签(例如,here)。
但是,我对科学记数法的指数有问题。最小化的工作示例最好地说明了问题:
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")
这会产生:
正如您所看到的,指数没有与其余的刻度一起移动。我试过了
ax.get_yaxis().get_offset_text().offset_text_position = "right"
和
ax.get_xaxis().get_offset_text().offset_text_position = "top"
但它们似乎没有效果。
答案 0 :(得分:3)
这是Joe Klington报道的错误: https://github.com/matplotlib/matplotlib/issues/4476 如果您查看该链接,似乎有一种解决方法可以将文本偏移到您想要的位置,但这并不简单。