Skip to content

If len(groupby) > 1 and overlay > 1, cycle multiple sequential cmaps? #380

@ahuang11

Description

@ahuang11

The first image is very noisy, the second image is much more understandable (although the legend labels are missing the names)
image

import pandas as pd
import xarray as xr
import hvplot.pandas

ds = xr.tutorial.open_dataset('air_temperature')

ds_sub = ds.sel(lat=[15, 40, 75], lon=272.5, time=ds['time.day'] == 15)
ds_sub = ds_sub.groupby(ds_sub['time'].dt.strftime('%m%d%H')).mean('time')
ds_sub.coords['label'] = ('lat', ['tropics', 'mid-latitude', 'arctic-circle'])
ds_sub['month'] = ds_sub['strftime'].str[:2]
ds_sub['hour'] = ds_sub['strftime'].str[-2:]


df = ds_sub.to_dataframe().reset_index()
df = df.drop(columns=['lat', 'lon', 'strftime'])

df.hvplot('month', 'air', groupby=['label', 'hour']).overlay(['label', 'hour']).opts(legend_position='right')

from pylab import *
def get_cmap(cmap):
    cmap = cm.get_cmap(cmap, 4)    # PiYG
    rgbs = [matplotlib.colors.rgb2hex(cmap(i)[:3]) for i in range(cmap.N)] # will return rgba, we take only first 3 so we get rgb
    return rgbs

ov = hv.NdOverlay()
for label, cmap in zip(df['label'].unique(), ['Reds', 'Greens', 'Blues'][::-1]):
    ov[label] =  df.loc[df['label'] == label].hvplot('month', 'air', groupby='hour', label=label, color=hv.Cycle(get_cmap(cmap)), dynamic=False).overlay()

ov

Metadata

Metadata

Assignees

No one assigned

    Labels

    [OLD] type: discussionOld labels for Discussion-type issues. Convert new Issues of this type to GitHub Discussions.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions