Skip to content

Instantly share code, notes, and snippets.

@sergiolucero
Last active September 30, 2020 20:44
Show Gist options
  • Save sergiolucero/59ed64aa4caf5d897b7bbed24540fd64 to your computer and use it in GitHub Desktop.
Save sergiolucero/59ed64aa4caf5d897b7bbed24540fd64 to your computer and use it in GitHub Desktop.
first use of datachile API
import pandas as pd
import matplotlib.pyplot as plt
import squarify # pip install squarify (algorithm for treemap)
from datachile import ChileCube
client = ChileCube()
query = client.get("exports",
{"drilldowns": [
["Date", "Year"],
["Destination Country", "Country", "Country"]
],
"measures": ["FOB US"],
"cuts": [{"drilldown": ["Date", "Year"],"values": [2016]}],
"parents": True
})
###############################
df = pd.DataFrame(query['data'])
df = df[df['FOB US']>df['FOB US'].quantile(.85)] # remove smaller countries
df = df.sort_values('FOB US', ascending=False)
df['perc'] = df['FOB US']/df['FOB US'].sum()
df['cp'] = ['%s (%.2f%%)' %(c,100*p) for c,p in zip(df['Country'],df['perc'])]
squarify.plot(sizes=df['FOB US'], label=df['cp'], alpha=.8 )
plt.axis('off')
plt.title('Principales Exportaciones desde Chile año 2016')
plt.show()
@sergiolucero
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment