🏙 Thebe Experiments with Live Python Coding 01
✒️ Cell #1. Wolfram & SymPy
!pip install -q sympy
import sympy,pylab as plt,numpy as np
from sympy.parsing.mathematica import parse_mathematica
formula_string="2.7-2*Cos[x]*Cos[y]-0.7*Cos[Pi-2*y]"
x,y=sympy.symbols('x,y')
expr=parse_mathematica(formula_string)
x_values,y_values=np.linspace(-3,3,100),np.linspace(-3,3,100)
X,Y=np.meshgrid(x_values,y_values)
Z=np.array([[sympy.lambdify((x,y),expr)(x_val,y_val)
for x_val in x_values] for y_val in y_values])
fig=plt.figure(figsize=(9,6)); ax=fig.add_subplot()
cf=ax.contourf(X,Y,Z,cmap=plt.cm.bone_r,alpha=.5,
hatches=['.','-','/','\\','+','//','\\\\'])
cp=ax.contour(X,Y,Z,linewidths=2,cmap=plt.cm.cool_r)
fmt={l:f'z={l:.2f}' for l in cp.levels}
ax.clabel(cp,cp.levels[1::2],inline=True,fmt=fmt,fontsize=12)
fig.colorbar(cp,shrink=1.); fig.colorbar(cf,shrink=1.)
ax.set_xlabel('X'); ax.set_ylabel('Y')
ax.grid(color='slategray',alpha=.1);
✒️ Cell #2. Pygal & Pandas
!pip install -q pygal
import pygal,pandas as pd
from IPython.display import SVG,display
from pygal.style import BlueStyle
columns=['Fresh','Milk','Grocery','Frozen','Detergents_Paper','Delicatessen']
colors=['#1b2c45','#5a8bbd','#008b8b','#ff5a8b']
index= ['C1','C2','C3']
data=[[26373,36423,22019,5154,4337,16523],[16165,4230,7595,201,4003,57],
[14276,803,3045,485,100,518]]
samples=pd.DataFrame(data,columns=columns,index=index)
line=pygal.Line(
fill=False,height=300,width=1000,
style=BlueStyle(opacity='.3',colors=colors,background='transparent'))
line.title='Samples of the Dataset "Wholesale Customers"'
line.x_labels=columns
line.add('C1',data[0]); line.add('C2',data[1])
line.add('C3',data[2]); line.add('MEAN',samples.mean())
line.render_to_file('samples.svg')
display(SVG(open('samples.svg','rb').read()))