Advertisement
Guest User

Untitled

a guest
Jul 20th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import pandas as pd
  2. from itertools import product
  3. import random
  4. countries = ['Australia','Mexico','Finland']
  5. goods = ['Dairy','Wheat','Beef']
  6. data = []
  7. for country, goods in product(countries,goods):
  8.     data.append(dict(
  9.         Country = country,
  10.         Goods = goods,
  11.         Value = random.randint(1000,50000),
  12.         Count = random.randint(100,10000)
  13.     ))
  14.    
  15. df = pd.DataFrame(data)
  16. df["CountryValueTotal"] = df.groupby('Country')["Value"].transform('sum')
  17. df["%CountryValueTotal"] = df["Value"] / df["CountryValueTotal"] * 100
  18. df["CountryCountTotal"] = df.groupby('Country')["Count"].transform('sum')
  19. df["%CountryCountTotal"] = df["Count"] / df["CountryCountTotal"] * 100
  20.  
  21. import hvplot.pandas
  22. from holoviews import opts
  23. from holoviews.plotting.links import DataLink
  24.  
  25. # the following one works - i.e. linked taps
  26. g1 = df.hvplot.bar(x='Country', by='Goods', y='%CountryCountTotal', stacked=False)
  27. g2 = df.hvplot.bar(x='Country', by='Goods', y='%CountryValueTotal', stacked=False)
  28. dlink = DataLink(g1, g2)
  29. g3 = g1 + g2
  30. g3.opts(
  31.     opts.Bars(width=300, height=250, framewise=True, xrotation = 90, tools=['tap']))
  32.  
  33. # 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
  34. g1 = df.hvplot.bar(x='Country', by='Goods', y='%CountryCountTotal', stacked=True) # <- stacked = true now
  35. g2 = df.hvplot.bar(x='Country', by='Goods', y='%CountryValueTotal', stacked=True) # <- stacked = true now
  36. dlink = DataLink(g1, g2)
  37. g3 = g1 + g2
  38. g3.opts(
  39.     opts.Bars(width=300, height=250, framewise=True, xrotation = 90, tools=['tap']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement