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
Data visualization with different Charts in Python?
Python provides various easy-to-use libraries for data visualization that work efficiently with both small and large datasets. This tutorial demonstrates how to create different types of charts using the same dataset to analyze population data from multiple perspectives.
Popular Python Visualization Libraries
The most commonly used Python libraries for data visualizations are:
Matplotlib − The foundational plotting library
Pandas − Built-in plotting capabilities for DataFrames
Plotly − Interactive web-based visualizations
Seaborn − Statistical plotting with beautiful defaults
Sample Dataset
We will analyze India's population data across different demographic variants to demonstrate various chart types:
| Country or Area | Year(s) | Variant | Value |
|---|---|---|---|
| India | 2019 | Medium | 1368737.513 |
| India | 2019 | High | 1378419.072 |
| India | 2019 | Low | 1359043.965 |
| India | 2019 | Constant fertility | 1373707.838 |
| India | 2019 | Instant replacement | 1366687.871 |
Line Charts
Line graphs show relationships and trends over time by connecting data points with lines ?
import matplotlib.pyplot as plt
years = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
india_population = [1173108018, 1189172906, 1205073612, 1220800359, 1266344631,
1309053980, 1324171354, 1339180127, 1354051854, 1368737513]
plt.figure(figsize=(10, 6))
plt.plot(years, india_population, marker='o', linewidth=2)
plt.title('India Population Growth Over Time')
plt.xlabel('Year')
plt.ylabel('Population')
plt.grid(True, alpha=0.3)
plt.show()
Scatter Plots
Scatter plots display individual data points without connecting lines, useful for showing correlations ?
import matplotlib.pyplot as plt
years = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
india_population = [1173108018, 1189172906, 1205073612, 1220800359, 1266344631,
1309053980, 1324171354, 1339180127, 1354051854, 1368737513]
plt.figure(figsize=(10, 6))
plt.scatter(years, india_population, s=100, color='red', alpha=0.7)
plt.title('India Population Data Points')
plt.xlabel('Year')
plt.ylabel('Population')
plt.grid(True, alpha=0.3)
plt.show()
Histograms
Histograms display the distribution of numerical data by grouping values into bins ?
import pandas as pd
import matplotlib.pyplot as plt
data = [
['India', 2019, 'Medium', 1368737.513],
['India', 2019, 'High', 1378419.072],
['India', 2019, 'Low', 1359043.965],
['India', 2019, 'Constant fertility', 1373707.838],
['India', 2019, 'Instant replacement', 1366687.871],
['India', 2019, 'Zero migration', 1370868.782],
['India', 2019, 'Constant mortality', 1366282.778],
['India', 2019, 'No change', 1371221.64],
['India', 2019, 'Momentum', 1367400.614]
]
df = pd.DataFrame(data, columns=['Country', 'Year', 'Variant', 'Value'])
plt.figure(figsize=(10, 6))
plt.hist(df['Value'], bins=5, color='skyblue', edgecolor='black', alpha=0.7)
plt.title('Distribution of Population Variants')
plt.xlabel('Population Value')
plt.ylabel('Frequency')
plt.grid(True, alpha=0.3)
plt.show()
Bar Charts
Bar charts compare different categories using rectangular bars ?
import pandas as pd
import matplotlib.pyplot as plt
data = [
['India', 2019, 'Medium', 1368737.513],
['India', 2019, 'High', 1378419.072],
['India', 2019, 'Low', 1359043.965],
['India', 2019, 'Constant fertility', 1373707.838],
['India', 2019, 'Instant replacement', 1366687.871]
]
df = pd.DataFrame(data, columns=['Country', 'Year', 'Variant', 'Value'])
plt.figure(figsize=(12, 6))
plt.bar(df['Variant'], df['Value'], color=['blue', 'green', 'red', 'orange', 'purple'])
plt.title('India Population by Different Variants (2019)')
plt.xlabel('Population Variant')
plt.ylabel('Population Value')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Pie Charts
Pie charts show proportional relationships between different categories ?
import matplotlib.pyplot as plt
variants = ['Medium', 'High', 'Low', 'Constant fertility', 'Instant replacement']
values = [1368737.513, 1378419.072, 1359043.965, 1373707.838, 1366687.871]
plt.figure(figsize=(10, 8))
plt.pie(values, labels=variants, autopct='%1.1f%%', startangle=90)
plt.title('India Population Distribution by Variants (2019)')
plt.axis('equal')
plt.show()
Chart Comparison
| Chart Type | Best For | Use Cases |
|---|---|---|
| Line Chart | Trends over time | Time series data, continuous data |
| Scatter Plot | Correlations | Relationship between variables |
| Histogram | Data distribution | Frequency analysis, statistical data |
| Bar Chart | Category comparison | Discrete categories, rankings |
| Pie Chart | Proportions | Parts of a whole, percentages |
Conclusion
Different chart types serve different analytical purposes. Line charts excel at showing trends, bar charts compare categories, and histograms reveal data distributions. Choose the appropriate visualization based on your data type and the insights you want to communicate.
