[yt-users] customizing colorbar

Nathan Goldbaum nathan12343 at gmail.com
Sat Jul 8 06:29:28 PDT 2017


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.htm>


More information about the yt-users mailing list