# this is the convention to import matplotlib
import matplotlib.pyplot as plt
# we need numpy to generate info
import numpy as np
# get info
x = np.linspace(0,5,11)
y = x**2
plt.plot(x,y)
plt.show()
# we can add attributes to modify the graphic
plt.plot(x,y,'ys-') # attribute 'ys-' means: Show me yellow line (y) with squares united (s-)
plt.show()
plt.hist(x)
plt.show()
plt.pie(x)
plt.show()
plt.scatter(x,y)
plt.show()
plt.boxplot(x)
plt.show()
plt.subplot(1,2,1) # (#rows, #colums, #index) - Graphic 1
plt.plot(x,y)
plt.subplot(1,2,2) # - Graphic 2
plt.hist(x)
plt.show()
plt.subplot(1,2,1)
plt.plot(x,y, 'r--') # Plot 1
plt.plot(y,x, 'b:') # Plot 2
plt.subplot(1,2,2) # Graphic 2
plt.pie(y)
plt.show()
# axes parameters: [pos axis x, pos axis y, size graph axis x, size graph axis y]
fig = plt.figure() # Object
axes = fig.add_axes([0.1, 0.1, 0.8, 0.9]) # Object graphic 1
axes2 = fig.add_axes([0.17, 0.55, 0.4, 0.3]) # Object graphic 2
axes.plot(x,y,'b') # Plot 1
axes2.plot(y,x,'r') # Plot 2
axes2.set_facecolor('gray')
fig.show()
# Generate a objects with 2 rows and 4 colums. In total 8 objects
fig, axes = plt.subplots(2,4)
# Each object we can access through the matrix index
axes[0,0].plot(x,np.cos(x)*-1)
axes[0,1].plot(x,np.sin(x)*-1, 'r')
axes[0,2].plot(x,np.tan(x)*-1, 'g')
axes[0,3].plot(x,np.cos(x)*-1)
axes[1,0].plot(x,np.cos(x))
axes[1,1].plot(x,np.sin(x), 'r')
axes[1,2].plot(x,np.tan(x), 'g')
axes[1,3].plot(x,np.cos(x)**2)
# Improved visualization of the axes of each chart
fig.tight_layout()
plt.plot(np.sin(x),y, label='sin(x)')
plt.title('This is a title')
plt.xlabel('Axis x')
plt.ylabel('Axis y')
plt.legend(loc='lower left') # It is necessary to add this line for our label to appear.
plt.show()
fig, axes = plt.subplots(1,2,figsize=(5,5))
# Object 1
axes[0].plot(np.sin(x),y,label='$sin(x)$')
axes[0].set_title('Title object 1')
axes[0].set_xlabel('Axis x Object 1')
axes[0].set_ylabel('Axis y Object 1')
axes[0].legend() # It is necessary to add this line for our label to appear.
#Object 2
axes[1].plot(y,np.sin(x),label='Relation y-sin(x)')
axes[1].set_title('Title object 2')
axes[1].set_xlabel('Axis x Object 2')
axes[1].set_ylabel('Axis y Object 2')
axes[1].legend(loc='lower right')
fig.tight_layout()
# Show us all styles that Matplotlib has
print(plt.style.available)
# Line to define style that we will use in ours graphics
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(7,7))
ax.plot(x,x+1,'r')
fig.tight_layout()
fig, ax = plt.subplots(figsize=(7,7))
ax.plot(x,x+1,'r--')
ax.plot(x,x+2,'b-')
ax.plot(x,x+3,'y.-')
ax.plot(x,x+4,'go:')
fig.tight_layout()
fig, ax = plt.subplots(figsize=(7,7))
ax.plot(x,x+1,color='#D426C8', alpha=0.2, linewidth=2, linestyle='--', marker='x') # RGB colors
ax.plot(x,x+2,color='#66FF89',linewidth=8, linestyle='-', marker='o', markersize=7)
ax.plot(x,x+3,color='blue',linewidth=6, linestyle='dashed', marker='v') # Comun colors
ax.plot(x,x+4,color='white',linewidth=4, linestyle=':', marker='P', markerfacecolor='blue')
fig.tight_layout()
# Get data
countrys = ['Germany', 'Switzerland', 'Greece', 'Italy', 'England']
population = [1000,800,900,1000,300]
# Line to define style that we will use in ours graphics
plt.style.use('default')
plt.bar(countrys, population, width=0.5, color=['r', 'b'])
plt.xticks(rotation=45)
plt.title('Country Population')
plt.xlabel('Countrys')
plt.ylabel('Population')
plt.show()
plt.barh(countrys, population, color=['r', 'b','y','c'])
plt.xticks(rotation=45)
plt.title('Country Population')
plt.xlabel('Countrys')
plt.ylabel('Population')
plt.show()
# Get data
data = np.random.normal(5,10,100)
plt.hist(data, bins=7) # bins = bars numbers
plt.show()
plt.hist(data, bins=6, histtype='step') # bins = bars numbers
plt.show()
plt.boxplot(data, vert=False, patch_artist=True, notch=True, showfliers=False)
plt.show()
# Get data
N=50
x = np.random.rand(N)
y = np.random.rand(N)
area = (30 * np.random.rand(N)) **2
colors = np.random.rand(N)
plt.scatter(x,y,s=area, c=colors, alpha=0.5)
plt.show()
# this is the convention to import seaborn
import seaborn as sns
sns.set(style='dark', palette='dark', font="Verdana", font_scale=1)
sns.barplot(x=['A','B','C'], y=[1,3,2])
plt.show() # seaborn is built with matplotlib. Some matplotlib commands affect seaborn, for example plt.show()
tips = sns.load_dataset('tips') # Dataset from seaborn
sns.displot(data= tips, x= 'total_bill')
plt.show()
# The hue attribute allows us to group with other variable
sns.displot(data= tips, x='total_bill', y='tip', hue ='sex')
plt.show()
# legend: dont show us hue segmentation
# palette: colors
# alpha: transparency
sns.displot(data= tips, x='total_bill', hue ='sex', kind ='kde', legend= False, palette='dark', alpha =.5)
plt.show()
# Chart for distributions and accumulation
sns.histplot(data=tips, x='tip', bins =15, cumulative=True)
plt.show()
# As shown on the y-axis and stat, present the multiple data
# multiple has other types: stack, layer and fill
sns.histplot(data=tips, x='tip', bins=15, hue='sex', stat='density', multiple='dodge')
plt.show()
# kde graphs: density graph
sns.kdeplot(data=tips, x='tip', hue='sex')
plt.show()
# with accumulation
sns.kdeplot(data=tips, x='tip', hue='sex', cumulative=True)
plt.show()
# shade: Create an area under the curve
sns.kdeplot(data=tips, x='tip', hue='sex', shade=True)
plt.show()
# displot: distribution graphs
# kind: allow us change graphs
sns.displot(data=tips, x='tip', hue='sex',kind='hist', multiple='stack')
plt.show()
# countplot: variables are taken into account
sns.countplot(data=tips, x='day', hue='sex'); # with "a" in the final line doesnt need plt.show()
sns.swarmplot(data=tips, x='day', y ='total_bill', hue='sex', dodge=True);
sns.boxplot(data=tips, x='day', y='total_bill', hue='sex', dodge=True);
# Violin: similar to Boxplot, but does not show the quartiles, but how the data is concentrated with split comparison.
sns.violinplot(data=tips, x='day', y='total_bill', hue='sex', split=True, palette='pastel');
# kind: allow us change graph
# col: allow us add new variable
sns.catplot(data=tips, x='day', y='total_bill', hue='sex', dodge=True, kind='box', col='time');
# scatter
sns.scatterplot(data= tips, x= 'total_bill', y = 'tip', hue= 'day');
sns.scatterplot(data=tips, x='total_bill', y ='tip', hue='day', style='time', size='size')
plt.legend(loc='center', bbox_to_anchor=(1.12, 0.5))
plt.show()
markers = {"Lunch": 'D', "Dinner": 's'}
sns.scatterplot(data=tips, x='total_bill', y ='tip', hue='day', style='time', size='size', markers=markers);
plt.legend(loc='center', bbox_to_anchor=(1.12, 0.5));
# Line
sns.lineplot(data=tips, x='total_bill', y ='tip', hue='day', style='time', size='size');
plt.legend(loc='center', bbox_to_anchor=(1.12, 0.5));
sns.relplot(data=tips, x='total_bill', y='tip', hue='day', style='time', size='size', kind='scatter', col='time');
# jointplot: joins 2 different graphs into one
sns.jointplot(data=tips, x='total_bill', y='tip', hue ='sex');
# more custom
sns.jointplot(data=tips, x='total_bill', y='tip', hue='sex', kind='hist', marginal_ticks=True, # shows a small table for the external graphic
marginal_kws=dict(bins=25, fill =True, multiple='dodge')); # marginal_kws: different parameters can be added to it
# Pairplot: correlates all the numeric variables in the dataset
sns.pairplot(data= tips, hue='sex');
# corner: show us one to one relation
sns.pairplot(data= tips, hue='sex', corner=True);
# Correlation between variables
tips.corr()
# Heatmap
sns.heatmap(tips.corr());
# annot: show us value
sns.heatmap(tips.corr(), annot=True, linewidths=5);