Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- from itertools import product
- import random
- countries = ['Australia','Mexico','Finland']
- goods = ['Dairy','Wheat','Beef']
- data = []
- for country, goods in product(countries,goods):
- data.append(dict(
- Country = country,
- Goods = goods,
- Value = random.randint(1000,50000),
- Count = random.randint(100,10000)
- ))
- df = pd.DataFrame(data)
- df["CountryValueTotal"] = df.groupby('Country')["Value"].transform('sum')
- df["%CountryValueTotal"] = df["Value"] / df["CountryValueTotal"] * 100
- df["CountryCountTotal"] = df.groupby('Country')["Count"].transform('sum')
- df["%CountryCountTotal"] = df["Count"] / df["CountryCountTotal"] * 100
- import hvplot.pandas
- from holoviews import opts
- from holoviews.plotting.links import DataLink
- # the following one works - i.e. linked taps
- g1 = df.hvplot.bar(x='Country', by='Goods', y='%CountryCountTotal', stacked=False)
- g2 = df.hvplot.bar(x='Country', by='Goods', y='%CountryValueTotal', stacked=False)
- dlink = DataLink(g1, g2)
- g3 = g1 + g2
- g3.opts(
- opts.Bars(width=300, height=250, framewise=True, xrotation = 90, tools=['tap']))
- # the following one gives an error : ValueError: DataLink can only be applied if overlapping dimension values are equal, bottom column on source does not match target
- g1 = df.hvplot.bar(x='Country', by='Goods', y='%CountryCountTotal', stacked=True) # <- stacked = true now
- g2 = df.hvplot.bar(x='Country', by='Goods', y='%CountryValueTotal', stacked=True) # <- stacked = true now
- dlink = DataLink(g1, g2)
- g3 = g1 + g2
- g3.opts(
- opts.Bars(width=300, height=250, framewise=True, xrotation = 90, tools=['tap']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement