pythonpandashvplot

Hvplot multiple columns filter


I am trying to plot a time-serie with a filter using hvplot. The only problems I have is that I want multiple columns in my filter. Here is the dataframe I have:

date           city     Prod1 Prod2 Prod3 Prod4
01/07/2012   Limoges      24    45    12     7
02/07/2012   Lyon         39    36    31     27
03/07/2012   Paris        57    48    48     32

I am able to do show the following:

 df.hvplot(kind='line', x='date', y= 'Prod1', groupby='city')

However, what I ultimately want is to show all the 3 cities on the time-series and have all the products in a filter so I can see how they all behaviour with respect to each product.


Solution

  • IIUC, using matias's suggestion in the comment, you can use:

    import pandas as pd 
    import numpy as np 
    import hvplot.pandas
    
    dates = pd.date_range(start='1/1/2012', periods=120)
    product_data = np.random.randint(low=10, high=100, size=(120, 3))
    city_names = ['city 1', 'city 2', 'city 3'] * 40
    
    df = pd.DataFrame({"date" : dates, "city" : city_names, "Prod1":product_data[:, 0], "Prod2":product_data[:, 1], "Prod3":product_data[:, 2]})
    df = df.melt(id_vars=['date', 'city'], value_vars=[f'Prod{i}' for i in (1, 2, 3)], var_name='prod')
    
    df.hvplot(x='date', by='city', groupby=['prod'])
    

    This allows to compare different product values for all cities across the dates:

    enter image description here