seaborn is a statistical plotting library to interact well with Panda’s DataFrame
It is built directly off the MatplotLib but uses simpler one line syntax
Scatter plots line up a set of two continuous features (Age, Salary, Height, Temp)
pip install seaborn # For installation run this command
Using hue in scatterplot – make your plots from 2D information to 3D information
df = pd.read_csv(“dm_office_sales.csv”) plt.figure(figsize=(12,4), dpi=100) sns.scatterplot(x=’salary’, y=’sales’, data=df, hue=’level of education’, palette=’Dark2′, style=’level of education’, alpha=0.7)
Aabhar : Jose Portilla (Head of Data Science at Pierian Training) @Udemy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv("dm_office_sales.csv")
df.head()
sns.scatterplot(x='salary', y='sales', data=df)
# Let us make the figure size better
plt.figure(figsize=(12,4), dpi=100)
sns.scatterplot(x='salary', y='sales', data=df)
# hue - coloring the scatter plots based on a) cateogrical column or b) continuous colum
sns.scatterplot(x='salary', y='sales', data=df, hue='level of education')
# hue - for a continuou value, seaborn takes the gradient automatically - same color faded or smoothens
# Using palette - provide the color map options. Details below:
# https://matplotlib.org/stable/gallery/color/colormap_reference.html below:
sns.scatterplot(x='salary', y='sales', data=df, hue='level of education', palette ='Dark2')
# Size setting of points,
# Higher the sales - bigger the points
sns.scatterplot(x='salary', y='sales', data=df, size='sales' )
# transperency to the points using alpha 0 - Fully transparent, 1 = Default
sns.scatterplot(x='salary', y='sales', data=df, alpha=0.3)
# Markers - points style - depending upon the categorical column values passed
sns.scatterplot(x='salary', y='sales', data=df, hue='level of education', style='level of education')
df = pd.read_csv("dm_office_sales.csv")
plt.figure(figsize=(12,4), dpi=100)
sns.scatterplot(x='salary', y='sales', data=df, hue='level of education', palette='Dark2', style='level of education', alpha=0.7)
Aabhar : Jose Portilla (Head of Data Science at Pierian Training) @Udemy
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,10)
y = x * 2 #np.log(x)
plt.plot(x,y)
plt.xlabel('My X Axes')
plt.ylabel("My Y Axes")
plt.title("Sample Plot")
plt.xlim(0,9)
plt.ylim(0,15) # lim allows to shrink and fit the ploat within provided value - you may loosed the graph here
plt.show() # If you run the script as python code not as jupyter-notebook, this is needed
# Using conventional functional method of saving fig using savefig() method
#plt.savefig('D:\\DataScienceLearning\\PythonPrograms\\RK_PGMS\\myimage.png', dpi=100.0)
#help(plt.savefig)
# USING FIGURE OBJECT
figObj = plt.figure()
print(type(figObj))
# RES -> <class 'matplotlib.figure.Figure'>
# Below creates a canvas for ratio for LEFT, BOTTOM, WIDTH, HEIGHT -> [(x,y),(w,h)]
figObj.add_axes([0,0,1,1]) # there numbers are basically a ratio number
figObj2 = plt.figure()
myAxes_A = figObj2.add_axes([0,0,1,1]) # This will occupy just half the canvas - This has notthing to do with the actual data points
myAxes_B = figObj2.add_axes([0.25,0.25,0.125,0.125]) # 1/4th and 1/8th
# Let us add multiple plots within the same figure
myAxes_A.plot(np.linspace(0,5,6), np.linspace(0,5,6)**4)
myAxes_B.plot(x,y) #Subplot with smaller window
# PUTTING LEGENDS, linewidth, linestyle, marker
fig = plt.figure()
ax= fig.add_axes([0,0,1,1],title='MyGraph', xlabel='X Axes', ylabel='Y Axes')
myXSets = np.linspace(0,10,10)
myYSets_1 = myXSets
myYSets_2 = myXSets**2
ax.plot(myXSets, myYSets_1, label="Set1", color='purple', marker='o', markersize=10)
ax.plot(myXSets, myYSets_2, label="Set2", marker = '+', linewidth = 10, linestyle='--', markeredgecolor='red') #Line width/style
# Set your legends to a location with loc value or number
ax.legend(loc='center left') # best-0,upper right-1, upper left-2, lower left-3, lower right-4, right = 5...
# You can set as per convas
ax.legend(loc=(1.1,0.75))