Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ENH: adjustable colorbar ticks#9903

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dopplershift merged 2 commits intomatplotlib:masterfromjklymak:enh-colorbar-ticks
Mar 22, 2018

Conversation

jklymak
Copy link
Member

@jklymakjklymak commentedDec 1, 2017
edited
Loading

PR Summary

This PR changes colorbar to draw fromself.vmin toself.vmax instead of from 0 to 1 so thatAutoTickLocator can work. Only modifies ticks if no boundaries are specified (i.e. contours).

See#9246

example:

importmatplotlib.pyplotaspltimportmatplotlib.colorsasmcolorsimportnumpyasnpX=np.random.randn(32,32)fig,ax=plt.subplots(2,2,figsize=(5,5),tight_layout=True)fornn,shrinkinenumerate([1.,0.4]):formm,loginenumerate([False,True]):iflog:norm=mcolors.LogNorm()Y=10**Xelse:norm=NoneY=Xpc=ax[nn,mm].pcolormesh(Y,norm=norm)ax[nn,mm].set_xticks([])ax[nn,mm].set_yticks([])fig.colorbar(pc,ax=ax[nn,mm],shrink=shrink,extend='both')plt.show()fig.savefig('ExampleCBOld.png')

before:

examplecbold

after:

examplecbnew

PR Checklist

  • Test/get to work LogNorm
  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@tacaswelltacaswell added this to thev2.2 milestoneDec 1, 2017
@jklymak
Copy link
MemberAuthor

OK, this failed enough tests that there are a bunch more edge cases I need to consider. Good thing there are tests. I'll probably re-do this from scratch and try to make it less invasive. Ido think this is reasonable for 2.2, though, because I'll keep plugging away at it.

@jklymakjklymakforce-pushed theenh-colorbar-ticks branch 2 times, most recently from9d1146d to5c48c10CompareDecember 2, 2017 06:48
@jklymakjklymak changed the titleWIP: ENH: adjustable colorbar ticksENH: adjustable colorbar ticksDec 2, 2017
@jklymak
Copy link
MemberAuthor

This is redone, and much simpler now. 4 image files had to be changed...

@jklymak
Copy link
MemberAuthor

Ping@efiringLogNorm() now works. I'm too lazy to doSymLogNorm() (well actually I disapprove of that normalization as a valid thing to do to data), but someone could easily figure it out.

The diff looks worse than it is because I moved a bit of code around.


self._set_label()

def _get_ticker_locator_formatter(self):
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Much of this was jut factored out of_ticker() But note the new locators for LogNorm and the fallback...

dstansby
dstansby previously requested changesDec 24, 2017
Copy link
Member

@dstansbydstansby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Some questions/comments, looks like this is good overall though

This locator is just a `.MaxNLocator` except the min and max are
clipped by the norm's min and max (i.e. vmin/vmax from the
image/pcolor/contour object). This is necessary so ticks don't
extrude into the "extend regions".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Shouldn't the docstring line up with the left of the quote marks?

