import numpy as np
Plotting
Graphical production
Matplotlib
Scatter plotting
= plt.subplots()
fig, ax for label in ["first", "second", "third"]:
10)*20, np.random.randn(10)*2,
ax.scatter(np.random.randn(=np.random.randint(10,30,10)**2, cmap=plt.cm.Spectral, alpha=0.5, label=label);
s
#Legend configuring
= plt.legend(loc='upper left', shadow=True)
legend = legend.get_frame()
frame '0.50') frame.set_facecolor(
Sympy
Ploting functions from text description
"(exp(x)-exp(-x))/(exp(x)+exp(-x))", xlim=(-2.5,2.5)) sympy.plot(
<sympy.plotting.plot.Plot>
Seaborn
First create a dataFrame with a continuous variable and a categorical variable to group the histograms
np.random.rand()= pd.DataFrame(data=np.concatenate((np.random.rand(10,1),np.random.randint(0, high=2, size=(10,1))), axis=1), columns=['indep_var',"group_var"])
frame "group_var"] = pd.Categorical(frame["group_var"].apply(lambda x: "A" if x else "B")) frame[
Or use np.random.choice to select random values in a list-type
'A','B'], size=10, replace=True) #, p=None -> prob_distribution np.random.choice([
array(['B', 'B', 'A', 'A', 'A', 'B', 'A', 'B', 'A', 'B'], dtype='<U1')
Barplots and Countplots from SNS
= plt.subplots(1,2, figsize=(9,3))
fig,axs =frame, y='indep_var', x="group_var", ax=axs[0]).set(title="Indep. var. values")
sns.barplot(data=frame, x="group_var", ax=axs[1]).set(title="Count in group"); sns.countplot(data
Small detour - categorical variables’ inner code can be accessed through:
print(frame["group_var"].cat.categories)
"group_var"].cat.codes.values frame[
Index(['A', 'B'], dtype='object')
array([0, 0, 1, 0, 1, 1, 0, 1, 0, 1], dtype=int8)
Box plot and distribution
= plt.subplots(1,2, figsize=(9,3))
fig,axs =frame, x="group_var", y="indep_var", ax=axs[0]).set(title="Boxplot")
sns.boxenplot(data=frame, x="indep_var", ax=axs[1]).set(title="Distribution"); sns.kdeplot(data
PIL
Loading and applying built-in filters and the kernel
= Image.open("bears.jpg").resize((400,300))#.rotate(-90)
img
= [
filters
ImageFilter.BLUR,
ImageFilter.CONTOUR,
ImageFilter.DETAIL,
ImageFilter.EDGE_ENHANCE,
ImageFilter.EDGE_ENHANCE_MORE,
ImageFilter.EMBOSS,
ImageFilter.FIND_EDGES,=(3,3), kernel=[1,1,1,1,1,1,1,1,1]),
ImageFilter.Kernel(size
ImageFilter.SHARPEN,
ImageFilter.SMOOTH,
ImageFilter.SMOOTH_MORE,=3),
ImageFilter.MaxFilter(size=3)
ImageFilter.MinFilter(size
]
= [(img, 'Original')] + [(img.filter(a_filter), a_filter.name) for a_filter in filters]
imgs =(10, 30))
plt.figure(figsize
for i, (image, label) in enumerate(imgs):
= plt.subplot((len(imgs)+1)//2, 2, i + 1)
ax
plt.imshow(image)
plt.title(label)"off") plt.axis(
Drawing on the image
= ImageDraw.Draw(img)
Drawer 245,130,320,190), fill=None, outline="red", width=3)
Drawer.rectangle((205,80,290,155), fill=None, outline="blue", width=3)
Drawer.rectangle(( img