In this article Histogram in Python Learn data visualization using Python’s Matplotlib — create Histograms, Pie Charts, and Sine & Cosine curves with code examples.
Data Visualization in Python: Histogram, Pie Chart, and Sine–Cosine Curves
Data visualization helps to understand data trends and relationships by representing them graphically.
In Python, we commonly use the Matplotlib library for 2D plotting.
Install (if not already):
pip install matplotlib numpy
Then import the modules:
import matplotlib.pyplot as plt
import numpy as np
-
Histogram
Definition:
A histogram shows how data is distributed across different intervals (called bins).
It’s useful for visualizing the frequency distribution of numerical data.
Example Code:
import matplotlib.pyplot as plt
import numpy as np
# Generate random data
data = np.random.randn(1000) # 1000 random values (Normal Distribution)
plt.hist(data, bins=20, color=’skyblue’, edgecolor=’black’)
plt.title(“Histogram of Random Data”)
plt.xlabel(“Data Range”)
plt.ylabel(“Frequency”)
plt.grid(axis=’y’, alpha=0.75)
plt.show()
Output Explanation:
- The X-axis shows data ranges (bins).
- The Y-axis shows how many data points fall in each bin.
- It helps detect patterns, outliers, or spread of data.
-
Pie Chart
Definition:
A Pie Chart displays data as a circular graph divided into slices, each representing a category’s proportion.
Example Code:
import matplotlib.pyplot as plt
# Data for pie chart
labels = [‘Python’, ‘Java’, ‘C++’, ‘JavaScript’]
sizes = [40, 25, 20, 15]
colors = [‘gold’, ‘lightcoral’, ‘lightskyblue’, ‘lightgreen’]
explode = (0.1, 0, 0, 0) # explode the 1st slice (Python)
plt.pie(sizes, labels=labels, colors=colors, autopct=’%1.1f%%’,
startangle=90, shadow=True, explode=explode)
plt.title(“Programming Language Popularity”)
plt.show()
Output Explanation:
- Each slice shows the percentage share of each programming language.
- The explode parameter highlights a slice.
- The autopct displays percentages inside the chart.
-
Sine and Cosine Curves
Definition:
The Sine and Cosine curves are basic trigonometric functions used to represent periodic (wave-like) data.
These functions are essential in mathematics, physics, and engineering.
Example Code:
import matplotlib.pyplot as plt
import numpy as np
# Create data points
x = np.linspace(0, 2 * np.pi, 100) # 0 to 2π
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label=’Sine Wave’, color=’blue’, linewidth=2)
plt.plot(x, y2, label=’Cosine Wave’, color=’red’, linestyle=’–‘, linewidth=2)
plt.title(“Sine and Cosine Curves”)
plt.xlabel(“Angle (radians)”)
plt.ylabel(“Amplitude”)
plt.legend()
plt.grid(True)
plt.show()
Output Explanation:
- The Sine wave starts at 0.
- The Cosine wave starts at 1.
- Both waves repeat every 2π2\pi2π radians.
Summary Table
Visualization | Purpose | Example Use Case |
Histogram | Shows data frequency distribution | Marks obtained by students |
Pie Chart | Shows percentage or proportional data | Market share, Budget allocation |
Sine & Cosine Curves | Show periodic or wave-like patterns | Sound waves, Signal processing |
Key Points
- Use plt.title(), plt.xlabel(), and plt.ylabel() to make plots readable.
- Always call plt.show() to display the figure.
- Combine multiple plots with plt.subplot() if required.
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
https://defineinfoloop.blogspot.com/?m=1