def show_grid_plot(self): # Remove rows with NaN values and then map standard states to colors elements.dropna( inplace=True ) # if inplace is not set to True the changes are not written to the dataframe colormap = {'gas': 'yellow', 'liquid': 'orange', 'solid': 'red'} elements['color'] = [colormap[x] for x in elements['standard state']] elements['size'] = elements['van der Waals radius'] / 10 # Create three ColumnDataSources for elements of unique standard states gas = ColumnDataSource(elements[elements['standard state'] == 'gas']) liquid = ColumnDataSource( elements[elements['standard state'] == 'liquid']) solid = ColumnDataSource( elements[elements['standard state'] == 'solid']) # Define the output file path output_file("elements_gridplot.html") # Create the figure object f1 = figure() # adding glyphs f1.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Gas', source=gas) f2 = figure() f2.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Liquid', source=liquid) f3 = figure() f3.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Solid', source=solid) f = gridplot([[f1, f2], [None, f3]]) # Save and show the figure show(f)
def show_elements(self): elements.dropna( inplace=True ) # if inplace is not set to True the changes are not written to the dataframe colormap = {'gas': 'yellow', 'liquid': 'orange', 'solid': 'red'} elements['color'] = [colormap[x] for x in elements['standard state']] elements['size'] = elements["van der Waals radius"] / 10 # Create three ColumnDataSources for elements of unique standard states gas = ColumnDataSource(elements[elements['standard state'] == 'gas']) liquid = ColumnDataSource( elements[elements['standard state'] == 'liquid']) solid = ColumnDataSource( elements[elements['standard state'] == 'solid']) # Define the output file path output_file("elements.html") # adding glyphs self.f.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Gas', source=gas) self.f.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Liquid', source=liquid) self.f.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Solid', source=solid) self.f.xaxis.axis_label = "Atomic radius" self.f.yaxis.axis_label = "Boiling point" # Save and show the figure self.__show__()
""" #Plotting periodic table elements #Importing libraries from bokeh.plotting import figure from bokeh.io import output_file, show from bokeh.layouts import gridplot from bokeh.sampledata.periodic_table import elements from bokeh.models import Range1d, PanTool, ResetTool, HoverTool, ColumnDataSource, LabelSet import pandas from numpy import nan # Drop missing elements elements.dropna(inplace=True) # Add color map colormap = {'gas': 'yellow', 'liquid': 'orange', 'solid': 'red', nan: 'grey'} # Add element color elements['color'] = [colormap[x] for x in elements['standard state']] # Add element size elements['size'] = elements['van der Waals radius'] / 10 # Bokeh ColumnDataSource gas = ColumnDataSource(elements[elements['standard state'] == 'gas']) liquid = ColumnDataSource(elements[elements['standard state'] == 'liquid']) solid = ColumnDataSource(elements[elements['standard state'] == 'solid']) #Define the output file path output_file("elements.html")
from bokeh.plotting import figure from bokeh.io import curdoc from bokeh.sampledata.periodic_table import elements from bokeh.models import Range1d, PanTool, ResetTool, HoverTool, ColumnDataSource, LabelSet, Select from bokeh.models.annotations import Span, BoxAnnotation, Label, LabelSet from bokeh.layouts import layout #Remove rows with NaN values and then map standard states to colors elements.dropna( inplace=True ) #if inplace is not set to True the changes are not written to the dataframe colormap = {'gas': 'green', 'liquid': 'blue', 'solid': 'red'} elements['color'] = [colormap[x] for x in elements['standard state']] elements['size'] = elements["van der Waals radius"] / 10 #Create three ColumnDataSources for elements of unique standard states gas = ColumnDataSource(elements[elements['standard state'] == 'gas']) liquid = ColumnDataSource(elements[elements['standard state'] == 'liquid']) solid = ColumnDataSource(elements[elements['standard state'] == 'solid']) #Create the figure object f = figure() #adding glyphs f.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Gas', source=gas)
from bokeh.sampledata.periodic_table import elements from bokeh.io import output_file, show from bokeh.models import Range1d, PanTool, ResetTool, HoverTool, ColumnDataSource from bokeh.plotting import figure output_file( "C:\Users\labuser\Documents\Python_Data_Analysis\Learning_Bokeh\Output\elements.html" ) f = figure() elements = elements.dropna() colormap = {'gas': 'yellow', 'liquid': 'orange', 'solid': 'red'} elements['color'] = [colormap[x] for x in elements['standard state']] gas = ColumnDataSource(elements[elements['standard state'] == 'gas']) liquid = ColumnDataSource(elements[elements['standard state'] == 'liquid']) solid = ColumnDataSource(elements[elements['standard state'] == 'solid']) f.circle(x="atomic radius", y="boiling point", size=[i / 10 for i in gas.data["van der Waals radius"]], fill_alpha=0.2, color="color", line_dash=[5, 3], legend='Gas', source=gas) f.circle(x="atomic radius", y="boiling point", size=[i / 10 for i in liquid.data["van der Waals radius"]],
def ex_7_create_label_annotations_for_span(self): # Remove rows with NaN values and then map standard states to colors elements.dropna( inplace=True ) # if inplace is not set to True the changes are not written to the dataframe colormap = {'gas': 'yellow', 'liquid': 'orange', 'solid': 'red'} elements['color'] = [colormap[x] for x in elements['standard state']] elements['size'] = elements['van der Waals radius'] / 10 # Create three ColumnDataSources for elements of unique standard states gas = ColumnDataSource(elements[elements['standard state'] == 'gas']) liquid = ColumnDataSource( elements[elements['standard state'] == 'liquid']) solid = ColumnDataSource( elements[elements['standard state'] == 'solid']) # Define the output file path output_file("elements_annotations.html") # Create the figure object f = figure() # adding glyphs f.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Gas', source=gas) f.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Liquid', source=liquid) f.circle(x="atomic radius", y="boiling point", size='size', fill_alpha=0.2, color="color", legend='Solid', source=solid) # Add axis labels f.xaxis.axis_label = "Atomic radius" f.yaxis.axis_label = "Boiling point" # Calculate the average boiling point for all three groups by dividing the sum by the number of values gas_average_boil = sum(gas.data['boiling point']) / len( gas.data['boiling point']) liquid_average_boil = sum(liquid.data['boiling point']) / len( liquid.data['boiling point']) solid_average_boil = sum(solid.data['boiling point']) / len( solid.data['boiling point']) # Create three spans span_gas_average_boil = Span(location=gas_average_boil, dimension='width', line_color='yellow', line_width=2) span_liquid_average_boil = Span(location=liquid_average_boil, dimension='width', line_color='orange', line_width=2) span_solid_average_boil = Span(location=solid_average_boil, dimension='width', line_color='red', line_width=2) # Add spans to the figure f.add_layout(span_gas_average_boil) f.add_layout(span_liquid_average_boil) f.add_layout(span_solid_average_boil) # Add labels to spans label_span_gas_average_boil = Label(x=80, y=gas_average_boil, text="Gas average boiling point", render_mode="css", text_font_size="10px") label_span_liquid_average_boil = Label( x=80, y=liquid_average_boil, text="Liquid average boiling point", render_mode="css", text_font_size="10px") label_span_solid_average_boil = Label( x=80, y=solid_average_boil, text="Solid average boiling point", render_mode="css", text_font_size="10px") # Add labels to figure f.add_layout(label_span_gas_average_boil) f.add_layout(label_span_liquid_average_boil) f.add_layout(label_span_solid_average_boil) # Save and show the figure show(f)
def exercise_8_creating_span_annotations_depicting_averages(self): # start Bokeh server by: bokeh serve server.py # Remove rows with NaN values and then map standard states to colors elements.dropna( inplace=True ) # if inplace is not set to True the changes are not written to the dataframe colormap = {'gas': 'yellow', 'liquid': 'orange', 'solid': 'red'} elements['color'] = [colormap[x] for x in elements['standard state']] elements[ 'van der Waals radius'] = elements['van der Waals radius'] / 10 # Create three ColumnDataSources for elements of unique standard states gas = ColumnDataSource(elements[elements['standard state'] == 'gas']) liquid = ColumnDataSource( elements[elements['standard state'] == 'liquid']) solid = ColumnDataSource( elements[elements['standard state'] == 'solid']) # Create the figure object f = figure() # adding glyphs size_list = [i / 10 for i in gas.data["van der Waals radius"]] f.circle(x="atomic radius", y="boiling point", size="van der Waals radius", fill_alpha=0.2, color="color", legend='Gas', source=gas) f.circle(x="atomic radius", y="boiling point", size="van der Waals radius", fill_alpha=0.2, color="color", legend='Liquid', source=liquid) f.circle(x="atomic radius", y="boiling point", size="van der Waals radius", fill_alpha=0.2, color="color", legend='Solid', source=solid) # Add axis labels f.xaxis.axis_label = "Atomic radius" f.yaxis.axis_label = "Boiling point" # Calculate the average boiling point for all three groups by dividing the sum by the number of values gas_average_boil = sum(solid.data['boiling point']) / len( solid.data['boiling point']) liquid_average_boil = sum(liquid.data['boiling point']) / len( liquid.data['boiling point']) solid_average_boil = sum(solid.data['boiling point']) / len( solid.data['boiling point']) solid_min_boil = min(solid.data['boiling point']) solid_max_boil = max(solid.data['boiling point']) # Create three spans span_gas_average_boil = Span(location=gas_average_boil, dimension='width', line_color='yellow', line_width=2) span_liquid_average_boil = Span(location=liquid_average_boil, dimension='width', line_color='orange', line_width=2) span_solid_boil = Span( location=solid_average_boil, dimension='width', line_color='red', line_width=2 ) # Location for this span will be updated when the Select widget is changed by the user # Add spans to the figure f.add_layout(span_gas_average_boil) f.add_layout(span_liquid_average_boil) f.add_layout(span_solid_boil) # Create a function that updates the location attribute value for span_solid_boil span # Also note that select.value returns values as strings so we need to convert the returned value to float def update_span(attr, old, new): span_solid_boil.location = float(select.value) # Select widgets expect a list of tuples of strings, so List[Tuple(String, String)], that's why you should convert average, max, and min to strings options = [(str(solid_average_boil), "Solid Average Boiling Point"), (str(solid_min_boil), "Solid Minimum Boiling Point"), (str(solid_max_boil), "Solid Maximum Boiling Point")] # Create the Select widget select = Select(title="Select span value", options=options) select.on_change("value", update_span) # Add Select widget to layout and then the layout to curdoc lay_out = layout([[select]]) curdoc().add_root(f) curdoc().add_root(lay_out)