In [143]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random

from plotly.offline import download_plotlyjs, init_notebook_mode, plot
from plotly.graph_objs import *

%matplotlib inline
init_notebook_mode()



In [144]:
file = "./Playdata.xlsx"
#df = pd.read_excel(file, sheetname = 'Sheet1', header = 0, index_col = 0, convert_float = True)
#dv = pd.read_excel(file, sheetname = 'Sheet3', header = 0, index_col = 0, convert_float = True)
df = pd.read_excel(file, sheetname = 'Sheet1', header = 0, convert_float = True)
dv = pd.read_excel(file, sheetname = 'Sheet3', header = 0, index_col = 0, convert_float = True)

df['protein_percent'] = df['protein']/dv['DV']['Protein'] *100
df['totalcal'] = df['calperserv']/dv['DV']['Caloriesm'] * 100
df['foodtext'] = df.name 


foodgrp_key= list(set(df['foodgroup']))
grp_val = list(map(int,range(len(foodgrp_key))))
grpdict = dict(zip(foodgrp_key,grp_val))

In [140]:
df.head().totalcal
#for i in df["foodgroup"]:
  # df["grpnumbers"] = grphash[df["foodgroup"][i]]
    
#grpdict
#grpdict["Burger"]
#grpdict


Out[140]:
0     0.60
1     4.50
2     3.25
3    10.35
4     8.55
Name: totalcal, dtype: float64

In [141]:
converter = {}
i=1
for item in df["foodgroup"]:
    if item not in converter:
        #converter[item] =i
        converter[item]= "rgb("+ str(random.randint(0,255)) + "," + str(random.randint(0,255)) + "," + str(random.randint(0,255)) + ")"
        i += 1
df['foodgrpnum'] = [converter[i] for i in df["foodgroup"]]
#print("rgb("+ str(random.randint(0,255)) + "," + str(random.randint(0,255)) + "," + str(random.randint(0,255)) + ")")
#df.foodgrpnum

In [ ]:


In [146]:
set(df.foodgr


Out[146]:
{'Bread',
 'Burger',
 'Candy',
 'Dessert',
 'Dressing',
 'Fruit',
 'Meat',
 'Pasta',
 'Pizza',
 'Spread',
 'Vegetable',
 'fruit'}

In [142]:
plot({
    'data': [
        Scatter(x=df['calperserv'],
                y=df['score'],
                text=df.foodtext ,
                #marker=Marker(size=df['calperserv'], sizemode='area'),   #, sizeref=131868,),
                marker = dict(size= df.score*3 ,
                    line= dict(width=1),
                    color= df.foodgrpnum ,
                    opacity= 0.8
                   ),
                #name= y[i],
                #mode='markers'
                mode="markers"
               ) 
    ],
    'layout': Layout(title='Nutrition and Calorie Count',xaxis=XAxis(title='Calories per Serving'), yaxis=YAxis(title='Nutrition 1-10'))
}, show_link=False)


Out[142]:
'file://C:\\Users\\Art\\Documents\\Python Workspace\\foodfilter\\temp-plot.html'

In [90]:
df.head().foodtext


Out[90]:
0      Collard Greens
1      Sweet Potatoes
2             Spinach
3            Broccoli
4    Butter Croissant
Name: foodtext, dtype: object

In [4]:
#df['foodgrpnum']
#df['foodgrpnum'] = df.apply(lambda row: [v for k, v in new_data.items() if row['foodgroup'] == k][0], axis = 1)

In [ ]:


In [32]:
type(df[['foodgrpnum']])


Out[32]:
pandas.core.frame.DataFrame

In [16]:
import colorsys

def get_N_HexCol(N=5):

    HSV_tuples = [(x*1.0/N, 0.5, 0.5) for x in range(N)]
    hex_out = []
    for rgb in HSV_tuples:
        rgb = map(lambda x: int(x*255),colorsys.hsv_to_rgb(*rgb))
        hex_out.append("".join(map(lambda x: chr(x).encode('hex'),rgb)))
    return hex_out

In [17]:
get_N_HexCol(5)


---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
<ipython-input-17-54ca925167f2> in <module>()
----> 1 get_N_HexCol(5)

<ipython-input-16-16a7c1a553cd> in get_N_HexCol(N)
      7     for rgb in HSV_tuples:
      8         rgb = map(lambda x: int(x*255),colorsys.hsv_to_rgb(*rgb))
----> 9         hex_out.append("".join(map(lambda x: chr(x).encode('hex'),rgb)))
     10     return hex_out

<ipython-input-16-16a7c1a553cd> in <lambda>(x)
      7     for rgb in HSV_tuples:
      8         rgb = map(lambda x: int(x*255),colorsys.hsv_to_rgb(*rgb))
----> 9         hex_out.append("".join(map(lambda x: chr(x).encode('hex'),rgb)))
     10     return hex_out

LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs

In [20]:
import colorsys
import random

def random_color(hue=None, sat=None, val=None):
  hue = hue / 360.0 if hue is not None else random.random()
  sat = sat if sat is not None else random.random()
  val = val if val is not None else random.random()
  to_eightbit = lambda value: int(round(value * 255))
  return map(to_eightbit, colorsys.hsv_to_rgb(hue, sat, val))

random_color(hue=0)    # something red:  [186, 98, 98]
random_color(sat=0)    # something gray: [134, 134, 134]
random_color(sat=1, val=1) # max chroma: [36, 0, 255]


Out[20]:
<map at 0x2c6b3dcc358>

In [23]:
random_color(50)


Out[23]:
<map at 0x2c6b3dcf438>

In [ ]: