In [140]:
import pandas as pd
import numpy as np
get_ipython().magic('matplotlib inline')
import matplotlib.pyplot as plt
import os
import folium
import vincent,json
import csv
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import seaborn as sns
import randomcolor
In [171]:
def color_match(colors):
cat_color = {}
for i in colors:
clr = randomcolor.RandomColor().generate()
# print(clr[0])
cat_color[i] = clr[0]
return cat_color
In [166]:
def plot_num1(map, df, name, lat, lon,num1, num2, num3,category,colors):
fg=folium.FeatureGroup(name=num1)
for lat,lon,category,num1,num2,num3,name in zip(df[lat],df[lon],df[category],df[num1],df[num2],df[num3],df[name]):
#make the html click popup
html="""
name: {}<br>
num1: {}<br>
num2: {}<br>
num3: {}<br>
category: {}<br>
"""
html = html.format(name,num1,num2,num3,category)
test = folium.IFrame(html, width=150, height=100)
fg.add_child(folium.CircleMarker(location=[lat,lon],popup=(folium.Popup(test)),radius=num1/10, color=colors.get(category),fill_color=colors.get(category) )) #,color=Color(category),fill_color=Color(cat)))
map.add_child(fg)
In [180]:
def plot_num2(map, df, lat, lon, num2, category, group, colors):
fh=folium.FeatureGroup(name="Numeric2 Data,Categories Chart")
for lat,lon,category,num2 in zip(df[lat],df[lon],df[category],df[num2]):
#make the groupbar chart click popup
popup= folium.Popup(max_width=800)
folium.Vega(group, height=250, width=450).add_to(popup)
fh.add_child(folium.RegularPolygonMarker(location=[lat,lon],popup=popup,radius=num2/10,color=colors.get(category),fill_color=colors.get(category),number_of_sides=3 ))#,color=Color(name),fill_color=Color(name),number_of_sides=3))
map.add_child(fh)
In [177]:
def plot_num3(map, df, lat, lon, category, num3, lines, colors):
fk=folium.FeatureGroup(name="NUM3,Datetime Chart")
for lat,lon,category,num3 in zip(df[lat],df[lon],df[category],df[num3]):
#make the line chart click popup
popup = folium.Popup(max_width=800)
folium.Vega(lines, height=250, width=450).add_to(popup)
fk.add_child(folium.RegularPolygonMarker(location=[lat,lon],popup=popup,radius=num3/10,color=colors.get(category),fill_color=colors.get(category), number_of_sides=4 ))
map.add_child(fk)
In [181]:
def data_input():
filename = input("Please input the dataset name: ")
name = input("Please input the specific column name of 'Name' of your dataset:")
date = input("Please input the specific column name of 'Date' of your dataset:")
lon = input("Please input the specific column name of 'Longitute' of your dataset:")
lat = input("Please input the specific column name of 'Latitue' of your dataset:")
category = input("Please input the specific column name of 'Category' of your dataset:")
num1 = input("Please input the specific 1st column name of 'Numeric' of your dataset:")
num2 = input("Please input the specific 2nd column name of 'Numeric' of your dataset:")
num3 = input("Please input the specific 3rd column name of 'Numeric' of your dataset:")
# filename = 'TestData.csv'
# name = 'NAME'
# date = 'DATE'
# lon = 'LON'
# lat = 'LAT'
# category = 'CAT'
# num1 = 'QUANT1'
# num2 = 'QUANT2'
# num3 = 'QUANT3'
#load the data
df=pd.read_csv(filename)
df.head()
#make the folium map
map=folium.Map(location=[df[lat].mean(),df[lon].mean()],zoom_start=6,tiles='stamenterrain')
#make the vincent chart for category
df_group=df.groupby(category)
print(len(df_group))
print(df_group.groups.keys())
colors = color_match(df_group.groups.keys())
df_group_sum=df_group[num1,num2,num3].sum()
group=vincent.GroupedBar(df_group_sum,width=350,height=200)
group.legend(title='Numeric data')
group.colors(brew='Pastel1')
group.axis_titles(x='Categories', y='Sum of the NUM1')
#make the vincent line chart for datetime
df_group2=df.groupby(date)
df_group_sum2=df_group2[num1,num2,num3].sum()
lines = vincent.Line(df_group_sum2,width=350,height=200)
lines.scales[0].type = 'ordinal'
lines.axis_titles(x='Datetime', y='Sum of the NUM2')
lines.legend(title='Categories')
#add quant1 on the map
plot_num1(map, df, name, lat, lon,num1, num2, num3, category, colors)
#add quant2 on the map
plot_num2(map, df, lat, lon, num2, category, group, colors)
# #add quant3 on the map
plot_num3(map, df, lat, lon, category, num3, lines, colors)
# #add layer contraller
map.add_child(folium.LayerControl())
# #save the map as html file
map.save(outfile='part3map_3.0.html')
In [3]:
cd Desktop
In [ ]:
# please run this function to start
In [182]:
data_input()