Do you want to learn how to create visually stunning and informative line plots that will captivate your audience by providing most apt information? Do you have the requirement of creating multiple line plots in the same figure representing sales of different products across different months in a year? Are you looking for a takeaway Python code with Seaborn library for creating line plots? If yes, you are in the right place.
In this blog post, we’ll explore how to create multiple line plots with Seaborn, a powerful data visualization library built on top of Matplotlib. I will also show how to add markers to the line plots to make the data points more visible, and how to add a legend to the plot to help readers understand the meaning of each line.
The Python code provided in this section creates multiple line plots using the Seaborn and Matplotlib libraries to visualize sales data for three different products across all 12 months of the year. Here are some key points to note:
sns.lineplot()
functions. Each lineplot() function has marker and color for different markers & colors. Also, there is an attribute called label which is assigned different labels. This would show up in legend. Each line plot as x and y assigned different labels in the dataframe. sns.despine()
function. If you want the spines, comment the code.Finally, the plot is displayed using the plt.show()
function.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Sample data
data = {
'Months': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'Product A': [1000, 1500, 1200, 2000, 1800, 2200, 1500, 1800, 2000, 2300, 1900, 2100],
'Product B': [800, 1200, 1500, 1800, 2000, 2300, 1600, 1900, 2200, 2500, 1800, 2000],
'Product C': [1200, 1300, 1100, 1400, 1600, 1900, 1300, 1500, 1700, 1900, 1500, 1700]
}
df = pd.DataFrame(data)
# Set the style and font sizes
sns.set_style('ticks')
plt.rcParams.update({'font.size': 8})
# Create a Seaborn line plot with different markers for each product
sns.lineplot(x='Months', y='Product A', data=df, marker='o', color='navy', label='Product A')
sns.lineplot(x='Months', y='Product B', data=df, marker='s', color='teal', label='Product B')
sns.lineplot(x='Months', y='Product C', data=df, marker='D', color='salmon', label='Product C')
# Set plot title and axes labels
plt.title('Sales Across Different Products')
plt.xlabel('Months')
plt.ylabel('Sales ($100K)')
# Add a legend
plt.legend(loc='lower right')
# Add a grid
plt.grid(True)
# Remove the top and right spines
sns.despine()
# Show the plot
plt.show()
Here is how the plot would look like for the above code:
In order to read data from CSV, all you need to do is use the following code instead of using “data” to create dataframe as shown in the previous code.
# Read data from CSV file
df = pd.read_csv('path/to/file.csv')
The following data was read from CSV file.
In this blog post, we explored how to create multiple line plots in the same figure with markers and legend using Seaborn library in Python. We added visual appeal to the plot by customizing the line colors, adding markers, and grid lines. The code snippets provided in this blog post can be used as a template to create similar single line or multiple line plots for other datasets. Seaborn’s ease of use and customizability make it an excellent tool for creating beautiful and informative visualizations. Whether you are a beginner or an experienced data analyst, Seaborn is a valuable library to have in your toolkit.
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…