Skip to content

Commit b1c7202

Browse files
committed
fix #143 : xticklabels are correct when zooming (need to set both xticks and xticklabels)
1 parent eb4b29b commit b1c7202

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

larray/viewer.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,8 @@ def plot(self):
12111211

12121212
row_min, row_max, col_min, col_max = self._selection_bounds()
12131213
dim_names = self.model().xlabels[0]
1214-
xlabels = self.model().xlabels
1215-
ylabels = self.model().ylabels
1214+
xlabels = self.model().xlabels[1][col_min:col_max]
1215+
ylabels = self.model().ylabels[1:][row_min:row_max]
12161216

12171217
assert data.ndim == 2
12181218

@@ -1224,27 +1224,29 @@ def plot(self):
12241224
if data.shape[1] == 1:
12251225
# plot one column
12261226
xlabel = ','.join(dim_names[:-1])
1227-
xticklabels = ['\n'.join([str(ylabels[j][r]) for j in range(1, len(ylabels))])
1228-
for r in range(row_min, row_max)]
1229-
ax.plot(data[:, 0])
1230-
ax.set_ylabel(xlabels[1][col_min])
1227+
xticklabels = ['\n'.join([str(ylabels[c][r]) for c in range(len(ylabels))])
1228+
for r in range(row_max - row_min)]
1229+
xdata = np.arange(row_max - row_min, dtype=int)
1230+
ax.plot(xdata, data[:, 0])
1231+
ax.set_ylabel(xlabels[0])
12311232
else:
12321233
# plot each row as a line
12331234
xlabel = dim_names[-1]
1234-
xticklabels = [str(xlabels[1][c]) for c in range(col_min, col_max)]
1235+
xticklabels = [str(label) for label in xlabels]
1236+
xdata = np.arange(col_max - col_min, dtype=int)
12351237
for row in range(len(data)):
1236-
label = ','.join([str(ylabels[j][row_min + row])
1237-
for j in range(1, len(ylabels))])
1238-
ax.plot(data[row], label=label)
1238+
label = ','.join([str(label) for label in ylabels[row]])
1239+
ax.plot(xdata, data[row], label=label)
12391240

12401241
# set x axis
12411242
ax.set_xlabel(xlabel)
1242-
ax.set_xlim(0, len(xticklabels) - 1)
1243+
ax.set_xlim((xdata[0], xdata[-1]))
12431244
# we need to do that because matplotlib is smart enough to
12441245
# not show all ticks but a selection. However, that selection
12451246
# may include ticks outside the range of x axis
12461247
xticks = [t for t in ax.get_xticks().astype(int) if t <= len(xticklabels) - 1]
1247-
xticklabels = [xticklabels[j] for j in xticks]
1248+
xticklabels = [xticklabels[t] for t in xticks]
1249+
ax.set_xticks(xticks)
12481250
ax.set_xticklabels(xticklabels)
12491251

12501252
if data.shape[1] != 1:

0 commit comments

Comments
 (0)