[yt-users] customizing colorbar

sacielo sacielo at gmail.com
Mon Jul 10 00:01:31 PDT 2017


Many thanks Nathan

for the brilliant reply and solution. I could edit it easily with
plot._font_properties.
​Only, when the colorbar is horizontal one should use new_ax.xaxis instead
of new_ax.yaxis
(took me a bit to spot it), but it is true that like this one can have
complete control over
the colorbar.
​
Best,

On Sat, Jul 8, 2017 at 8:34 PM, <yt-users-request at lists.spacepope.org>
wrote:

> Send yt-users mailing list submissions to
>         yt-users at lists.spacepope.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
> or, via email, send a message with subject or body 'help' to
>         yt-users-request at lists.spacepope.org
>
> You can reach the person managing the list at
>         yt-users-owner at lists.spacepope.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of yt-users digest..."
>
>
> Today's Topics:
>
>    1. customizing colorbar (sacielo)
>    2. Re: customizing colorbar (Nathan Goldbaum)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sat, 8 Jul 2017 10:48:20 +0200
> From: sacielo <sacielo at gmail.com>
> To: yt-users at lists.spacepope.org
> Subject: [yt-users] customizing colorbar
> Message-ID:
>         <CALAgqdL+fOwJoB+8---Yh3YYHbLwimDS+DrbO0GpbV3iXCeSPQ at mail.gmail.
> com>
> Content-Type: text/plain; charset="utf-8"
>
> Hello,
>
> I was trying to change the looks of the colorbar of slice plots; first I
> found out I could create a new colorbar in a new position. I obtained
> something I like very much like this (for some dataset ds):
>
> ?myvar = 'density'
> sl = yt.SlicePlot(ds, 'x', myvar, center='c', width=(3,'kpc'))
> sl.hide_colorbar()
> sl.hide_axes()
> plot   = sl.plots[myvar]
> fig    = plot.figure
> new_ax = fig.add_axes((0.025,0.94,0.75,0.025))
> ??
> cb     = plot.figure.colorbar(plot.image, new_ax, extend='both',
> orientation='horizontal', format='%.0E'?)?
> sl._setup_plots()
>
> ?The only problem now is I cannot change the font properties of this new
> colorbar. Even trying?, e.g.:
>
> sl.set_font({'family': 'sans-serif', 'style': 'normal', 'weight': 'normal',
> 'size': 36})
>
> ?will change all fonts, including the default colorbar, all text and
> annotations, but not this new colorbar.
> I think plot.figure.colorbar does not accept any argument to edit the font.
>
> Do you know how I could change this? Or maybe have I gone down the wrong
> path?
> ?I wouldn't like to do all manually with matplotlib, as I plan to add many
> more yt annotations to this plot, which all work very well.
>
> ?Thank you!?
>
> ?Best,?
>
> --
> Salvatore
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.
> org/attachments/20170708/ad1d41c4/attachment.html>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 8 Jul 2017 08:29:28 -0500
> From: Nathan Goldbaum <nathan12343 at gmail.com>
> To: Discussion of the yt analysis package
>         <yt-users at lists.spacepope.org>
> Subject: Re: [yt-users] customizing colorbar
> Message-ID:
>         <CAJXewOns=aZfkkYoxaP+2HuBLahkrsU4zyMzp__yoiKSW-
> FcoA at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> On Sat, Jul 8, 2017 at 3:48 AM, sacielo <sacielo at gmail.com> wrote:
>
> > Hello,
> >
> > I was trying to change the looks of the colorbar of slice plots; first I
> > found out I could create a new colorbar in a new position. I obtained
> > something I like very much like this (for some dataset ds):
> >
> > ?myvar = 'density'
> > sl = yt.SlicePlot(ds, 'x', myvar, center='c', width=(3,'kpc'))
> > sl.hide_colorbar()
> > sl.hide_axes()
> > plot   = sl.plots[myvar]
> > fig    = plot.figure
> >
>
> Rather than creating a new colorbar, you can get access to the original
> colorbar via plot.cb and the original colorbar axes via plot.cax.
>
> However, I don't think there's an easy way to make a colorbar horizontal
> after its already been created.
>
> Right now yt's plots don't have a mode to create a horizontal colorbar.
> They aren't really designed for total customization, they're optimized for
> quick plotting and as such make many assumptions about how the plots look.
> So you're running into a mismatch between assumptions the plotting system
> needs to make about how the plots should look to avoid making people
> manually create plots every time, and the inflexibility that implies if you
> really *do* want to manually plot something.
>
> If you want fully manual control over the plot looks and don't want to use
> the axes and colorbar yt is creating, then you may want to manually
> construct the plots using an image buffer. Here are some examples in the
> docs on how to do that:
>
> http://yt-project.org/docs/dev/visualizing/manual_plotting.html
> http://yt-project.org/docs/dev/cookbook/complex_plots.
> html?highlight=manual#multi-plot-slice-and-projections
>
> Of course that doesn't let you use the yt plot annotations, so I don't
> think that's what you're looking for either.
>
>
> > new_ax = fig.add_axes((0.025,0.94,0.75,0.025))
> > ??
> > cb     = plot.figure.colorbar(plot.image, new_ax, extend='both',
> > orientation='horizontal', format='%.0E'?)?
> > sl._setup_plots()
> >
> > ?The only problem now is I cannot change the font properties of this new
> > colorbar. Even trying?, e.g.:
> >
> > sl.set_font({'family': 'sans-serif', 'style': 'normal', 'weight':
> > 'normal', 'size': 36})
> >
>
> Another way to do this using your current approach would be to customize
> the colorbar you're creating using the plot's new FontProperties instance.
> After calling this function you can get access to the FontProperties
> instance used via plot._font_properties.
>
> Next, you'd need to apply the new FontProperties to the tick marks and
> labels on the colorbar you've created:
>
>     fp = plot._font_properties
>     labels = new_ax.yaxis.get_ticklabels()
>     labels += [new_ax.yaxis.label, new_ax.yaxis.get_offset_text()]
>     for label in labels:
>         label.set_fontproperties(fp)
>
> Here's a worked out example:
>
> https://gist.github.com/anonymous/9a80cd6f0148d1df362a2c9cfb6d10da
>
>
> >
> > ?will change all fonts, including the default colorbar, all text and
> > annotations, but not this new colorbar.
> > I think plot.figure.colorbar does not accept any argument to edit the
> font.
> >
> > Do you know how I could change this? Or maybe have I gone down the wrong
> > path?
> > ?I wouldn't like to do all manually with matplotlib, as I plan to add
> many
> > more yt annotations to this plot, which all work very well.
> >
> > ?Thank you!?
> >
> > ?Best,?
> >
> > --
> > Salvatore
> >
> > _______________________________________________
> > yt-users mailing list
> > yt-users at lists.spacepope.org
> > http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
> >
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.
> org/attachments/20170708/39ec6e27/attachment-0001.htm>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>
>
> ------------------------------
>
> End of yt-users Digest, Vol 113, Issue 11
> *****************************************
>



-- 
Salvatore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20170710/bdb4c7d6/attachment.html>


More information about the yt-users mailing list