python - How to move the y axis scale factor to the position next to the y axis label? -
i have data plotted force scientific notation powers of 10 (instead of exponential). heres snippet of code:
import matplotlib.ticker mticker formatter = mticker.scalarformatter(usemathtext=true) formatter.set_powerlimits((-3,2)) ax.yaxis.set_major_formatter(formatter) however, scale factor of x10^-4 appears on top left hand corner of graph.
is there simple method force position of scale factor next y label have illustrated in diagram below?
you may set offset invisible, such not appear in original position.
ax.yaxis.offsettext.set_visible(false) you may offset formatter update label it
offset = ax.yaxis.get_major_formatter().get_offset() ax.yaxis.set_label_text("original label" + " " + offset) such appears inside label.
the following automates using class callback, such if offset changes, updated in label.
import numpy np import matplotlib.pyplot plt import matplotlib.ticker mticker class labeloffset(): def __init__(self, ax, label="", axis="y"): self.axis = {"y":ax.yaxis, "x":ax.xaxis}[axis] self.label=label ax.callbacks.connect(axis+'lim_changed', self.update) ax.figure.canvas.draw() self.update(none) def update(self, lim): fmt = self.axis.get_major_formatter() self.axis.offsettext.set_visible(false) self.axis.set_label_text(self.label + " "+ fmt.get_offset() ) x = np.arange(5) y = np.exp(x)*1e-6 fig, ax = plt.subplots() ax.plot(x,y, marker="d") formatter = mticker.scalarformatter(usemathtext=true) formatter.set_powerlimits((-3,2)) ax.yaxis.set_major_formatter(formatter) lo = labeloffset(ax, label="my label", axis="y") plt.show() 

Comments
Post a Comment