Python Seaborn #1 – A quick reference

  1. seaborn is a statistical plotting library to interact well with Panda’s DataFrame
  2. It is built directly off the MatplotLib but uses simpler one line syntax
  3. Scatter plots line up a set of two continuous features (Age, Salary, Height, Temp)
  4. pip install seaborn # For installation run this command
  5. Using hue in scatterplot – make your plots from 2D information to 3D information
  6. 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)
  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")

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:
# 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)

Python MatPlotLib – A quick reference

  1. matplotlib is a library in Python for plotting/visualizing the data
  2. Two approaches for creating plots – i) functional based methods ii) OOP based methods using FIGURE object
  3. Figure object helps you in movig the axes within the same canvas, thus allowing to have multiple plots in a single canvas
  4. Main lib –
  5. 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.xlabel('My X Axes')
plt.ylabel("My Y Axes")
plt.title("Sample Plot")
plt.ylim(0,15)  # lim allows to shrink and fit the ploat within provided value - you may loosed the graph here # 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)

figObj = plt.figure()
# 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