intake / xrviz

Interactive visualisation interface for Xarrays
https://xrviz.readthedocs.io
BSD 3-Clause "New" or "Revised" License
105 stars 21 forks source link

Add custom colormap and colorbar ticklabels #72

Open gewitterblitz opened 3 years ago

gewitterblitz commented 3 years ago

Hi,

I have two questions:

Is it possible to use custom colormaps in xrviz without changing too much of base code?

Here's my implementation of an animation using an xarray dataset:

import xarray as xr
from xrviz.dashboard import Dashboard

data = xr.open_mfdataset('./*.nc',parallel=True)

hid_colors = ['#D7CCC8', 'FFCCBC', 'FF8A65', 'BF360C', 'F44336',
              'FFEE58', '94D82D', '51CF66', '2B8A3E', '66D9E8',
              '228BE6', '364FC7', 'EEBEFA', 'F783AC', 'F8F9FA',
              'F8F9FA','F8F9FA','F8F9FA']

cmaphid_colors = [hex_to_rgb(color) for color in hid_colors]
cmaphid = [[t/255 for t in x] for x in cmaphid_colors]

cmapncarpid = colors.ListedColormap(cmaphid)
plt.register_cmap(name='ncarpid',cmap=cmapncarpid)

initial_params = {# Select Variable
                  'Variables': 'PID',

                  # Set Coords
                  'Set Coords': ['lat0', 'lon0'],

                  # Axes
                  'x': 'lon0',
                  'y': 'lat0',
                  'z': 'animate',

                  # Style
                  'height': 300,
                  'width': 650,
                  'colorbar': True,
                  'cmap': 'ncarpid'     
}

However, when I use the custom cmap ncarpid, it is not recognized by xrviz.

Also, is there a way to replace default colorbar ticklabels with strings of our choice?

Here's an example of the dashboard generated from code snippet above:

Screen Shot 2020-11-10 at 1 43 43 PM
martindurant commented 3 years ago

If hvPlot can accept it as a parameter, there is no reason xrviz shouldn't allow custom colour maps or labels - but this is not implemented yet. I don't know really how we would be able to squeeze such specific options into the interface; perhaps we could allow for some arguments that are passed through and override whatever is selected in the interface.

gewitterblitz commented 3 years ago

hvplot indeed accepts colormap. Here's an example:

pid_plot = data.PID.isel(time=17,z0=15).hvplot(colorbar=True,height=600,width=800).opts(colorbar_opts={
                                                                                            'background_fill_alpha':0.1,
                                                                                            'bar_line_width':2,
                                                                                            'label_standoff':8,
                                                                                            'major_label_text_font_size':2,
                                                                                            'major_label_overrides':clabs,
                                                                                            'ticker': ticker
                                                                                            },
                                                                                            clim = (1,18),
                                                                                            cmap=cmapncarpid,
                                                                                            )
pid_plot

Output:

Screen Shot 2020-11-10 at 2 24 40 PM

I think I am stuck in between xrviz and a pure hvplot based animation approach because even though I could generate a static hvplot plot, the animation based approach provided by @philippjfr here does not allow me to change ticklabels and varying two dimensions (42 timesteps + 40 z values) is apparently too big to animate. Here is the code and warning I get when I do that:

Code:

data['PID'] = data['PID'].where(data['PID'] >=1)

time = pn.widgets.Player(name='time', start=0, end=42, loop_policy='loop', interval=100)
alt = pn.widgets.Select(name='alt', value=15, options = list(np.arange(0,40,1)))

pid_plot = data.PID.interactive.isel(time=time).isel(z0=alt).hvplot(colorbar=True,height=600,width=800,cmap=cmapncarpid,
                                                                  )
pid_plot

Warning message:

WARNING:param.Parameterized: Use method 'warning' via param namespace 
WARNING:param.Parameterized:Use method 'warning' via param namespace 
WARNING:param.main: The cross product of different application states is very large to explore (N=1680), consider reducing the number of options on the widgets or increase the max_states specified in the function to remove this warning

My notebook crashes after trying too hard to render the animation this way.