ЁЯУК

Python Matplotlib Course

Jul 14, 2024

# Python Matplotlib Course - Amit Thinks ## Welcome and Introduction - YouTube Channel: Amit Thinks - Course Topic: Python Matplotlib - Matplotlib: Open-source plotting library developed by John D. Hunter - Used for creating interactive visualizations in Python - Popular for data visualization and data science ## Types of Plots with Matplotlib - Line graphs - Histograms - Pie charts - Bar graphs - Scatter plots ## Lesson Topics Covered - Introduction to Matplotlib - Installation and setup - Basic plotting - Using different types of visualizations - Live running examples ## What is Matplotlib? - Python library for interactive visualizations - Created by John D. Hunter in 2003 - Written in Python - Helps in creating figures and diagrams - Features: Free and open-source, supports pandas DataFrame, export to different file formats like PNG, PDF, SVG, etc. - Extendable with third-party packages like Basemap and Cartopy ## Installation and Setup - Use PyCharm (Community Edition) - Steps for setting up Matplotlib: 1. Install Python and pip (current version) 2. Install PyCharm Community Edition 3. Install Matplotlib in PyCharm - Verifying installation by running a simple Python program ## Running First Matplotlib Program - **Importing Libraries:** - `import matplotlib.pyplot as plt` - `import numpy as np` - **Creating a Simple Line Plot:** - Define x and y coordinates using numpy arrays - Plot the graph using `plt.plot(x, y)` - Display using `plt.show()` ## Creating Plots - **Sub-module pyplot: `import matplotlib.pyplot as plt`** - Alias can be used for easier references - **Example: Creating a Line Plot** - Import necessary libraries - Define data points (x and y coordinates) - Use `plt.plot()` to plot the data - Display using `plt.show()` ## Adding Labels and Titles - **Adding Axis Labels:** - `plt.xlabel('X-axis label')` - `plt.ylabel('Y-axis label')` - **Adding Title:** - `plt.title('Title of the plot')` ## Grid Lines - **Adding Grid Lines:** - Use `plt.grid()` to add grid lines to the plot ## Bar Graphs - **Creating Bar Graphs:** - Use `plt.bar(x, y)` - Bars represent data values with rectangular bars of different heights ## Pie Charts - **Creating Pie Charts:** - Use `plt.pie(sizes, labels=labels, autopct='%1.1f%%')` - Example showing distribution of values ## Histograms - **Creating Histograms:** - Use `plt.hist(data, bins)` - Represents frequency distribution of data ## Scatter Plots - **Creating Scatter Plots:** - Use `plt.scatter(x, y)` - Represents relationship between two variables using dots ## Legends - **Adding Legends:** - Use `plt.legend()` to add legends to the plot - Position legends with `loc` parameter - Change background color: `plt.legend().get_frame().set_facecolor('color')` - Adjust font size with `fontsize` parameter ## Exporting Figures - Export visualizations into different file formats such as PNG, PDF, SVG etc. - Example: `plt.savefig('filename.png')` --- # Example Codes ### Line Plot Example ```python import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 10, 1) y = np.arange(10, 0, -1) plt.plot(x, y) plt.xlabel('X-axis label') plt.ylabel('Y-axis label') plt.title('Line Graph') plt.grid(True) plt.show()

Bar Graph Example

import matplotlib.pyplot as plt x = ['A', 'B', 'C', 'D'] y = [10, 20, 15, 25] plt.bar(x, y) plt.xlabel('Categories') plt.ylabel('Values') plt.title('Bar Graph') plt.show()

Pie Chart Example

import matplotlib.pyplot as plt labels = 'A', 'B', 'C', 'D' sizes = [15, 30, 45, 10] plt.pie(sizes, labels=labels, autopct='%1.1f%%') plt.title('Pie Chart') plt.show()

Histogram Example

import matplotlib.pyplot as plt import numpy as np data = [20, 20, 30, 40, 10, 30, 40, 50, 60, 70] bins = [0, 20, 40, 60, 80] plt.hist(data, bins) plt.xlabel('Bins') plt.ylabel('Frequency') plt.title('Histogram') plt.show()

Scatter Plot Example

import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 6, 7, 8] y = [5, 15, 25, 35, 25, 15, 5, 10] plt.scatter(x, y, color='r') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot') plt.show()