Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Vertical Histogram in Python and Matplotlib
A vertical histogram is the default orientation in Matplotlib where bars extend upward from the x-axis. This tutorial shows how to create vertical histograms using plt.hist() with customizable figure settings.
Basic Vertical Histogram
The plt.hist() function creates vertical histograms by default. Here's a simple example ?
import matplotlib.pyplot as plt
# Sample data
data = [1, 2, 3, 1, 2, 3, 4, 1, 3, 4, 5]
plt.hist(data)
plt.title('Vertical Histogram')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()
Customizing Figure Properties
You can customize the figure size and layout using rcParams ?
import matplotlib.pyplot as plt
# Set figure properties
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = [1, 2, 3, 1, 2, 3, 4, 1, 3, 4, 5]
# Create vertical histogram (default orientation)
plt.hist(data, orientation="vertical")
plt.title('Custom Sized Vertical Histogram')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()
Enhanced Vertical Histogram
Add colors, transparency, and grid for better visualization ?
import matplotlib.pyplot as plt
data = [1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
plt.figure(figsize=(8, 5))
plt.hist(data, bins=5, color='skyblue', alpha=0.7, edgecolor='black')
plt.title('Enhanced Vertical Histogram')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.grid(axis='y', alpha=0.3)
plt.show()
Key Parameters
| Parameter | Description | Default |
|---|---|---|
orientation |
Histogram orientation | "vertical" |
bins |
Number of bins | 10 |
color |
Bar color | Blue |
alpha |
Transparency (0-1) | 1.0 |
Conclusion
Vertical histograms are the default in Matplotlib and perfect for displaying frequency distributions. Use plt.hist() with customization parameters like bins, color, and alpha for enhanced visualizations.
Advertisements
