Nem*_*vic 3 python numpy matplotlib colorbar
我对这个“简单”的问题失去了理智:
在 matplotlib 的颜色条(如图所示)中,我需要将 offsetText (基本乘数)从颜色条的顶部移动到底部。
我用于该图的代码是(使用 gridspec):
f.add_subplot(ax12)
ax10 = plt.Subplot(f, gs00[1, 0])
cb = plt.colorbar(h3,cax=ax10)
cb.formatter.set_scientific(True)
cb.formatter.set_powerlimits((0,0))
cb.ax.yaxis.offsetText.set(size=6)
cb.update_ticks()
ax10.yaxis.set_ticks_position('left')
ax10.tick_params(labelsize=6)
f.add_subplot(ax10)
Run Code Online (Sandbox Code Playgroud)
提前致谢!(顺便说一句,Python 版本 = 2.7.6,matplotlib 版本 = 1.3.1 - 在我完成当前项目之前,目前无法升级)
通常不可能更改 offsetText 标签的位置。这仍然是一个悬而未决的问题。
因此,解决方案可以是覆盖 yaxis 的_update_offset_text_position方法,将 offsetText 放置在 yaxis 的底部。
import matplotlib.pyplot as plt
import types
def bottom_offset(self, bboxes, bboxes2):
bottom = self.axes.bbox.ymin
self.offsetText.set(va="top", ha="left")
self.offsetText.set_position(
(0, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72.0))
fig, ax = plt.subplots()
im = ax.imshow([[1e5,2e5],[0.1e5,1e5]])
cb = plt.colorbar(im)
cb.formatter.set_scientific(True)
cb.formatter.set_powerlimits((0,0))
def register_bottom_offset(axis, func):
axis._update_offset_text_position = types.MethodType(func, axis)
register_bottom_offset(cb.ax.yaxis, bottom_offset)
cb.update_ticks()
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果颜色条位于图的左侧,则以下内容可能看起来更好:
self.offsetText.set(va="top", ha="right")
self.offsetText.set_position(
(1, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72.0))
Run Code Online (Sandbox Code Playgroud)