Data Visualization in Python: A Beginner's Guide

Table of contents
- π Introduction
- 1οΈβ£ Why is Data Visualization Important?
- 2οΈβ£ Top Python Libraries for Data Visualization
- 3οΈβ£ Getting Started with Matplotlib
- 4οΈβ£ Data Visualization with Seaborn
- 5οΈβ£ Interactive Visualization with Plotly
- 6οΈβ£ Best Practices for Effective Data Visualization
- Types of Data Visualization Charts: From Basic to Advanced
- In this guide, weβll explore the different types of data visualizations and how to create them using Python libraries like Matplotlib, Seaborn, and Plotly.
- 1. Bar Charts:- Comparing Categories
- π When to Use
- π Example Code (Matplotlib)
- π 2. Line Charts:- Showing Trends Over Time
- π When to Use
- π Example Code (Matplotlib)
- 3οΈβ£ Pie Charts β Displaying Proportions
- π When to Use
- π Example Code (Matplotlib)
- 4οΈβ£ Scatter Plots β Showing Relationships Between Variables
- 5οΈβ£ Histograms β Understanding Data Distributions
- Advanced Charts for Data Visualization

π Introduction
Data visualization is one of the most essential skills in data science, helping us to analyze and interpret complex datasets effectively. Python provides several powerful libraries for creating stunning and informative visualizations.
π Learn More with TechGyan! Subscribe to TechGyan YouTube Channel for in-depth Python tutorials, coding insights, and hands-on projects. Don't miss out on the latest tech updates!
In this blog, youβll learn: β
Why data visualization is important
β
Top Python libraries for visualization
β
Hands-on examples with Matplotlib, Seaborn, and Plotly
Letβs get started! π
1. Univariate Plots
A univariate plot is a type of graph that helps us understand one variable at a time. "Uni" means one, so we only analyze a single feature.
Examples of univariate plots:
Histogram - Shows how data is distributed.
Box Plot - Shows the median, range, and outliers.
Line Chart - Displays trends over time.
For example, if we analyze students' heights, we can use a histogram to see how many students fall into different height ranges.
2. Multivariate Plots
A multivariate plot is used when we want to study relationships between two or more variables.
Examples of multivariate plots:
Scatter Plot - Shows the relationship between two variables.
Heatmap - Shows correlations using color intensity.
Pair Plot - Displays relationships between multiple variables in one view.
For example, if we analyze the relationship between students' height and weight, a scatter plot can show whether taller students tend to be heavier.
3. Training Data & Test Data
When we build a machine learning model, we divide the data into two parts:
Training Data - This is used to train the model so it can learn patterns.
Test Data - This is used to check how well the model performs on unseen data.
For example, if we train a model to predict student grades based on their study hours, we use training data for learning and test data to check accuracy.
4. Performance Measures
After training a model, we need to measure how good it is. Some common performance measures are:
Accuracy - The percentage of correct predictions.
Precision & Recall - Used in classification problems to check correctness.
Mean Squared Error (MSE) - Used in regression to measure errors.
For example, if our model predicts student grades, we can compare its predictions with actual grades and measure accuracy.
1οΈβ£ Why is Data Visualization Important?
πΉ Helps in understanding trends and patterns in data.
πΉ Makes it easier to communicate insights.
πΉ Helps in decision-making based on data-driven analysis.
πΉ Useful in machine learning for feature selection and analysis.
2οΈβ£ Top Python Libraries for Data Visualization
Python offers multiple libraries for visualization, each serving different purposes:
Library | Best For |
Matplotlib | Basic plots and customization |
Seaborn | Statistical data visualization |
Plotly | Interactive and dynamic plots |
Pandas Visualization | Quick plotting from DataFrames |
Bokeh | High-performance interactive plots |
ggplot (Plotnine) | Grammar of graphics-style plotting |
3οΈβ£ Getting Started with Matplotlib
Matplotlib is the most fundamental visualization library in Python. It allows you to create simple static plots.
π Install Matplotlib
pip install matplotlib
π Example: Creating a Basic Line Chart
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
plt.plot(x, y, marker='o', linestyle='-', color='b', label='Data')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Basic Line Chart")
plt.legend()
plt.show()
4οΈβ£ Data Visualization with Seaborn
Seaborn is built on top of Matplotlib and provides a more aesthetically pleasing interface for statistical graphics.
π Install Seaborn
pip install seaborn
π Example: Creating a Histogram
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Sample data
data = pd.DataFrame({"values": [12, 15, 20, 25, 30, 30, 35, 40, 42, 50]})
sns.histplot(data["values"], bins=5, kde=True, color='g')
plt.title("Histogram Example by techGyan")
plt.show()
5οΈβ£ Interactive Visualization with Plotly
If you want interactive charts, Plotly is the best choice.
π Install Plotly
pip install plotly
π Example: Creating an Interactive Bar Chart
import plotly.express as px
import pandas as pd
# Sample Data
data = pd.DataFrame({
"Category": ["A", "B", "C", "D"],
"Values": [10, 25, 40, 30]
})
fig = px.bar(data, x='Category', y='Values', title="Interactive Bar Chart by techGyan", color='Category')
fig.show()
6οΈβ£ Best Practices for Effective Data Visualization
βοΈ Choose the right chart type (bar, line, scatter, histogram, etc.).
βοΈ Use labels, legends, and titles for clarity.
βοΈ Keep the design simple and clean (avoid too much clutter).
βοΈ Use color contrast effectively for better readability.
βοΈ Make use of interactive elements when necessary.
Types of Data Visualization Charts: From Basic to Advanced
In this guide, weβll explore the different types of data visualizations and how to create them using Python libraries like Matplotlib, Seaborn, and Plotly.
Simple Charts for Data Visualization
These are the basic charts youβll use when starting with data visualization. They are easy to create, simple to understand, and help you quickly analyze your data. We use Python libraries like Matplotlib and Seaborn to make these charts.
1. Bar Charts:- Comparing Categories
A bar chart is used to compare different categories using rectangular bars.
π When to Use
β Comparing sales across different products
β Showing population distribution by country
β Comparing monthly revenue trends
π Example Code (Matplotlib)
import matplotlib.pyplot as plt
categories = ["A", "B", "C", "D"]
values = [10, 25, 40, 30]
plt.bar(categories, values, color=['blue', 'orange', 'green', 'red'])
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart")
plt.show()
π 2. Line Charts:- Showing Trends Over Time
A line chart is useful for displaying data over time.
π When to Use
β Analyzing stock market trends
β Tracking website traffic over months
β Showing temperature changes over time
π Example Code (Matplotlib)
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y, marker='o', linestyle='-', color='blue')
plt.xlabel("Time")
plt.ylabel("Values")
plt.title("Line Chart")
plt.show()
3οΈβ£ Pie Charts β Displaying Proportions
A pie chart is best for representing percentage distributions.
π When to Use
β Market share of different companies
β Percentage of expenses in a budget
β Customer segmentation data
π Example Code (Matplotlib)
import matplotlib.pyplot as plt
# Data for the pie chart
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [30, 25, 20, 25]
# Create the pie chart
plt.figure(figsize=(6,6))
plt.pie(values, labels=categories, autopct='%1.1f%%', colors=['blue', 'orange', 'green', 'red'])
plt.title("Pie Chart by TechGyan")
# Save as a JPG file
plt.savefig("pie_chart_techgyan.jpg", format='jpg')
plt.show()
4οΈβ£ Scatter Plots β Showing Relationships Between Variables
A scatter plot helps visualize relationships between two numerical variables.
π When to Use
β Examining correlation between age and income
β Identifying trends in customer purchases
β Analyzing height vs. weight distribution
π Example Code (Seaborn)
import matplotlib.pyplot as plt
# Data for scatter plot
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
# Create scatter plot
plt.scatter(x, y, color='purple')
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.title("Scatter Plot")
# Save as a JPG file
plt.savefig("scatter_plot.jpg", format='jpg')
# Show the plot
plt.show()
5οΈβ£ Histograms β Understanding Data Distributions
A histogram represents the distribution of numerical data by dividing it into bins.
π When to Use
β Analyzing exam scores distribution
β Checking income distribution in a city
β Visualizing age groups in a population
π Example Code (Seaborn)
import matplotlib.pyplot as plt
# Data for histogram
data = [10, 20, 20, 30, 40, 40, 40, 50]
# Create histogram
plt.figure(figsize=(6,6))
plt.hist(data, bins=4, color='gray', edgecolor='black')
plt.xlabel("Value Ranges")
plt.ylabel("Frequency")
plt.title("Histogram by techgyan")
# Save as a JPG file
plt.savefig("histogram.jpg", format='jpg')
# Show the plot
plt.show()
Advanced Charts for Data Visualization
After learning basic charts, it's time to explore advanced charts! These charts help you:
β
Dive deeper into your data
β
Find detailed insights
β
Visualize multiple variables
β
Uncover hidden patterns and relationships
Advanced charts provide a more comprehensive analysis, making it easier to spot trends, correlations, and anomalies in your data. π
6οΈβ£ Heatmaps β Visualizing Correlations
A heatmap is useful for displaying relationships between multiple variables.
π When to Use
β Showing correlation between stock prices
β Analyzing website user activity by hour
β Visualizing temperature variations by region
π Example Code (Seaborn)
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# Sample Data (4x4 Matrix)
data = np.array([[10, 20, 30, 40],
[20, 30, 40, 50],
[30, 40, 50, 60],
[40, 50, 60, 70]])
# Create Heatmap
plt.figure(figsize=(6, 5))
sns.heatmap(data, annot=True, cmap="coolwarm", linewidths=0.5, fmt="d")
# Labels & Title
plt.title("Heatmap Example")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
# Save as Image
heatmap_path = "/mnt/data/heatmap_example.jpg"
plt.savefig(heatmap_path, format="jpg", dpi=300)
plt.show()
heatmap_path
π― Conclusion
Different types of data visualizations serve different purposes. Hereβs a quick summary:
Visualization Type | Best For |
Bar Chart | Comparing categories |
Line Chart | Showing trends over time |
Pie Chart | Displaying proportions |
Scatter Plot | Finding relationships between variables |
Histogram | Understanding data distribution |
Heatmap | Analyzing correlations visually |
By choosing the right visualization type, you can make your data more insightful and meaningful! π
π₯ Want to master data visualization? Watch detailed tutorials on the TechGyan YouTube Channel and boost your Python skills!
Subscribe to my newsletter
Read articles from techGyan : smart tech study directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

techGyan : smart tech study
techGyan : smart tech study
TechGyan is a YouTube channel dedicated to providing high-quality technical and coding-related content. The channel mainly focuses on Android development, along with other programming tutorials and tech insights to help learners enhance their skills. What TechGyan Offers? β Android Development Tutorials π± β Programming & Coding Lessons π» β Tech Guides & Tips π οΈ β Problem-Solving & Debugging Help π β Latest Trends in Technology π TechGyan aims to educate and inspire developers by delivering clear, well-structured, and practical coding knowledge for beginners and advanced learners.