data visualisation seaborn
Slides do conjunto (12)
-
- Important Features of Seaborn
- Seaborn is built on top of Python’s core visualization library Matplotlib. It is meant to serve as a complement, and not a replacement. However, Seaborn comes with some very important features. Let us see a few of them here. The features help in −
- Built in themes for styling matplotlib graphics
- Visualizing univariate and bivariate data
- Fitting in and visualizing linear regression models
- Plotting statistical time series data
- Seaborn works well with NumPy and Pandas data structures
- It comes with built in themes for styling Matplotlib graphics
-
- Importing Data as Pandas DataFrame
- In this section, we will import a dataset. This dataset loads as Pandas DataFrame by default. If there is any function in the Pandas DataFrame, it works on this DataFrame.
- The following line of code will help you import the dataset −
- # Seaborn for plotting and styling import seaborn as sb
- df = sb.load_dataset('tips')
- print df.head()
- DataFrames store data in the form of rectangular grids by which the data can be over viewed easily. Each row of the rectangular grid contains values of an instance, and each column of the grid is a vector which holds data for a specific variable. This means that rows of a DataFrame do not need to contain, values of same data type, they can be numeric, character, logical, etc. DataFrames for Python come with the Pandas library, and they are defined as two-dimensional labeled data structures with potentially different types of columns
-
- The above two figures show the difference in the default Matplotlib and Seaborn plots. The representation of data is same, but the representation style varies in both.
- Basically, Seaborn splits the Matplotlib parameters into two groups−
- Plot styles
- Plot scale
-
- Removing Axes Spines
- In the white and ticks themes, we can remove the top and right axis spines using the despine() function.
- Overriding the Elements
- If you want to customize the Seaborn styles, you can pass a dictionary of parameters to the set_style() function. Parameters available are viewed using axes_style() function.
- Example
- import seaborn as sb
- print sb.axes_style
-
- Scaling Plot Elements
- We also have control on the plot elements and can control the scale of plot using the set_context() function. We have four preset templates for contexts, based on relative size, the contexts are named as follows
- uilding Color Palette
- Seaborn provides a function called color_palette(), which can be used to give colors to plots and adding more aesthetic value to it.
- Usage
- seaborn.color_palette(palette = None, n_colors = None, desat = None)
-
- Return
- Return refers to the list of RGB tuples. Following are the readily available Seaborn palettes −
- Deep
- Muted
- Bright
- Pastel
- Dark
- Colorblind
- Besides these, one can also generate new palette
- It is hard to decide which palette should be used for a given data set without knowing the characteristics of data. Being aware of it, we will classify the different ways for using color_palette() types −
- qualitative
- sequential
- diverging
- We have another function seaborn.palplot() which deals with color palettes. This function plots the color palette as horizontal array. We will know more regarding seaborn.palplot()in the coming examples.
-
- Sequential Color Palettes
- Sequential plots are suitable to express the distribution of data ranging from relative lower values to higher values within a range.
- Appending an additional character ‘s’ to the color passed to the color parameter will plot the Sequential plot.
- Diverging Color Palette
- Diverging palettes use two different colors. Each color represents variation in the value ranging from a common point in either direction.
- Assume plotting the data ranging from -1 to 1. The values from -1 to 0 takes one color and 0 to +1 takes another color.
- By default, the values are centered from zero. You can control it with parameter center by passing a value.
- Plotting Univariate Distribution
- Distribution of data is the foremost thing that we need to understand while analysing the data. Here, we will see how seaborn helps us in understanding the univariate distribution of the data.
- Function distplot() provides the most convenient way to take a quick look at univariate distribution. This function will plot a histogram that fits the kernel density estimation of the data.
-
- Kernel Density Estimation (KDE) is a way to estimate the probability density function of a continuous random variable. It is used for non-parametric analysis.
- Setting the hist flag to False in distplot will yield the kernel density estimation plot.
- Scatter Plot
- Scatter plot is the most convenient way to visualize the distribution where each observation is represented in two-dimensional plot via x and y axis.
- Hexbin Plot
- Hexagonal binning is used in bivariate data analysis when the data is sparse in density i.e., when the data is very scattered and difficult to analyze through scatterplots.
- An addition parameter called ‘kind’ and value ‘hex’ plots the hexbin plot.
-
- Kernel Density Estimation
- Kernel density estimation is a non-parametric way to estimate the distribution of a variable. In seaborn, we can plot a kde using jointplot().
- Pass value ‘kde’ to the parameter kind to plot kernel plot.
- Example
- import pandas as pd
- import seaborn as sb
- from matplotlib import pyplot as plt
- df = sb.load_dataset('iris')
- sb.jointplot(x = 'petal_length',y = 'petal_width',data = df,kind = 'hex')
- plt.show()
-
- Datasets under real-time study contain many variables. In such cases, the relation between each and every variable should be analyzed. Plotting Bivariate Distribution for (n,2) combinations will be a very complex and time taking process.
- To plot multiple pairwise bivariate distributions in a dataset, you can use the pairplot()function. This shows the relationship for (n,2) combination of variable in a DataFrame as a matrix of plots and the diagonal plots are the univariate plots.
- In our previous chapters we learnt about scatter plots, hexbin plots and kde plots which are used to analyze the continuous variables under study. These plots are not suitable when the variable under study is categorical.
- When one or both the variables under study are categorical, we use plots like striplot(), swarmplot(), etc,. Seaborn provides interface to do so.
- Box Plots
- Boxplot is a convenient way to visualize the distribution of data through their quartiles.
- Box plots usually have vertical lines extending from the boxes which are termed as whiskers. These whiskers indicate variability outside the upper and lower quartiles, hence Box Plots are also termed as box-and-whisker plot and box-and-whisker diagram. Any Outliers in the data are plotted as individual points.
-
- Violin Plots
- Violin Plots are a combination of the box plot with the kernel density estimates. So, these plots are easier to analyze and understand the distribution of the data.
- Let us use tips dataset called to learn more into violin plots. This dataset contains the information related to the tips given by the customers in a restaurant.
- Bar Plot
- The barplot() shows the relation between a categorical variable and a continuous variable. The data is represented in rectangular bars where the length the bar represents the proportion of the data in that category.
- Bar plot represents the estimate of central tendency. Let us use the ‘titanic’ dataset to learn bar plots.
- Categorical data can we visualized using two plots, you can either use the functions pointplot(), or the higher-level function factorplot().
- What is Facet Grid?
- Facet grid forms a matrix of panels defined by row and column by dividing the variables. Due of panels, a single plot looks like multiple plots. It is very helpful to analyze all combinations in two discrete variables.
- Let us visualize the above the definition with an example
- The advantage of using Facet is, we can input another variable into the plot. The above plot is divided into two plots based on a third variable called ‘diet’ using the ‘col’ parameter.
- We can make many column facets and align them with the rows of the grid −
-
- Most of the times, we use datasets that contain multiple quantitative variables, and the goal of an analysis is often to relate those variables to each other. This can be done through the regression lines.
- While building the regression models, we often check for multicollinearity, where we had to see the correlation between all the combinations of continuous variables and will take necessary action to remove multicollinearity if exists. In such cases, the following techniques helps.
Informações
- Descrição
- Slides sobre data visualisation seaborn, criado por Sayooj Balakrishnan em 11-12-2018.
- Criado por
- Sayooj Balakrishnan
- Visualizações
- 1
- Avaliação