Matplotlib Notes With Outputs
Matplotlib Notes With Outputs
Introduction
Matplotlib is a popular Python library for data visualization. It offers a variety of plots to visualize data
effectively and works seamlessly with libraries like NumPy and Pandas.
Installation
To install Matplotlib, use the following command:
Basic Structure
To create a plot, you generally follow these steps:
2. Create a plot:
plt.show()
Line Plot
Used to visualize trends over time.
Example:
x = [1, 2, 3, 4]
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Line Plot Example')
plt.legend()
plt.show()
Bar Plot
Used for categorical data comparison.
plt.title('Bar Plot')
plt.show()
Scatter Plot
Used for finding relationships between variables.
x = [1, 2, 3, 4]
plt.scatter(x, y, color='red')
plt.title('Scatter Plot')
plt.show()
Pie Chart
Used for displaying proportions.
plt.title('Pie Chart')
plt.show()
Matplotlib Examples with Outputs
Line Plot Example
x = [1, 2, 3, 4]
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Line Plot')
plt.legend()
plt.show()
Output: A line connecting the points (1, 10), (2, 15), (3, 20), (4, 25).
plt.title('Bar Plot')
plt.show()
plt.scatter(x, y, color='red')
plt.title('Scatter Plot')
plt.show()
Output: Red points at coordinates (1, 10), (2, 14), (3, 15), (4, 18).
Pie Chart Example
labels = ['A', 'B', 'C']
plt.title('Pie Chart')
plt.show()
Output: A pie chart with three segments labeled A (20%), B (30%), C (50%).