Advanced Graphs and Applications - Using Matplotlib
In this lesson, we'll explore various ways to represent data using histograms, scatter plots, pie charts, and subplots.
1. Histogram - Visualizing Data Distribution
A histogram is a useful graph for analyzing the distribution of data.
import matplotlib.pyplot as plt import numpy as np # Generate random data (normal distribution) data = np.random.randn(1000) # Draw a histogram plt.hist(data, bins=30, color='purple', alpha=0.7) # Graph settings plt.title("Histogram of Data Distribution") plt.xlabel("Value") plt.ylabel("Frequency") plt.show()
Key Concepts
-
bins=30: Divides the data into 30 bins (intervals) -
alpha=0.7: Adjusts the transparency of the graph (0.0: transparent, 1.0: opaque)
Using a histogram, you can analyze whether data is concentrated around specific values or follows a normal distribution.
2. Scatter Plot - Analyzing Relationships Between Data
Scatter plots are used to visually represent correlations between two variables.
import matplotlib.pyplot as plt import numpy as np # Generate random data x = np.random.rand(50) y = np.random.rand(50) # Draw a scatter plot plt.scatter(x, y, color='blue', alpha=0.5) # Graph settings plt.title("Scatter Plot") plt.xlabel("X Value") plt.ylabel("Y Value") plt.show()
Key Concepts
-
plt.scatter(x, y): Represents the relationship between x-axis and y-axis data in points -
alpha=0.5: Adjusts point transparency to easily see overlapping areas
Scatter plots are useful for checking correlations between variables or exploring outliers.
3. Pie Chart - Representing Proportions
Pie charts are used to visually represent the proportions of data.
import matplotlib.pyplot as plt labels = ["A", "B", "C", "D"] values = [30, 20, 40, 10] plt.pie(values, labels=labels, autopct="%1.1f%%", colors=['red', 'blue', 'green', 'orange']) plt.title("Pie Chart Representing Proportions") plt.show()
Key Concepts
-
labels: Specifies the name of each slice -
autopct="%1.1f%%": Displays percentage values (to one decimal place) -
colors: Specifies the color of each slice
Pie charts are effective when expressing the relative sizes of data.
4. Subplots - Arranging Multiple Graphs
With Matplotlib, you can arrange multiple graphs on a single screen.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) plt.figure(figsize=(10, 4)) # First graph (sine graph) plt.subplot(1, 2, 1) plt.plot(x, y1, color='blue') plt.title("Sine Function") # Second graph (cosine graph) plt.subplot(1, 2, 2) plt.plot(x, y2, color='red') plt.title("Cosine Function") plt.tight_layout() plt.show()
Key Concepts
-
plt.subplot(rows, columns, position): Arranges multiple graphs -
figsize=(10, 4): Adjusts the overall graph size -
plt.tight_layout(): Automatically adjusts the spacing between graphs
Using subplots allows for easy comparison of multiple data sets at once.
Utilizing Matplotlib makes data analysis more intuitive and efficient.
Reference Materials
Lessons in this chapter · Essential Python Libraries for AI
- 1. NumPy: Python Library Optimized for Numerical Computation
- 2. Basic Operations and Advanced Features in NumPy
- 3. Multiple Choice Quiz
- 4. Pandas: Python Library for Data Manipulation
- 5. Basic Operations and Advanced Features in Pandas
- 6. Fill-in-the-Blank Quiz
- 7. Visualize Data with Matplotlib
- 8. Graph Types and Applications with Matplotlib
- 9. Multiple Choice Quiz
- 10. Seaborn: A Visualization Library for Python
- 11. Differences Between Seaborn and Matplotlib and How to Use Both
- 12. Fill-in-the-Blank Quiz
- 13. Scikit-Learn: A Beginner-Friendly Machine Learning Library
- 14. Preprocessing Data and Evaluating Models with Scikit-Learn
- 15. Multiple Choice Quiz
- 16. NLTK: Python Library for Natural Language Processing
- 17. Advanced NLP Techniques with NLTK
- 18. Fill-in-the-Blank Quiz
- 19. OpenCV: Python Library for Computer Vision
- 20. Advanced Image Processing Techniques with OpenCV
- 21. Multiple Choice Quiz
Which method is most appropriate to fill in the blank?
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help