Skip to content

Commit

Permalink
Fix off-by-1 error in subplot indexing
Browse files Browse the repository at this point in the history
- hokay so matplotlib starts counting from 1 instead of 0 in this case (maybe because of matlab imitation?)
  • Loading branch information
eldond committed Apr 26, 2018
1 parent bd3f4f7 commit 160a39f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def short_demo():
axs.plot(x, np.cos(x))
axs.plot(x, np.sin(x))
fig.clear()
axs = fig.add_subplot(1, 1, 0)
axs = fig.add_subplot(1, 1, 1)
axs.plot(x, np.sin(x)+1)
axs.plot(x, np.cos(x)-1)
axs.text(0, 0, 'figure cleared then re-used')
Expand Down
4 changes: 2 additions & 2 deletions pyqtmpl/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ def add_subplot(self, nrows, ncols, index, projection=None, polar=None, **kwargs
raise NotImplementedError('projection keyword in add_subplot is not ready')
if polar is not None and polar is not False:
raise NotImplementedError('polar projection is not ready')
row = int(np.floor(index/ncols))
row = int(np.floor((index-1)/ncols))
if row > (nrows-1):
raise ValueError('index {} would be on row {}, but the last row is {}!'.format(index, row, nrows-1))
col = index % ncols
col = (index-1) % ncols
ax = Axes(**kwargs)
self.layout.addItem(ax, row, col)
if self.axes is None:
Expand Down
2 changes: 1 addition & 1 deletion pyqtmpl/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def pick_share(share, ii, jj, axs_):
subplot_kw = subplot_kw if subplot_kw is not None else {}
for i in range(nrows):
for j in range(ncols):
index = i*ncols + j
index = i*ncols + j + 1
axs[i, j] = fig.add_subplot(
nrows, ncols, index,
sharex=pick_share(sharex, i, j, axs), sharey=pick_share(sharey, i, j, axs),
Expand Down

0 comments on commit 160a39f

Please sign in to comment.