Knowledge Base

Takeaway Sheet: The seaborn Library

Practice

1# Check for available styles
2
3import matplotlib.pyplot as plt
4print(plt.style.available) # calling different sets of colors

1# Plot a graph with a given style
2
3with plt.style.context('seaborn-pastel'):
4 # write your code here

1# Apply the style to all plots in the project
2
3plt.style.use('ggplot') # the ggplot style is selected here

1# Construct a jointplot
2# the kind='reg' parameter: to plot a density distribution or regression in addition
3# the color parameter: to select the graph color
4import seaborn as sns
5import pandas as pd
6
7sns.jointplot(x="column1", y="column2", data=df, kind='reg', color='blue')

1# view current color palette
2
3current_palette = sns.color_palette("coolwarm", 20)
4print(sns.palplot(current_palette))

1# set the color palette
2
3sns.set_palette('dark')

1# Set the style
2# Parameter options: 'darkgrid' (by default), 'whitegrid', 'dark', 'white', or 'ticks'
3# 'dark', 'white', or 'ticks' don't have grids
4# 'whitegrid' is good for complex graphs
5
6sns.set_style("dark")

1# Load a built-in dataset
2
3import seaborn as sns
4iris = sns.load_dataset("iris")
5print(iris.head())

1# plot a bar graph
2# the estimator method to aggregate data
3import seaborn as sns
4ax = sns.barplot(x="column1", y="column2", data=df, estimator=median)

1# plot box and whisker graph
2# hue - an extra parameter for a third dimension
3
4import seaborn as sns
5
6ax = sns.boxplot(x="column1", y="column2", hue="diet", data=df)

1# plot a line graph and histogram for a density distribution
2# 'bins': the number of bins
3
4import seaborn as sns
5
6sns.distplot(df['column'], bins=10)

1# pair distribution plot
2# hue - an extra parameter for a third dimension
3
4sns.pairplot(df, hue="column3")

1# Construct a violin plot
2# palette - the plot style
3
4sns.violinplot(x="column1", y="column2", data=df, palette='rainbow')

1# Construct a strip plot, or a scatter chart for each category
2
3sns.stripplot(x="column1", y="column2", data=df)
Send Feedback
close
  • Bug
  • Improvement
  • Feature
Send Feedback
,