Skip to content

Instantly share code, notes, and snippets.

@AntonioMarsella
AntonioMarsella / pycodestyle_check_multiple_files.py
Created June 6, 2020 12:36
pycodestyle check multiple files
# Import needed package
import pycodestyle
# Create a StyleGuide instance
style_checker = pycodestyle.StyleGuide()
# Run PEP 8 check on multiple files
result = style_checker.check_files(['nay_pep8.py', 'yay_pep8.py'])
# Print result of PEP 8 style check
@AntonioMarsella
AntonioMarsella / bokeh_color_mapping.py
Created March 19, 2020 14:11
bokeh color mapping
from bokeh.models import CategoricalColorMapper
mapper = CategoricalColorMapper(factors=['setosa', 'virginica', 'versicolor], palette=['red', 'green', 'blue'])
plot = figure(x_axis_label='petal_length', y_axis_label='sepal_length')
plot.circle('petal_length', 'sepal_length', size=10, source=source, color={'field': 'species', 'transform': mapper}=
@AntonioMarsella
AntonioMarsella / bokek_tools.py
Last active March 19, 2020 14:03
bokeh plotting tools
plot = figure(tools='box_select, lasso_select')
plot.circle(petal_length, sepal_length, selection_color='red', nonselect_fill_alpha=0.2, nonselection_fill_color='grey')
#to apport change to the visual appearance
from bokeh.models import HoverTool
#it doesn't display the tooltips and shows an horizontal line under the mouse position for the inspection
hover = HoverTool(tooltips=None, mode='hline')
plot = figure(tools[hover, 'crosshair'])
plot.circle(x, y, size=15, hover_color='red')
@AntonioMarsella
AntonioMarsella / bokeh_columndatasource.py
Created March 18, 2020 12:52
Column Data Source basic in bokeh
from bokeh.models import ColumnDataSource
source = ColumnDataSource(data={'x': [1,2,3,4,5], 'y': [8,6,5,2,3]})
#pass a python dict to the initializer, that should have string keys and two arrays of
#the SAME length
print(source.data)
from bokeh.sampledata.iris import flowers as df
column_from_dataframe = ColumnDataSource(df)
@AntonioMarsella
AntonioMarsella / bokeh_pandas.py
Created March 18, 2020 12:44
Using Pandas Dataframe in Bokeh with flowers (iris dataset)
from bokeh.io import output_file, show
from bokeh.plotting import figure
# Flowers is a Pandas Dataframe
from bokeh.sampledate.iris import flowers
plot = figure()
plot.circle(flowers['petal_length'], flowers['sepal_length'], size=10)
output_file('pandas.html')
show(plot)
@AntonioMarsella
AntonioMarsella / bokeh_numpy.py
Created March 18, 2020 12:42
passing numpy arrays to bokeh
from bokeh.io import output_file, show
from bokeh.plotting import figure
import numpy as np
x = np.linspace(0, 100, 1000) #one number every 100 till 1000
y = np.sin(x) + np.random.random(1000) * 0.2 #random noise added to sinusoid
plot = figure()
plot.line(x,y)
output_file('numpy.html')
show(plot)
from bokeh.io import output_file
from bokeh.plotting import figure
xs = [ [1,1,2,2,], [2,2,4], [2,2,3,3] ]
ys = [ [2,5,5,2], [3,5,5], [2,3,4,2] ]
plot = figure()
plot.patches(xs, ys, fill_color= ['red', 'blue', 'green'], line_color='white')
output_file('patches.html')
show(plot)
@AntonioMarsella
AntonioMarsella / bokeh_line.py
Created March 17, 2020 19:17
lines and markers plot using bokeh glyphs
from bokeh.io import output_file, show
from bokeh.plotting import figure
x = [1,2,3,4,5]
y = [8,6,5,2,3]
plot = figure()
plot.line(x, y, line_width=2)
#to use markers
plot.circle(x, y, fill_color='white', size=10)
output_file('line.html')
show(plot)
@AntonioMarsella
AntonioMarsella / bokeh_circle.py
Last active March 17, 2020 18:39
circles with bokeh
from bokeh.io import output_file, show
from bokeh.plotting import figure
plot = figure(x_axis_label='x axis', y_axis_label='y axis',plot_width=400, tools='pan,box_zoom')
plot.circle([1,2,3,4,5], [8,6,5,2,3]) #x and y coordinates lists
output_file('circle.html')
show(plot) #save and open to the browser
plot1 = figure()
plot1.circle(x=10, y=[2,5,8,12], size=[10,20,30,40])
@AntonioMarsella
AntonioMarsella / read_json.py
Created March 17, 2020 07:10
Read Json files
import json
with open('filename.json', 'r') as f:
json_dict = json.load(f)
for key in json_dict:
print(key['attribute'])