# How to visualize data with seaborn's lineplot() function

Becca Weng - March 6th, 2023

This post will go over how to effectively visualize data using seaborn’s built-in lineplot() function. There are many parameters you can use. Check out the table of contents on the left if you’re interested in a particular argument or customization problem.

For this tutorial, we’re using a dataset on Steam game popularity. The original dataset can be found on Kaggle. In this case, we have subset only for games in the Counter-Strike series. There is one row of data per game, per month from mid-2012 to the end of 2021, depending on the game’s initial release date.

Line plots are a critical tool in articulating trends over time. And for many data professionals, analyzing trends is a critical part of everyday deliverables and larger projects. So let's get started.

## Import packages and load data

import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme()

## Basic line plot: sns.lineplot(data, x, y)

# Create basic line plot
basic_plt = sns.lineplot(data = df, x = "Year", y = "Peak Players")
basic_plt.set(title = 'Peak Counter-Strike Players Over Time')

Output:

Seaborn’s lineplot() function has three critical arguments to produce a basic line plot:

• data: variable, in this case df where your data is stored
• x: name of the column (i.e. “Year”) that stores the variable on the x-axis
• y: name of the column (i.e. “Peak Players”) that stores the variable on the y-axis

## Comparing groups: hue, legend, style, and markers

### Hue Example

Beyond your x and y variables, you may have other variables you want to compare. In this case, there are several Counter-Strike games in the dataset, and we want to see if the peak players are meaningfully different per game.

# Create plot using hue argument
hue_plot = sns.lineplot(data = df, x = "Year", y = "Peak Players", hue = "Name")
hue_plot.set(title = "Peak Players Over Time, Grouped by Game")

plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)

Output:

In the above plot, we used the hue argument to compare different Counter-Strike games. From the plot, we can see that Counter-Strike: Global Offensive, has significantly more peak players, so we’ll examine the outlier game first, and then the rest of the games.

### Hue and legend Example

# Subset just for Global Offensive
cs_global = df[df["Name"] == "Counter-Strike: Global Offensive"]

# Create line plot using hue, no legend specification
hue_plot2 = sns.lineplot(cs_global, x = "Year", y = "Peak Players", hue = "Month")
hue_plot2.set(title = "Peak Players Per Year, Grouped by Month")

plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)

Output:

In the above plot, notice that only even numbered months appear, this is because hue, when given a quantitative variable, will scale the color, and by default, can leave off some of the values. We'll use legend = "full" to correct this.

# Create line plot using hue, with full legend
hue_plot3 = sns.lineplot(cs_global, x = "Year", y = "Peak Players", hue = "Month", legend = "full")
hue_plot3.set(title = "Peak Players Per Year, Grouped by Month")

plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)

Output:

All 12 months appear in the graph now!

## BONUS: seaborn lineplots with Generative AI

In Einblick, we've implemented an AI agent, called Einblick Prompt, which can create data workflows from as little as one sentence. In the below canvas, we used generative AI to get the same charts we coded up manually above:

### Using Generative AI in Einblick

1. Open the canvas
2. Fork the canvas
3. Right-click anywhere in the canvas > Prompt
4. Type in: "Plot a lineplot of peak players by year, using seaborn. Only include Global Offensive, and color the lines by month. Include a legend."
5. Run the code in Einblick's data notebook immediately

Test out different graphs and prompts, and get results in seconds!

### Hue and style Example

Style changes the appearance of the lines. It can be used alone, or in conjunction with hue.

# Subset data to exclude Counter-Strike: Global Offensive
cs_subset = df[df["Name"] != "Counter-Strike: Global Offensive"]

# Create line plot using hue, style, and markers
style_plot = sns.lineplot(data = cs_subset, x = "Year", y = "Peak Players", hue = "Name", style = "Name")
style_plot.set(title = "Peak Players Per Year, Grouped by Game")

plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)

Output:

### Style, markers, and dashes Example

Once you have implemented the style argument, you can add more styling via markers and dashes to specify if you want to add in data points or dashed lines, respectively.

# Create line plot using hue, style, and markers
style_plot2 = sns.lineplot(data = cs_subset, x = "Year", y = "Peak Players", hue = "Name", style = "Name", markers = True, dashes = False)
style_plot2.set(title = "Peak Players Per Year, Grouped by Game")

plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)

Output:

Note that the plot now has markers (markers = True), but the lines are no longer dashed (dashes = False).

## Visualizing errors: err_style and errorbar

There are two main ways to change the appearance of error:

• err_style: changes the appearance of errors (err_style = "bars"), the default is "band"
• errorbar: changes the height of the errors, the default is ("ci", 95), which represents a 95% confidence interval

### errorbar Example

# Create line plot using hue, style, and markers
style_plot = sns.lineplot(data = cs_subset, x = "Year", y = "Peak Players", hue = "Name", style = "Name", markers = True, errorbar = ("se", 4))
style_plot.set(title = "Peak Players Per Year, Grouped by Game, with Error Band")

plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)

Output:

Note that the bands are a lot wider than before because we have set it to 4 times the standard error via errorbar = ("se", 4).

### err_style = "bars" Example

# Use stylied error bars
errbar_plot = sns.lineplot(data = cs_subset, x = "Year", y = "Peak Players", hue = "Name", style = "Name", errorbar = ("ci", 95), err_style = "bars")
errbar_plot.set(title = "Peak Players Per Year, Grouped by Game, with Error Bars")

plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)

Output:

For more arguments or examples, check out the seaborn documentation.

Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.

## Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

• All connectors
• Unlimited teammates
• All operators
###### Company
Website Data Collection