-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Make colorbar compatible accepting a list of axes created via gridspec #8755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1b0ac37
Clarify how a FancyArrowPatch behaves
dstansby 070a578
Clarify in display units
dstansby 089818b
Allow divmod to be overridden by numpy
dstansby b87778e
Various cleanups to backends code.
anntzer 40fa293
Update FreeType version in test_tightlayout4.
QuLogic c0ecc7d
Add a unique number to any renderer hash keys.
QuLogic df672df
Update tests that are marked flaky.
QuLogic 9bf168f
Use itertools.count for renderer unique ID.
QuLogic 454e9d6
Simplify cla sharex/sharey code; alternative to #8710
efiring edfdc53
MNT: colorbar accept numpy array input (#8739)
jklymak 2284e98
Changed normalization in _spectral_helper() to obtain conistent scali…
DietBru 234089d
Added note to api_changes
DietBru a62d3e3
Fix contour colour level determination
dstansby 03c00d7
Correct contour level test
dstansby 65f3906
sort input files
bmwiedemann 62aa006
First pass; seems to work. More testing needed
jklymak fb3f419
First pass; seems to work. More testing needed
jklymak 89857c7
First pass; seems to work. More testing needed
jklymak c68a411
First pass; seems to work. More testing needed
jklymak 031d356
First change
jklymak 1c5a14e
Aded comments and cleaned up
jklymak 12737f5
Aded comments and cleaned up
jklymak 600253e
Colormap that handles gridspec
jklymak fd388aa
Colormap that handles gridspec
jklymak 262fa33
Colormap that handles gridspec
jklymak 37998b4
Fixed subplotspec geometry having None
jklymak d9b49b5
Fixed subplotspec geometry having None, PEP8 errors
jklymak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
First change
- Loading branch information
commit 031d3565f29d81c54e9ab7e174e33fce4dacf093
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ | |
import matplotlib.patches as mpatches | ||
import matplotlib.path as mpath | ||
import matplotlib.ticker as ticker | ||
import matplotlib.transforms as mtrans | ||
import matplotlib.transforms as mtransforms | ||
|
||
from matplotlib import docstring | ||
|
||
|
@@ -317,7 +317,7 @@ def __init__(self, ax, cmap=None, | |
linthresh=self.norm.linthresh) | ||
else: | ||
self.formatter = ticker.ScalarFormatter() | ||
elif cbook.is_string_like(format): | ||
elif isinstance(format, six.string_types): | ||
self.formatter = ticker.FormatStrFormatter(format) | ||
else: | ||
self.formatter = format # Assume it is a Formatter | ||
|
@@ -344,6 +344,7 @@ def draw_all(self): | |
Calculate any free parameters based on the current cmap and norm, | ||
and do all the drawing. | ||
''' | ||
|
||
self._process_values() | ||
self._find_range() | ||
X, Y = self._mesh() | ||
|
@@ -399,6 +400,10 @@ def set_ticks(self, ticks, update_ticks=True): | |
self.update_ticks() | ||
self.stale = True | ||
|
||
def get_ticks(self, minor=False): | ||
"""Return the x ticks as a list of locations""" | ||
return self._tick_data_values | ||
|
||
def set_ticklabels(self, ticklabels, update_ticks=True): | ||
""" | ||
set tick labels. Tick labels are updated immediately unless | ||
|
@@ -611,6 +616,7 @@ def _ticker(self): | |
else: | ||
eps = (intv[1] - intv[0]) * 1e-10 | ||
b = b[(b <= intv[1] + eps) & (b >= intv[0] - eps)] | ||
self._tick_data_values = b | ||
ticks = self._locate(b) | ||
formatter.set_locs(b) | ||
ticklabels = [formatter(t, i) for i, t in enumerate(b)] | ||
|
@@ -681,9 +687,10 @@ def _process_values(self, b=None): | |
self.norm.vmin = 0 | ||
self.norm.vmax = 1 | ||
|
||
self.norm.vmin, self.norm.vmax = mtrans.nonsingular(self.norm.vmin, | ||
self.norm.vmax, | ||
expander=0.1) | ||
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular( | ||
self.norm.vmin, | ||
self.norm.vmax, | ||
expander=0.1) | ||
|
||
b = self.norm.inverse(self._uniform_y(self.cmap.N + 1)) | ||
|
||
|
@@ -1110,17 +1117,19 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, | |
parent_anchor = kw.pop('panchor', loc_settings['panchor']) | ||
pad = kw.pop('pad', loc_settings['pad']) | ||
|
||
# turn parents into a list if it is not already | ||
parents = np.atleast_1d(parents).ravel().tolist() | ||
# turn parents into a list if it is not already. We do this w/ np | ||
# because `plt.subplots` can return an ndarray and is natural to | ||
# pass to `colorbar`. | ||
parents = np.atleast_1d(parents).ravel() | ||
|
||
fig = parents[0].get_figure() | ||
if not all(fig is ax.get_figure() for ax in parents): | ||
raise ValueError('Unable to create a colorbar axes as not all ' | ||
'parents share the same figure.') | ||
|
||
# take a bounding box around all of the given axes | ||
parents_bbox = mtrans.Bbox.union([ax.get_position(original=True).frozen() | ||
for ax in parents]) | ||
parents_bbox = mtransforms.Bbox.union( | ||
[ax.get_position(original=True).frozen() for ax in parents]) | ||
|
||
pb = parents_bbox | ||
if location in ('left', 'right'): | ||
|
@@ -1141,12 +1150,12 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, | |
|
||
# define a transform which takes us from old axes coordinates to | ||
# new axes coordinates | ||
shrinking_trans = mtrans.BboxTransform(parents_bbox, pb1) | ||
shrinking_trans = mtransforms.BboxTransform(parents_bbox, pb1) | ||
|
||
# transform each of the axes in parents using the new transform | ||
for ax in parents: | ||
new_posn = shrinking_trans.transform(ax.get_position()) | ||
new_posn = mtrans.Bbox(new_posn) | ||
new_posn = mtransforms.Bbox(new_posn) | ||
ax.set_position(new_posn) | ||
if parent_anchor is not False: | ||
ax.set_anchor(parent_anchor) | ||
|
@@ -1155,9 +1164,18 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, | |
cax.set_aspect(aspect, anchor=anchor, adjustable='box') | ||
return cax, kw | ||
|
||
# helper functions for row,col to index. | ||
def index2rowcolunm(index,ncols): | ||
col = index%ncols + 1 | ||
row = int(np.floor(index/ncols)+1) | ||
return row,col | ||
def rowcolunm2index(row,col,ncols): | ||
index = col-1+(row-1)*ncols | ||
return index | ||
|
||
|
||
@docstring.Substitution(make_axes_kw_doc) | ||
def make_axes_gridspec(parent, **kw): | ||
def make_axes_gridspec(parents, **kw): | ||
''' | ||
Resize and reposition a parent axes, and return a child axes | ||
suitable for a colorbar. This function is similar to | ||
|
@@ -1204,13 +1222,52 @@ def make_axes_gridspec(parent, **kw): | |
pad_s = (1. - shrink) * 0.5 | ||
wh_ratios = [pad_s, shrink, pad_s] | ||
|
||
# make parents a 1-d ndarray if its not already... | ||
parents = np.atleast_1d(parents).ravel() | ||
# get the appropriate subplot spec. Loop through the parents. | ||
gsp0 = parents[0].get_subplotspec().get_gridspec() | ||
minind = 10000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to use |
||
maxind = -10000 | ||
for parent in parents: | ||
gsp = parent.get_subplotspec().get_gridspec() | ||
if gsp == gsp0: | ||
ss = parent.get_subplotspec().get_geometry() | ||
print(ss) | ||
if ss[2]<minind: | ||
minind = ss[2] | ||
if ss[3]>maxind: | ||
maxind = ss[3] | ||
else: | ||
pass | ||
# get from maxind and minind to nrows and ncols.. | ||
ncols0 = gsp0.get_geometry()[1] | ||
print(ncols0) | ||
print(maxind) | ||
|
||
minrow,mincol=index2rowcolunm(minind,ncols0) | ||
maxrow,maxcol=index2rowcolunm(maxind,ncols0) | ||
print('mnind %d maxind %d'%(minind,maxind)) | ||
print('mncol %d maxcol %d'%(mincol,maxcol)) | ||
print('mnrow %d maxrow %d'%(minrow,maxrow)) | ||
nrows = maxrow-minrow+1 | ||
ncols = maxcol-mincol+1 | ||
|
||
print('nrows %d ncols %d'%(nrows,ncols)) | ||
print(minind) | ||
print(maxind) | ||
subspec = gridspec.SubplotSpec(gsp0,minind,maxind) | ||
print('GSP0') | ||
print(gsp0.get_geometry()) | ||
print(subspec) | ||
print(subspec.get_geometry()) | ||
|
||
gs_from_subplotspec = gridspec.GridSpecFromSubplotSpec | ||
if orientation == 'vertical': | ||
pad = kw.pop('pad', 0.05) | ||
wh_space = 2 * pad / (1 - pad) | ||
|
||
gs = gs_from_subplotspec(1, 2, | ||
subplot_spec=parent.get_subplotspec(), | ||
subplot_spec=subspec, | ||
wspace=wh_space, | ||
width_ratios=[x1 - pad, fraction] | ||
) | ||
|
@@ -1220,6 +1277,8 @@ def make_axes_gridspec(parent, **kw): | |
hspace=0., | ||
height_ratios=wh_ratios, | ||
) | ||
print(gs) | ||
print(gs2) | ||
|
||
anchor = (0.0, 0.5) | ||
panchor = (1.0, 0.5) | ||
|
@@ -1228,7 +1287,7 @@ def make_axes_gridspec(parent, **kw): | |
wh_space = 2 * pad / (1 - pad) | ||
|
||
gs = gs_from_subplotspec(2, 1, | ||
subplot_spec=parent.get_subplotspec(), | ||
subplot_spec=subspec, | ||
hspace=wh_space, | ||
height_ratios=[x1 - pad, fraction] | ||
) | ||
|
@@ -1243,12 +1302,32 @@ def make_axes_gridspec(parent, **kw): | |
anchor = (0.5, 1.0) | ||
panchor = (0.5, 0.0) | ||
|
||
parent.set_subplotspec(gs[0]) | ||
parent.update_params() | ||
parent.set_position(parent.figbox) | ||
parent.set_anchor(panchor) | ||
# we need to repackage the parent axes into gs. | ||
# We need to know where they are in the new gs. | ||
# the new gs has nrows by ncols. | ||
gsnew = gs_from_subplotspec(nrows, ncols, subplot_spec=gs[0]) | ||
|
||
for parent in parents: | ||
geo=parent.get_subplotspec().get_geometry() | ||
ncol0 = geo[1] | ||
print("Parent Geo:") | ||
print(geo) | ||
oldrow,oldcol = index2rowcolunm(geo[2],ncol0) | ||
newrow = oldrow-minrow+1 | ||
newcol = oldcol-mincol+1 | ||
newminind = rowcolunm2index(newrow,newcol,ncols) | ||
oldrow,oldcol = index2rowcolunm(geo[3],ncol0) | ||
newrow = oldrow-minrow+1 | ||
newcol = oldcol-mincol+1 | ||
newmaxind = rowcolunm2index(newrow,newcol,ncols) | ||
print('newminind %d newmaxind %d'%(newminind,newmaxind)) | ||
|
||
parent.set_subplotspec(gridspec.SubplotSpec(gsnew,newminind,newmaxind)) | ||
parent.update_params() | ||
parent.set_position(parent.figbox) | ||
parent.set_anchor(panchor) | ||
|
||
fig = parent.get_figure() | ||
fig = parents[0].get_figure() | ||
cax = fig.add_subplot(gs2[1]) | ||
cax.set_aspect(aspect, anchor=anchor, adjustable='box') | ||
return cax, kw | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the argument name is an API change (as
make_akes_gridspec(parent=obj)
will be broken).