Search code examples
pythonpandaspython-xarrayhvplot

plot data interactive as rgb


I have a bunch of pictures I'd like to plot with hvplot.rgb. The user should be able to switch between the pictures with a widget like a slider.

My problem is, that i don't know how to use the hvplot.rgb()-function for interactive dataframes.

here's what i have so far:

import numpy as np
import pandas as pd
import hvplot
import hvplot.pandas
import hvplot.xarray
import panel as pn

######################## generating a Dataframe with random rgb-arrays

height = 2
length = 2
count = 3      # number of pictures in dataframe


def rgb(height,lenght):
    image = np.random.randint(255, size=(height,length,3))
    x = np.arange(length)
    y = np.arange(height)
    channel = np.arange(len(image[0][0]))
    im_xr = xr.DataArray(image,coords={'y': y,'x': x,'channel': channel},dims=["y", "x", "channel"])
    return im_xr    # returning an xarray in order to use hv.plot.rgb()


list_rgb = []

for i in range(count):
    list_rgb.append(rgb(height,length))
    
df = pd.DataFrame([list_rgb]).T

df.rename(columns = {0:'rgb'}, inplace = True)

dfi = df.interactive()

slider = pn.widgets.IntSlider(name='slider', start=0, end=count-1, step=1)

pipe = dfi[(dfi.index == slider.param.value)]

so at this point i can already plot the generated pictures in the DataFrame with

df.rgb[0].hvplot.rgb(x='x', 
                 y='y', 
                 bands='channel', 
                 data_aspect=1, 
                 flip_yaxis=True, 
                 xaxis=False, 
                 yaxis=None,
                 widget_type='scrubber',
                 widget_location='bottom'
                )

How can I call the .hvplot.rgb() function to plot the "pipe"-interactive dataframe?


Solution

  • so here's how to do it:

    
    import numpy as np
    import pandas as pd
    import hvplot
    import hvplot.pandas
    import hvplot.xarray
    import panel as pn
    
    ######################## generating a Dataframe with random rgb-arrays
    
    height = 2     # height of the generated picture
    width = 2     # width of the generated picture
    count = 3      # number of pictures in dataframe
    
    
    def rgb(height,width):
        image = np.random.randint(255, size=(height,width,3))  
        return image
        
    list_rgb = []
    
    for i in range(count):
        list_rgb.append(rgb(height,width))
    
        
        
    x = np.arange(width)
    y = np.arange(height)
    channel = np.arange(3)
    time = np.arange(count)
          
    xar = xr.DataArray(list_rgb, 
                       dims=("time", "y", "x", "channel")
                       , 
                       coords={"x": x,
                              "y": y,
                              "channel": channel,
                              "time" : time},
                      name="rgb")
        
    xar.hvplot.rgb(x='x', 
                   y='y', 
                   bands='channel', 
                   data_aspect=1, 
                   flip_yaxis=True, 
                   xaxis=False, 
                   yaxis=None
                  )
    

    additionally the scrubber tool is a pretty nice thing to create movies out of pictures with hvplot, like for example timelaps-shows...

    xar.hvplot.rgb(x='x', 
                   y='y', 
                   bands='channel', 
                   data_aspect=1, 
                   flip_yaxis=True, 
                   xaxis=False, 
                   yaxis=None,
                   widget_type='scrubber',
                   widget_location='bottom'
                  )