1

I'm new to Python and trying to use Holoviews to plot the selections from two dropdowns against each other. I have data in a csv file that looks like this:

time    col1    col2    col3    col4
0.01    1.9     .023    138     9.10
0.02    2.3     .024    155     9.75
0.03    3.0     .027    162     10.3

I want to be able to select any two columns and plot them against each other: col1 against col2, col3 against time, etc. I've figured out how to make ONE dropdown and plot any column against one predefined key dimension, but I can't figure out how to make a second dropdown to let me change the kdim.

My code so far is:

df = pd.read_csv("data.csv")
cols = list(df.columns)
hv.HoloMap({column: hv.Curve(df, 'time', column) for column in cols}).opts(framewise = True)
DLH
  • 13
  • 3

1 Answers1

0

You can get 2 dropdowns and select any combination of 2 columns of your dataset by either creating a HoloMap or a DynamicMap.

  • Creating a HoloMap: this is a plot where the number of plot combinations is fixed and are created in advance. Here's a working example:
# import libraries
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh')

# create sample data
df = pd.DataFrame(
    data=np.random.rand(50, 4),
    columns=['col1', 'col2', 'col3', 'col4'],
)
df['time'] = np.arange(0., 0.5, step=0.01)

# loop over your column names twice to create all possible plots in your holomap
hv.HoloMap({
    (col1, col2): hv.Scatter((df[col1], df[col2])) for col1 in df.columns for col2 in df.columns},
    kdims=['x', 'y'],
)


  • Another way is creating a DynamicMap where the number of plot combinations are not created in advance but created on the fly when you change the dropdowns:
# function that creates the dynamic plot when called by your dynamicmap
def plot_scatter(x, y):
    return hv.Scatter((df[x], df[y]))

# create your dynamicmap
dmap = hv.DynamicMap(plot_scatter, kdims=['x', 'y'])

# define the range of values that your dropdowns should have
dmap.redim.values(x=df.columns, y=df.columns)



Resulting plot:

HoloMap two dropdowns

Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
  • i'm using Jupyter Notebooks, are you using an IDE? Then check here: https://stackoverflow.com/questions/57971107/how-do-i-get-my-interactive-holoviews-graph-to-display-in-visual-studio-without/57971108#57971108 – Sander van den Oord Dec 16 '20 at 15:41