这个“简单”的问题让我失去了理智:
在 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)
提前致谢! (顺便说一句,Python 版本 = 2.7.6,matplotlib 版本 = 1.3.1 - 在我完成当前项目之前升级目前不是一个选项)
最佳答案
通常不可能更改 offsetText 标签的位置。这仍然是一个 open issue .
因此,一个解决方案是覆盖 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()
如果颜色条位于绘图的左侧,则以下内容可能看起来更好:
self.offsetText.set(va="top", ha="right")
self.offsetText.set_position(
(1, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72.0))
关于python - Colorbar offsetText(科学基础乘数)从colorbar的顶部移动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43718275/