How To Create A Multiline Plot Using Seaborn?

Data visualization is a crucial component of data analysis, and plotting is one of the best ways to visualize data. The Python data visualization package Seaborn offers a high-level interface for making visually appealing and educational statistics visuals. The multiline plot, which lets you see numerous lines on one plot and makes it simple to compare and contrast various data sets, is one of Seaborn’s most helpful visualizations.

In this post, we will explore How To Create A Multiline Plot Using Seaborn using examples and step-by-step directions.

How To Create A Multiline Plot Using Seaborn?

  • What is a Multiline Plot?
  • Visualizing Multiline Plot Using Seaborn lineplot()
  • Visualizing Seaborn Multiline Plot with Hue
  • Customizing Seaborn Multiline Plot

What is a Multiline Plot?

A multiline plot is a kind of plot where several lines are displayed on a single graph, each of which represents a distinct category or group of data. Plots of this kind are helpful for contrasting and comparing various data sets, spotting patterns and trends, and illustrating how variables relate to one another. In order to examine and compare data across time, multiline plots are frequently used in finance, economics, and scientific research.

Visualizing Multiline Plot Using Seaborn lineplot()

Visualizing multiline plots using Seaborn can be achieved by first preparing your data in the appropriate format and then using Seaborn’s lineplot() function to create the visualization. The very first and important step is to import the necessary libraries and then proceed with visualizing the loaded dataset. The implementation is shown below in an example with a random dataset. We can specify the x-axis variable, y-axis variable, and any additional parameters such as hue (for grouping) or style (for line styles).

Python
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'Year': [2010, 2011, 2012, 2013, 2014],
    'A': [50, 40, 30, 60, 10],
    'B': [10, 20, 25, 42, 12],
    'C': [20, 30, 40, 50, 60]
})

# plot multiple lines
sns.lineplot(data=df, x='Year', y='A', label='A')
sns.lineplot(data=df, x='Year', y='B', label='B')
sns.lineplot(data=df, x='Year', y='C', label='C')
plt.legend()
plt.show()

Output:


Visualizing Seaborn Multiline Plot with Hue

Creating a multiline plot with Seaborn and specifying the hue involves utilizing the hue parameter within the lineplot function to add another dimension to the visualization.

The code snippet demonstrated below, a multiline plot depicting FMRI dataset that contains observations from a functional magnetic resonance imaging (FMRI) study. It includes columns such as ‘timepoint’, representing different time points during the study, ‘signal’, indicating the recorded FMRI signal intensity, and ‘region’, specifying the brain region being observed.

The ci parameter is set to None, implying that no confidence intervals will be displayed on the plot.

Python
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('fmri')

sns.lineplot(data=data, x='timepoint', y='signal', hue='region',ci=None)
plt.xlabel('Timepoint')
plt.ylabel('Signal')
plt.title('FMRI Signal Over Time by Region')
plt.show()

Output:


Customizing Seaborn Multiline Plot

For customizing our plot appearance, Let’s visualize the multiline plot using Seaborn and customize the chart using matplotlib, with each line representing a different brain region. Several customization options are applied to enhance the appearance of the plot in the below code:

  • plt.xlabel(): Sets the label for the x-axis with custom font properties, including fontsize, fontweight, and color.
  • plt.ylabel(): Sets the label for the y-axis with similar custom font properties.
  • plt.title(): Sets the title of the plot with custom font properties, including fontsize, fontweight, and color.
  • Customizing Legend: The plt.legend() function customizes the legend, setting the title to ‘Brain Region’ with a specific title fontsize and positioning it in the upper-left corner.
  • Customizing Grid: The plt.grid() function enables gridlines on the plot with a custom linestyle, transparency, and color.

Python
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('fmri')
sns.lineplot(data=data, x='timepoint', y='signal', hue='region', ci=None)

# Customize plot appearance
plt.xlabel('Timepoint', fontsize=14, fontweight='bold', color='blue')  # Set x-axis label with custom font properties
plt.ylabel('Signal Intensity', fontsize=14, fontweight='bold', color='blue')  # Set y-axis label with custom font properties
plt.title('FMRI Signal Over Time by Region', fontsize=16, fontweight='bold', color='green')  # Set plot title with custom font properties

# Customize legend
plt.legend(title='Brain Region', title_fontsize='12', loc='upper left')  # Customize legend title and position

# Customize grid
plt.grid(True, linestyle='--', alpha=0.5, color='gray')  # Enable grid with custom linestyle, transparency, and color
plt.show()

Output:


Conclusion

Creating multiline plots using Seaborn is an effective way to visualize trends and patterns in your data. By following the steps outlined above and utilizing Seaborn’s intuitive interface, you can easily create informative and visually appealing multiline plots for your data analysis tasks. Experiment with different parameters and customization options to tailor the plots to your specific needs and effectively communicate your findings.



Contact Us