Data Visualization Matplotlib Seaborn
Data Visualization Matplotlib Seaborn
Seaborn
• Principles of Data Visualization and Plotting
Techniques
• Karan Bajaj
Principles of Data Visualization
• • Clarity: Visualizations should be easy to read
and interpret.
• • Relevance: Only present what’s necessary.
• • Consistency: Use consistent formatting and
scales.
• • Context: Add context with titles, axis labels,
and legends.
• • Accuracy: Ensure no distortion of the data.
What is Matplotlib?
• • A powerful, flexible Python library for
creating static, animated, and interactive
visualizations.
• • Works well with NumPy arrays and Pandas
DataFrames.
• • Ideal for custom and complex visualizations.
Creating Plots with Matplotlib
• • Line Plot: Use for trends over time or
continuous data.
• • Bar Plot: Use for categorical comparisons.
• • Histogram: Use for distribution of
continuous variables.
• • Scatter Plot: Use for relationships between
two continuous variables.
Example: Line Plot in Matplotlib
• ```python
• import matplotlib.pyplot as plt
• x = [1, 2, 3, 4, 5]
• y = [2, 4, 6, 8, 10]
• plt.plot(x, y)
• plt.title('Simple Line Plot')
• plt.xlabel('X Axis')
• plt.ylabel('Y Axis')
• plt.show()
• ```
Example: Bar Plot in Matplotlib
• ```python
• import matplotlib.pyplot as plt
• categories = ['A', 'B', 'C', 'D', 'E']
• values = [3, 7, 2, 5, 4]
• plt.bar(categories, values)
• plt.title('Simple Bar Plot')
• plt.xlabel('Categories')
• plt.ylabel('Values')
• plt.show()
• ```
Example: Scatter Plot in Matplotlib
• ```python
• import matplotlib.pyplot as plt
• x = [1, 2, 3, 4, 5]
• y = [2, 4, 6, 8, 10]
• plt.scatter(x, y)
• plt.title('Simple Scatter Plot')
• plt.xlabel('X Axis')
• plt.ylabel('Y Axis')
• plt.show()
• ```
Example: Histogram in Matplotlib
• ```python
• import numpy as np
• import matplotlib.pyplot as plt
• data = np.random.randn(1000)
• plt.hist(data, bins=30)
• plt.title('Histogram')
• plt.xlabel('Value')
• plt.ylabel('Frequency')
• plt.show()
• ```
Plot Type Best For Example
Line Plot Trends over time Temperature changes, stock market trends
Bar Plot Categorical comparisons Population by country, product sales
Histogram Data distribution Age distribution, exam scores
Scatter Plot Relationship between variables Height vs. weight, ad spending
vs. sales
Difference Between Matplotlib and Seaborn
Matplotlib and Seaborn are both Python libraries for data visualization, but they differ in functionality,
aesthetics, and ease of use.