Python Data Visualization Examples: Master Charts with Code

Python, renowned for its simplicity and readability, is not only a powerful language for data manipulation but also excels in data visualization. It offers several libraries like Matplotlib, Seaborn, and Plotly that make creating insightful visualizations a breeze.

Python 3 and Data Visualization
Python 3 and Data Visualization

In this article, we'll delve into Python data visualization, exploring various examples to help you understand how to effectively communicate data insights using this versatile language.

Matplotlib Cheat Sheet for Data Analysts (Chapter 6/15) | Python for Data Analysts
Matplotlib Cheat Sheet for Data Analysts (Chapter 6/15) | Python for Data Analysts

Static Visualizations with Matplotlib

Matplotlib, Python's oldest visualization library, is great for creating static, publication-quality figures.

GitHub - vega/altair: Declarative visualization library for Python
GitHub - vega/altair: Declarative visualization library for Python

Let's dive into two common chart types: bar charts and scatter plots.

Bar Charts with Matplotlib

Python Data Visualization Libraries Explained (Matplotlib, Seaborn, Plotly)
Python Data Visualization Libraries Explained (Matplotlib, Seaborn, Plotly)

Bar charts are excellent for comparing categorical data. Here's a simple example of a bar chart using Matplotlib:

import matplotlib.pyplot as plt
import numpy as np

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
companies = [np.random.rand() for _ in range(6)]

plt.bar(months, companies)
plt.title('Monthly Sales')
plt.xlabel('Months')
plt.ylabel('Sales')
plt.show()

Scatter Plots with Matplotlib

the visualizing data with python
the visualizing data with python

Scatter plots are useful for exploring relationships between two sets of data. Let's create a simple scatter plot:

numbers = np.random.randint(1, 100, (100, 2))

plt.scatter(numbers[:, 0], numbers[:, 1])
plt.title('Random Scatter Plot')
plt.xlabel('Dataset 1')
plt.ylabel('Dataset 2')
plt.show()

Dynamic and Interactive Visualizations with Plotly

Data Analysis and Visualization Using Python
Data Analysis and Visualization Using Python

Plotly offers interactive visualizations that users can manipulate for deeper data exploration.

Let's create an animated line chart and an interactive 3D scatter plot.

Data Visualization with Python
Data Visualization with Python
Exploratory Data Analysis (EDA) Cheat Sheet (Chapter 8/15) | Python for Data Analysts
Exploratory Data Analysis (EDA) Cheat Sheet (Chapter 8/15) | Python for Data Analysts
Data Visualization in Python using Matplotlib
Data Visualization in Python using Matplotlib
Data Analysis and Visualization Using Python
Data Analysis and Visualization Using Python
Seaborn Cheat Sheet for Data Analysts (Chapter 7/15) | Python for Data Analysts
Seaborn Cheat Sheet for Data Analysts (Chapter 7/15) | Python for Data Analysts
Day 28: Matplotlib in Python 📊
Day 28: Matplotlib in Python 📊
Python For Data Analysis: A Complete Guide For Beginners
Python For Data Analysis: A Complete Guide For Beginners
Python Data Visualization with Matplotlib - Part 2 | Towards Data Science
Python Data Visualization with Matplotlib - Part 2 | Towards Data Science
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Professional Data Analysis and Visualization Using Python Pandas
Professional Data Analysis and Visualization Using Python Pandas
Choosing a Python Visualization Tool
Choosing a Python Visualization Tool
a book cover with the words python concept for data analyses on it and an image of a
a book cover with the words python concept for data analyses on it and an image of a
Data Visualization using Matplotlib
Data Visualization using Matplotlib
Spatial Data Science using Python
Spatial Data Science using Python
Python Data Types Explained Visually | Beginner Python Guide
Python Data Types Explained Visually | Beginner Python Guide
Data Analyst project using Python
Data Analyst project using Python
🌟 Introducing Dash 🌟
🌟 Introducing Dash 🌟
Python Graph Gallery
Python Graph Gallery
Python Complete Roadmap 🐍💻
Python Complete Roadmap 🐍💻
a screenshot of a computer screen with the text create dataset in red and black
a screenshot of a computer screen with the text create dataset in red and black

Animated Line Chart with Plotly

Animations can help illustrate changes over time. Here's an example of an animated line chart:

import plotly.graph_objects as go

fig = go.Figure()

for i in range(10):
    fig.add_trace(go.Scatter(x=[2*i, 2*i+1], y=[2*i, 2*i+1], mode='lines'))

fig.show()

Interactive 3D Scatter Plot with Plotly

Plotly's 3D plotting capabilities allow for data exploration from different angles. Here's an example:

fig = go.Figure(data=[go.Scatter3d(
    x=[1, 2, 3],
    y=[2, 3, 2],
    z=[3, 2, 1],
    mode='markers'
)])

fig.show()

Python's libraries for data visualization are vast and powerful. By employing Matplotlib for static visualizations and Plotly for interactive ones, you can effectively communicate complex data insights clearly and engagingly.

Related Articles

Python Selenium Code Examples Lambda Function Python Code Example Python Sample N Elements From List Python Sample Based On Probability Sample Python Jupyter Notebook Snowflake Python Code Example Python Svm Example Code Visual Studio Code Python Examples Sample Basic Python Script Python Sample N From List