<jupyter_start><jupyter_text>Cost Incurred vs Revenue Plot for stores in Pune *Bubble size represents Profits<jupyter_code>from bokeh.palettes import Spectral11
from bokeh.models import HoverTool
from bokeh.models import LinearInterpolator, CategoricalColorMapper
from bokeh.models import ColumnDataSource

sss = stdf_city[0].apply(pd.Series)
Profit = sss['Profit']
source = ColumnDataSource(dict(
    x= sss['CostIncurredAtStore'],
    y= sss['RevenueOfStore'],
    profit = sss['Profit'],
    profit_scaled = Profit.apply(lambda x: abs(x/5)),
    stores = stdf_city['Name'],    
    emp = sss['NoOfEmployees'],
        
    ))


def update_m (month):
    sss = stdf_city[month].apply(pd.Series)
    Profit = sss['Profit']
    new_data = dict(
        x= sss['CostIncurredAtStore'],
        y= sss['RevenueOfStore'],
        stores = stdf_city['Name'],    
        emp = sss['NoOfEmployees'],
        profit = Profit,
        profit_scaled = Profit.apply(lambda x: abs(x/5))
        
    )
    source.data = new_data #updating the source data with the newdata i.e data of each year
    p1.title.text = str(month)  #updating the title 
    push_notebook() #push this into chart

size_mapper = LinearInterpolator(     #this is to give size for each data point according to their population
    x=[5, 15],
    y = [3,5]
)

#to give color to each type of data point
color_mapper = CategoricalColorMapper(
   factors = list(stdf_city['Name'].unique()),  #this tells the compiler to color the continents
  palette = Spectral11,)

hover = HoverTool(tooltips = [("Employee Count","@emp"),("Profit","@profit"), ("Revenue","@y"), ("Cost Incurred","@x")], #when u hover mouse on data points
                  show_arrow=False)



PLOT_OPTS = dict(     #the dimensions of figure is given
    height =450,
    width = 750,
    x_axis_type='log',
    x_range=[1300, 30000],
    y_range=(1500,30000)
)


p1 = figure( #how do u want the overall dimensions of fig
    title = str('2013 October Cost Incurred vs Revenue'),toolbar_location='above',  #title should always be in string format
    title_location = 'above',
    tools=[hover],
    **PLOT_OPTS)


p1.circle(
    x='x',y='y', #these have been wriiten before and is being called for the sake of hovering to work and is defined in update function
    size={'field':'profit_scaled'},  #we cant use the size of data point as population as the china population is one billion and all the pixels gets filled and hence we use a mapper and give the rangee of X and y axis
    color = {'field':'stores','transform':color_mapper}, #this will color all the regions defined by color_mapper
    legend='stores', #a legend of which color is what continent
    source=source,  #what is the data source
    alpha=0.9) #how much of transparency of data pojint

p1.legend.border_line_color = None  #to remove the border
p1.legend.location = (10,0)  #this is going to take legend out of the chart box
p1.right.append(p1.legend[0]) #this is going to place the legend to the right
show(p1, notebook_handle=True) #notebook_handle will take the consideration of viewing each year that we defined in update
interact(update_m, month=(0,47,1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10564263322884013, 'fraction_numerical': 0.01818181818181818, 'mean_word_length': 3.8717557251908397, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '428534', 'license': 'Apache-2.0', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1005, 'n_words': 343, 'path': '_notebooks/2020-01-28-Interactive Bubble Plot.ipynb', 'repo_name': 'shwetkm/blog', 'size': '3190'}