jklymak reacted with thumbs up emoji
extrude into the "extend regions".
"""

def __init__(self, colorbar, *args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why are there args and kwargs here if they're not used? Can this also get a docstring (just for the colorbar parameter is probably fine)

jklymak reacted with thumbs up emoji
extrude into the "extend regions".

"""
def __init__(self, colorbar, *args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same as above comment on__init__

jklymak reacted with thumbs up emoji
X, Y = self._mesh()
C = self._values[:, np.newaxis]
self._config_axes(X, Y)
if self.filled:
self._add_solids(X, Y, C)
# self._set_view_limits()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is this old debug code that needs to be removed?

jklymak reacted with thumbs up emoji
if (isinstance(self.norm, colors.LogNorm)
and self._use_adjustable()):
ax.set_xscale('log')
ax.set_yscale('log')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

and only the yscale be log here?

dstansby reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

They both need to be log to make theaspect kwarg work properly (... or we could do a bunch of math and make the X values make the aspect ratio right.)

dstansby reacted with thumbs up emoji

self._set_label()

def _get_ticker_locator_formatter(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This method could do with a quick comment underneath to explain what it does.

jklymak reacted with thumbs up emoji
else:
b = self._boundaries[self._inside]
locator = ticker.FixedLocator(b, nbins=10)
# locator, formatter = _get_ticker_locator_formatter()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This commented out line can go

jklymak reacted with thumbs up emoji
@dstansbydstansby self-assigned thisDec 24, 2017
@jklymakjklymak requested review fromefiring and removed request forefiringJanuary 10, 2018 03:38
@jklymakjklymak modified the milestones:v2.2,v3.0Jan 10, 2018
@jklymak
Copy link
MemberAuthor

Pushed to 3.0 as this isn't urgent. But I think it'd be easy to put in 2.2 if someone wants to dive in and re-milestone...

@jklymak
Copy link
MemberAuthor

rebased

@jklymak
Copy link
MemberAuthor

@dstansby I think I addressed all your concerns.
@efiring requesting a review from you (though no rush) as this is your bailiwick...

@efiring
Copy link
Member

The before-and-after comparison looks great, but tests seem to be failing now.

@jklymak
Copy link
MemberAuthor

Yeah, its just the streamplot image test at 0.036 tolerance. I'll try to track it down. It passes on my machine and I took the text out so it can't be freetype. Grrrr.

@skotaro
Copy link

skotaro commentedFeb 20, 2018
edited
Loading

I'm not sure that this is the right place to tell this, but I recently wrote a detailed instruction on colorbar in matplotlib which might contribute for improving tutorials about colorbar.
https://qiita.com/skotaro/items/01d66a8c9902a766a2c0
It's only in Japanese for the moment but I think you can get the ideas from figures and sample codes. Main topics are 1. Anatomy of a colorbar, 2. Putting a colorbar at the right place with the right size, and 3. Changing ticker into log scale nicely. It's not comprehensive but I think it still helps users puzzled by colorbar.

After writing it, fortunately or not, I found this PR which seems to change colorbar's behavior significantly (no pseudo minor ticks, no normalization of vmin/vmax to 0/1, for example). Actually these modified behaviors are parts of the main topics in my instruction. Then I'm wondering whether I should translate the introduction as it is into English and make a PR to improve docs for the current behavior of colorbar (about cbar.ticker, especially), or I should wait for this PR to be implemented in, say, next rc version and revise the instruction to catch up the new behaviors. What do you think?

@jklymak
Copy link
MemberAuthor

@skotaro Sounds like we had some similar difficulties with colorbars. I will push in the near future for this PR to get in, but it won’t be part of the distribution until 3.0 (July/Aug).

You may want to checkout constrained layout in 2.2Rc1 for easier colorbar placement. That doesn’t preclude a tutorial that explains how to fix things manually, but I’d suggest such a tutorial should at least reference the automated methods.

@efiring
Copy link
Member

The problem with the doc build is that after the merge, the sphinx conf.pyexclude_patterns list is out of sync with the location where you are putting your users/whats_new file.
It looks like this is a bug in master: there are directories users/prev_whats_new and users/next_whats_new, butexclude_patterns = ['api/api_changes/*', 'users/whats_new/*'].

@jklymakjklymakforce-pushed theenh-colorbar-ticks branch 5 times, most recently from651d609 to2c33640CompareMarch 18, 2018 04:58
@jklymak
Copy link
MemberAuthor

Somehow the doc build didn't like the name I had for the whats-new file. Works fine now. 🤷‍♂️

ax.xaxis.get_major_formatter().set_offset_string(offset_string)
_log.debug('Using fixed locator on colorbar')
ticks, ticklabels, offset_string = self._ticker(locator, formatter)
if self.orientation == 'vertical':
Copy link
Member

@efiringefiringMar 18, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You can use the long_axis, short_axis here, too. Initialize them prior to the_use_auto_colorbar_locator check.
Instead ofax.set_yticklabels you can use the equivalentshort_axis.set_ticklabels, etc.

jklymak reacted with thumbs up emoji

Colorbar ticks now adjust for the size of the colorbar if the
colorbar is made from a mappable that is not a contour or
doesn't have a BoundaryNorm, or boundaries are not specified.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

extra space


def __init__(self, colorbar):
"""
_ColorbarAutoLocator(colorbar)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is there a particular need to include the signature?

if type(self.norm) == colors.LogNorm:
long_axis.set_minor_locator(_ColorbarLogLocator(self,
base=10., subs='auto'))
long_axis.set_minor_formatter(ticker.NullFormatter())
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Need to check that some ticks are labelled...

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Fixed. LogLocator works fine here.

@afvincent
Copy link
Contributor

@jklymak Is there an obvious reason why the ticks of the colorbar in the bottom right panel are not symmetrical (in log space) around 1 (e.g. [1e3, 1e0, 1e-3], instead of [1e2, 1e-1]), while the old one were “quite” symmetrical?

@jklymak
Copy link
MemberAuthor

@afvincent because the LogLocator only returned two tick marks as being able to fit and it chose those? Its just using the default LogLocator - i.e. if you had a normal yaxis that size, those are the ticks you'd get.

@anntzer
Copy link
Contributor

@skotaro Are you still interested in including your (extensive!) colorbar tutorial into the mpl docs? Even if it needs to be updated following this PR and/or to take constrained_layout into account, feel free to open an early issue/PR to track the work.

@jklymak
Copy link
MemberAuthor

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@anntzeranntzeranntzer left review comments

@efiringefiringefiring approved these changes

@dopplershiftdopplershiftdopplershift approved these changes

@dstansbydstansbydstansby left review comments

Assignees

@dstansbydstansby

Labels
None yet
Projects
None yet
Milestone
v3.0.0
Development

Successfully merging this pull request may close these issues.

9 participants
@jklymak@efiring@skotaro@afvincent@anntzer@dopplershift@dstansby@tacaswell@QuLogic

[8]ページ先頭

©2009-2025 Movatter.jp