Skip to content

Commit

Permalink
Enable offset, scientific notation and math text to format tick labels
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Dec 15, 2023
1 parent 12dc315 commit 1274f85
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/silx/gui/plot/backends/BackendMatplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,21 +541,9 @@ def __init__(self, plot, parent=None):
# Set axis zorder=0.5 so grid is displayed at 0.5
self.ax.set_axisbelow(True)

# disable the use of offsets
try:
axes = [
self.ax.get_yaxis().get_major_formatter(),
self.ax.get_xaxis().get_major_formatter(),
self.ax2.get_yaxis().get_major_formatter(),
self.ax2.get_xaxis().get_major_formatter(),
]
for axis in axes:
axis.set_useOffset(False)
axis.set_scientific(False)
except:
_logger.warning(
"Cannot disabled axes offsets in %s " % matplotlib.__version__
)
# Configure axes tick label formatter
for axis in (self.ax.yaxis, self.ax.xaxis, self.ax2.yaxis, self.ax2.xaxis):
self.__setAxisFormatter(axis)

self.ax2.set_autoscaley_on(True)

Expand All @@ -575,6 +563,13 @@ def __init__(self, plot, parent=None):
self._enableAxis("right", False)
self._isXAxisTimeSeries = False

@staticmethod
def __setAxisFormatter(axis):
"""Configure matplotlib Axis formatter"""
formatter = ScalarFormatter(useOffset=True, useMathText=True)
formatter.set_scientific(True)
axis.set_major_formatter(formatter)

def getItemsFromBackToFront(self, condition=None):
"""Order as BackendBase + take into account matplotlib Axes structure"""

Expand Down Expand Up @@ -1267,14 +1262,7 @@ def setXAxisTimeSeries(self, isTimeSeries):
NiceAutoDateFormatter(locator, tz=self.getXAxisTimeZone())
)
else:
try:
scalarFormatter = ScalarFormatter(useOffset=False)
except:
_logger.warning(
"Cannot disabled axes offsets in %s " % matplotlib.__version__
)
scalarFormatter = ScalarFormatter()
self.ax.xaxis.set_major_formatter(scalarFormatter)
self.__setAxisFormatter(self.ax.xaxis)

def setXAxisLogarithmic(self, flag):
# Workaround for matplotlib 2.1.0 when one tries to set an axis
Expand Down

0 comments on commit 1274f85

Please sign in to comment.