Skip to content

Commit cceb7b4

Browse files
committed
FIX: make matplotlib actually optional
1 parent 8038604 commit cceb7b4

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

larray/util/plot.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
import numpy as np
2-
from matplotlib.ticker import MaxNLocator
32

3+
try:
4+
from matplotlib.ticker import MaxNLocator
45

5-
class MaxNMultipleWithOffsetLocator(MaxNLocator):
6-
def __init__(self, nbins=None, offset=0.5, **kwargs):
7-
super().__init__(nbins, **kwargs)
8-
self.offset = offset
6+
class MaxNMultipleWithOffsetLocator(MaxNLocator):
7+
def __init__(self, nbins=None, offset=0.5, **kwargs):
8+
super().__init__(nbins, **kwargs)
9+
self.offset = offset
910

10-
def tick_values(self, vmin, vmax):
11-
# matplotlib calls them vmin and vmax but they are actually the limits and vmin can be > vmax
12-
invert = vmin > vmax
13-
if invert:
14-
vmin, vmax = vmax, vmin
11+
def tick_values(self, vmin, vmax):
12+
# matplotlib calls them vmin and vmax but they are actually the limits and vmin can be > vmax
13+
invert = vmin > vmax
14+
if invert:
15+
vmin, vmax = vmax, vmin
1516

16-
max_desired_ticks = self._nbins
17-
# not + 1 because we place ticks in the middle
18-
num_ticks = vmax - vmin
19-
desired_numticks = min(num_ticks, max_desired_ticks)
20-
if desired_numticks < num_ticks:
21-
step = np.ceil(num_ticks / desired_numticks)
22-
else:
23-
step = 1
24-
vmin = int(vmin)
25-
vmax = int(vmax)
26-
# when we have an offset, we do not add 1 to vmax because we place ticks in the middle
27-
# (by adding the offset), and would result in the last "tick" being outside the limits
28-
stop = vmax + 1 if self.offset == 0 else vmax
29-
new_ticks = np.arange(vmin, stop, step)
30-
if invert:
31-
new_ticks = new_ticks[::-1]
32-
return new_ticks + self.offset
17+
max_desired_ticks = self._nbins
18+
# not + 1 because we place ticks in the middle
19+
num_ticks = vmax - vmin
20+
desired_numticks = min(num_ticks, max_desired_ticks)
21+
if desired_numticks < num_ticks:
22+
step = np.ceil(num_ticks / desired_numticks)
23+
else:
24+
step = 1
25+
vmin = int(vmin)
26+
vmax = int(vmax)
27+
# when we have an offset, we do not add 1 to vmax because we place ticks in the middle
28+
# (by adding the offset), and would result in the last "tick" being outside the limits
29+
stop = vmax + 1 if self.offset == 0 else vmax
30+
new_ticks = np.arange(vmin, stop, step)
31+
if invert:
32+
new_ticks = new_ticks[::-1]
33+
return new_ticks + self.offset
3334

34-
def __call__(self):
35-
"""Return the locations of the ticks."""
36-
vmin, vmax = self.axis.get_view_interval()
37-
return self.tick_values(vmin, vmax)
35+
def __call__(self):
36+
"""Return the locations of the ticks."""
37+
vmin, vmax = self.axis.get_view_interval()
38+
return self.tick_values(vmin, vmax)
39+
except ImportError:
40+
pass

0 commit comments

Comments
 (0)