A line plot displays information as a series of data points called 'markers' connected by straight line segments. It is often used to visualize a trend in data over time.
- Trend Analysis: Shows trends over time or other ordered sequences.
- Continuous Data: Best for continuous data with an inherent order.
- Comparative Analysis: Multiple lines can compare different datasets.
- Time Series Analysis: Tracking changes over time (e.g., stock prices, temperature changes).
- Performance Monitoring: Monitoring performance metrics over time (e.g., CPU usage).
- Scientific Data: Displaying experimental results or growth rates.
- Forecasting: Projecting future values based on historical trends.
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 14, 19, 25, 30]
plt.plot(x, y, marker='o')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Basic Line Plot')
plt.grid(True)
plt.show()
plt.plot(x, y, marker='o', linestyle='--', color='g', linewidth=2, markersize=10)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Customized Line Plot')
plt.xlim(0, 6)
plt.ylim(5, 35)
plt.grid(True)
plt.show()
import seaborn as sns
sns.lineplot(x=x, y=y, marker='o')
plt.title('Seaborn Line Plot')
plt.show()
sns.set(style='whitegrid')
sns.lineplot(x=x, y=y, marker='o')
plt.title('Customized Seaborn Line Plot')
plt.show()
A scatter plot uses dots to represent values for two different numeric variables. The position of each dot on the horizontal and vertical axis indicates values for an individual data point.
- Relationship Analysis: Shows relationships between two variables.
- Outliers: Helps identify outliers and anomalies.
- Clusters: Can show clustering of data points.
- Correlation Analysis: Assessing the relationship between two variables (e.g., height vs. weight).
- Outlier Detection: Identifying outliers in the data.
- Pattern Recognition: Recognizing patterns and clusters in the data.
plt.scatter(x, y, c='red', marker='x')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Scatter Plot')
plt.show()
sizes = [50, 100, 200, 300, 400]
colors = ['red', 'green', 'blue', 'orange', 'purple']
plt.scatter(x, y, s=sizes, c=colors, alpha=0.5, edgecolors='w', linewidth=2)
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Customized Scatter Plot')
plt.show()
sns.scatterplot(x=x, y=y, color='red')
plt.title('Seaborn Scatter Plot')
plt.show()
sns.scatterplot(x=x, y=y, size=sizes, hue=colors, palette='coolwarm', edgecolor='w', linewidth=2)
plt.title('Customized Seaborn Scatter Plot')
plt.show()
A bar plot represents categorical data with rectangular bars. Each bar's height or length is proportional to the values they represent.
- Categorical Data: Represents categorical data.
- Comparative Analysis: Compare different categories or groups.
- Distribution: Show the distribution of categorical data.
- Comparing Categories: Comparing values across different categories (e.g., sales by region).
- Frequency Distribution: Displaying the frequency distribution of categorical data.
- Survey Results: Visualizing survey results and responses.
categories = ['A', 'B', 'C', 'D', 'E']
values = [10, 20, 15, 25, 30]
plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.show()
plt.bar(categories, values, color=['red', 'blue', 'green', 'purple', 'orange'], edgecolor='black')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Customized Bar Plot')
plt.show()
sns.barplot(x=categories, y=values)
plt.title('Seaborn Bar Plot')
plt.show()
sns.barplot(x=categories, y=values, palette='viridis')
plt.title('Customized Seaborn Bar Plot')
plt.show()
To compare multiple series of data:
import numpy as np
# Sample data
categories = ['A', 'B', 'C', 'D', 'E']
values1 = [10, 20, 15, 25, 30]
values2 = [15, 18, 14, 22, 28]
# Create an array with the positions of the bars on the x-axis
bar_width = 0.4
bar_positions1 = np.arange(len(categories))
bar_positions2 = bar_positions1 + bar_width
# Create the bar plots
plt.bar(bar_positions1, values1, width=bar_width, color='blue', label='Series 1')
plt.bar(bar_positions2, values2, width=bar_width, color='green', label='Series 2')
# Adding labels and title
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Grouped Bar Plot')
plt.xticks(bar_positions1 + bar_width / 2, categories)
plt.legend()
plt.show()
By mastering line plots, scatter plots, and bar plots, you can effectively visualize trends, relationships, and comparisons in your data. These basic visualizations are foundational for more advanced data analysis and presentation.