Colorbar offsetText (scientific base multiplier) move from top to bottom of colorbar

后端 未结 2 1387
不思量自难忘°
不思量自难忘° 2020-12-18 14:51

I\'m losing my wits here with this \'simple\' problem:

In the colorbar (illustrated in picture) in matplotlib I need to move offsetText (base multiplier) from top of

2条回答
  •  天涯浪人
    2020-12-18 15:13

    It's in general not possible to change the position of the offsetText label. This would still be an open issue.

    A solution can therefor be to overwrite the yaxis' _update_offset_text_position method to position the offsetText on the bottom of the 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()
    

    If the colorbar is positioned on the left side of the plot the following might look better:

    self.offsetText.set(va="top", ha="right")
    self.offsetText.set_position(
                (1, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72.0))
    

提交回复
热议问题