👀
Crash Visualization
  • Welcome
  • Preface
    • Who the book is written for
    • How the book is organized
  • 1. Introduction of Data Visualization
    • 1.1 What is data visualization?
    • 1.2 Why does visualization matter?
  • 2. Tricks in Visualization
    • 2.1 Choose Appropriate Chart
    • 2.2 Features of Charts
      • 2.2.1 Table
      • 2.2.2 Column Chart
      • 2.2.3 Line Chart
      • 2.2.4 Pie Chart
      • 2.2.5 Scatter Chart
      • 2.2.6 Map Chart
    • 2.3 Misused Graph
    • 2.4 Tips in Visualization
  • 3. Matplotlib
    • 3.1 Basic Concepts
    • 3.2 Line Chart
    • 3.3 Area Chart
    • 3.4 Column Chart
    • 3.5 Histogram Chart
    • 3.6 Scatter Chart
    • 3.7 Lollipop Chart
    • 3.8 Pie Chart
    • 3.9 Venn Chart
    • 3.10 Waffle Chart
    • 3.11 Animation
  • 4. Seaborn
    • 4.1 Trends
    • 4.2 Ranking
      • 4.2.1 Barplot
      • 4.2.2 Boxplot
    • 4.3 Composition
      • 4.3.1 Stacked Chart
    • 4.4 Correlation
      • 4.4.1 Scatter Plot
      • 4.4.2 Linear Relationship
      • 4.4.3 Heatmap
      • 4.4.4 Pairplot
    • 4.5 Distribution
      • 4.5.1 Boxplot
      • 4.5.2 Violin plot
      • 4.5.3 Histogram plot
      • 4.5.4 Density plot
      • 4.5.5 Joint plot
  • 5. Bokeh
    • 5.1 Basic Plotting
    • 5.2 Data Sources
    • 5.3 Annotations
    • 5.4 Categorical Data
    • 5.5 Presentation and Layouts
    • 5.6 Linking and Interactions
    • 5.7 Network Graph
    • 5.8 Widgets
  • 6. Plotly
    • 6.1 Fundamental Concepts
      • 6.1.1 Plotly Express
      • 6.1.2 Plotly Graph Objects
    • 6.2 Advanced Charts
      • 6.2.1 Advanced Scatter Chart
      • 6.2.2 Advanced Bar Chart
      • 6.2.3 Advanced Pie Chart
      • 6.2.4 Advanced Heatmap
      • 6.2.5 Sankey Chart
      • 6.2.6 Tables
    • 6.3 Statistical Charts
      • 6.3.1 Common Statistical Charts
      • 6.3.2 Dendrograms
      • 6.3.3 Radar Chart
      • 6.3.4 Polar Chart
      • 6.3.5 Streamline Chart
    • 6.4 Financial Charts
      • 6.4.1 Funnel Chart
      • 6.4.2 Candlestick Chart
      • 6.4.3 Waterfall Chart
  • Support
    • Donation
Powered by GitBook
On this page
  • 1. Quick start
  • 2. Simple plot
  • 3. Smart plot
  • 4. Facet plot

Was this helpful?

  1. 4. Seaborn

4.1 Trends

Previous4. SeabornNext4.2 Ranking

Last updated 4 years ago

Was this helpful?

1. Quick start

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt​
import seaborn as  sns
sns.set(rc={'figure.figsize':(12,6)},font_scale=1.3, style='whitegrid',palette ='muted')

You can set aesthetic parameters in one step by sns.set()

  • font_scale: Separate scaling factor to independently scale the size of the font elements.

  • style: dict, None, or one of {darkgrid, whitegrid, dark, white, ticks}

  • palette: Color palette, see ​

You can set aesthetic parameters in one step by sns.set()

  • font_scale: Separate scaling factor to independently scale the size of the font elements.

  • style: dict, None, or one of {darkgrid, whitegrid, dark, white, ticks}

  • palette: Color palette, see ​

2. Simple plot

Firstly, let us create a simple dataset. The head of the dataset looks as below:

# create a dataset
index = pd.date_range("1 1 2012", periods=100, freq="m", name="date")
data = np.random.randn(100, 4).cumsum(axis=0)
df = pd.DataFrame(data, index, ["a", "b", "c", "d"])

Then, we can plot a very simple line chart. Since we've already set all the aesthetic parameters at the beginning, here the code is pretty short and clean.

sns.lineplot(data=df)

We can not only plot a full dataset, but also pieces of the dataset. For example, I will truncate two lists of data from the original dataset. One is in a different period, the other is in the same period. Then we put them together by plt.subplots()‌

  • a and b are different lines in different periods.

  • c and c are different lines in the same period.

list1 = [df.loc[:"2015", "a"], df.loc["2015":, "b"]]
list2 = [df.loc[:"2015", "c"], df.loc[:"2015", "d"]]

​f,axes  = plt.subplots(2,1)      # create a subplot, 2 row and 1 column  
sns.lineplot(data=list1, ax=axes[0])   # plot list 1  
sns.lineplot(data=list2, ax=axes[1])   # plot list 2

3. Smart plot

fmri = sns.load_dataset("fmri")                 # import embedded dataset 
sns.lineplot(x="timepoint", y="signal",  data=fmri)        # line plot

The relationship between x and y can be shown for different subsets of the data using the hue, size, and style parameters. These parameters control what visual semantics are used to identify the different subsets.

  • hue: Grouping variable that will produce lines with different colors. Can be either categorical or numeric, although color mapping will behave differently in the latter case.

  • size: Grouping variable that will produce lines with different widths. Can be either categorical or numeric, although size mapping will behave differently in the latter case.

  • style: Grouping variable that will produce lines with different dashes and/or markers. Can have a numeric type but will always be treated as categorical.

‌

For example, seaborn groups variables in the "fmri" dataset and show the groups with different colors, and use color and line dashing to represent two different grouping variables.

# Plot the responses for different events and regions
sns.lineplot(x="timepoint", y="signal", data=fmri, hue="region", style = "event")

4. Facet plot

The chart above is quite complicated and not user-friendly. People need to read slowly and carefully to get the information. In this case, we need to consider another way to deliver the same information but more clear.‌

sns.relplot(x="timepoint", y="signal", data=fmri,
                col="region", hue="event", style="event", kind="line")

More complex datasets will have multiple measurements for the same value of the x variable. The default behavior in seaborn is to aggregate the multiple measurements at each x value by plotting the mean and the 95% confidence interval ( grey area) around the mean. Let us take the "" dataset for example.

​is a useful approach is to draw multiple instances of the same plot on different subsets of the dataset. It allows a viewer to quickly extract a large amount of information about complex data.‌

We can Use to combine and ​

color_palette()
color_palette()
fmri
FacetGrid
relplot()
lineplot()
FacetGrid