Skip to content

Commit 001162d

Browse files
committed
Fix formatter&locator when switching linear/log scale
1 parent 2181492 commit 001162d

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/silx/gui/plot/backends/BackendMatplotlib.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,23 @@ def setGraphYLimits(self, ymin, ymax, axis):
12381238

12391239
# Graph axes
12401240

1241+
def __initXAxisFormatterAndLocator(self):
1242+
if self.ax.xaxis.get_scale() != "linear":
1243+
return # Do not override formatter and locator
1244+
1245+
if not self.isXAxisTimeSeries():
1246+
self.ax.xaxis.set_major_formatter(DefaultTickFormatter())
1247+
return
1248+
1249+
# We can't use a matplotlib.dates.DateFormatter because it expects
1250+
# the data to be in datetimes. Silx works internally with
1251+
# timestamps (floats).
1252+
locator = NiceDateLocator(tz=self.getXAxisTimeZone())
1253+
self.ax.xaxis.set_major_locator(locator)
1254+
self.ax.xaxis.set_major_formatter(
1255+
NiceAutoDateFormatter(locator, tz=self.getXAxisTimeZone())
1256+
)
1257+
12411258
def setXAxisTimeZone(self, tz):
12421259
super(BackendMatplotlib, self).setXAxisTimeZone(tz)
12431260

@@ -1249,17 +1266,7 @@ def isXAxisTimeSeries(self):
12491266

12501267
def setXAxisTimeSeries(self, isTimeSeries):
12511268
self._isXAxisTimeSeries = isTimeSeries
1252-
if self._isXAxisTimeSeries:
1253-
# We can't use a matplotlib.dates.DateFormatter because it expects
1254-
# the data to be in datetimes. Silx works internally with
1255-
# timestamps (floats).
1256-
locator = NiceDateLocator(tz=self.getXAxisTimeZone())
1257-
self.ax.xaxis.set_major_locator(locator)
1258-
self.ax.xaxis.set_major_formatter(
1259-
NiceAutoDateFormatter(locator, tz=self.getXAxisTimeZone())
1260-
)
1261-
else:
1262-
self.ax.xaxis.set_major_formatter(DefaultTickFormatter())
1269+
self.__initXAxisFormatterAndLocator()
12631270

12641271
def setXAxisLogarithmic(self, flag):
12651272
# Workaround for matplotlib 2.1.0 when one tries to set an axis
@@ -1271,8 +1278,10 @@ def setXAxisLogarithmic(self, flag):
12711278
self.ax.set_xlim(1, 10)
12721279
self.draw()
12731280

1274-
self.ax2.set_xscale("log" if flag else "linear")
1275-
self.ax.set_xscale("log" if flag else "linear")
1281+
xscale = "log" if flag else "linear"
1282+
self.ax2.set_xscale(xscale)
1283+
self.ax.set_xscale(xscale)
1284+
self.__initXAxisFormatterAndLocator()
12761285

12771286
def setYAxisLogarithmic(self, flag):
12781287
# Workaround for matplotlib 2.0 issue with negative bounds
@@ -1290,8 +1299,15 @@ def setYAxisLogarithmic(self, flag):
12901299
if redraw:
12911300
self.draw()
12921301

1293-
self.ax2.set_yscale("log" if flag else "linear")
1294-
self.ax.set_yscale("log" if flag else "linear")
1302+
if flag:
1303+
self.ax2.set_yscale("log")
1304+
self.ax.set_yscale("log")
1305+
return
1306+
1307+
self.ax2.set_yscale("linear")
1308+
self.ax2.yaxis.set_major_formatter(DefaultTickFormatter())
1309+
self.ax.set_yscale("linear")
1310+
self.ax.yaxis.set_major_formatter(DefaultTickFormatter())
12951311

12961312
def setYAxisInverted(self, flag):
12971313
if self.ax.yaxis_inverted() != bool(flag):

0 commit comments

Comments
 (0)