Lecture

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.

Drawing a Histogram
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.

Drawing a Scatter Plot
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.

Drawing a Pie Chart
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.

Creating Subplots
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

Mission
0 / 1

Which method is most appropriate to fill in the blank?

In Matplotlib, when arranging multiple graphs, the method is used.
figure()
subplot()
scatter()
pie()

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help