IMG_3103.jpeg
Document Details

Uploaded by QuieterEarthArt25
University of Surrey
Full Transcript
# Matplotlib Tutorial ## Introduction - Matplotlib is a plotting library for Python - It is used for creating static, interactive, and animated visualizations in Python. ### Key Features - **Versatile Plotting:** Generate line plots, scatter plots, bar charts, histograms, and more. - **Customiza...
# Matplotlib Tutorial ## Introduction - Matplotlib is a plotting library for Python - It is used for creating static, interactive, and animated visualizations in Python. ### Key Features - **Versatile Plotting:** Generate line plots, scatter plots, bar charts, histograms, and more. - **Customization:** Extensive control over plot elements like colors, markers, labels, and annotations. - **Multi-format Output:** Save plots in various formats, including PNG, JPG, PDF, and SVG. - **Integration:** Works well with NumPy and Pandas. ### Basic Plotting - Sample data ```python import numpy as np import matplotlib.pyplot as plt # Generate some data x = np.linspace(0, 10, 100) y = np.sin(x) ``` - Creating a simple plot ```python # Create a figure and an axes. fig, ax = plt.subplots() # Plot the data ax.plot(x, y) # Add labels and title ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_title('Sine Wave Plot') # Show the plot plt.show() ``` ### Customization - Line Properties ```python # Customize line color, width, and style ax.plot(x, y, color='red', linewidth=2, linestyle='--') ``` - Adding Labels and Annotations ```python # Add a label to the line ax.plot(x, y, label='Sine Wave') ax.legend() # Annotate a point ax.annotate('Max Value', xy=(np.pi/2, 1), xytext=(np.pi/2 + 1, 0.8), arrowprops=dict(facecolor='black', shrink=0.05)) ``` ### Types of Plots - Scatter Plot ```python # Create a scatter plot x = np.random.rand(100) y = np.random.rand(100) plt.scatter(x, y, color='green', marker='o') plt.show() ``` - Bar Chart ```python # Create a bar chart categories = ['A', 'B', 'C', 'D'] values = [25, 40, 30, 35] plt.bar(categories, values, color='skyblue') plt.show() ``` ### Subplots - Creating multiple plots in one figure ```python # Create two subplots fig, axes = plt.subplots(1, 2, figsize=(12, 4)) # Plot on the first subplot axes.plot(x, y, color='blue') axes.set_title('Subplot 1') # Plot on the second subplot axes.scatter(x, y, color='red') axes.set_title('Subplot 2') plt.tight_layout() # Adjust subplot parameters for a tight layout. plt.show() ``` ### Advanced Plotting - 3D Plotting ```python from mpl_toolkits.mplot3d import Axes3D # Create a 3D figure fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(111, projection='3d') # Sample 3D data x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) x, y = np.meshgrid(x, y) z = np.sin(np.sqrt(x**2 + y**2)) # Plot the surface ax.plot_surface(x, y, z, cmap='viridis') # Show the plot plt.show() ``` - Contour Plot ```python # Create a contour plot x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) x, y = np.meshgrid(x, y) z = np.sin(np.sqrt(x**2 + y**2)) # Create a contour plot plt.contour(x, y, z, cmap='coolwarm') plt.colorbar() # Add a colorbar plt.show() ``` ### Saving Plots - Save plots to a file ```python # Save the plot to a file plt.savefig('sine_wave_plot.png') # Save as PDF plt.savefig('sine_wave_plot.pdf', format='pdf') ``` ## Conclusion - Matplotlib is a powerful tool for creating a wide variety of plots and visualizations in Python. Its flexibility and customizability make it an essential library for data analysis and presentation.