├── LICENSE ├── Matplotlib Fundamentals ├── 01 Beginner Matplotlib Concepts │ ├── 03 Saving and Displaying Plots │ │ └── saving_displaying_plots.py │ ├── 04 Integration with Pandas │ │ └── integration_pandas.py │ ├── 02 Plot Customization │ │ └── plot_customization.py │ ├── 01 Basic Plotting │ │ └── basic_plotting.py │ └── README.md ├── 03 Advanced Matplotlib Concepts │ ├── 03 Optimization for Large Datasets │ │ └── optimization_large_datasets.py │ ├── 02 Animations and Interactive Plots │ │ └── animations_interactive_plots.py │ ├── 01 3D Visualizations │ │ └── 3d_visualizations.py │ ├── 04 Custom Visualizations │ │ └── custom_visualizations.py │ └── README.md └── 02 Intermediate Matplotlib Concepts │ ├── 03 Advanced Customization │ └── advanced_customization.py │ ├── 04 Data Exploration Visualizations │ └── data_exploration_visualizations.py │ ├── 01 Subplots and Layouts │ └── subplots_layouts.py │ ├── 02 ML Evaluation Plots │ └── ml_evaluation_plots.py │ └── README.md ├── README.md └── Matplotlib Interview Questions └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 rohanmistry231 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Matplotlib Fundamentals/01 Beginner Matplotlib Concepts/03 Saving and Displaying Plots/saving_displaying_plots.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import load_iris 6 | except ImportError: 7 | load_iris = None 8 | 9 | # %% [1. Introduction to Saving and Displaying Plots] 10 | # Learn how to save and display Matplotlib plots for ML workflows. 11 | # Covers saving as PNG/PDF/SVG and displaying in Jupyter/scripts. 12 | 13 | print("Matplotlib version:", plt.matplotlib.__version__) 14 | 15 | # %% [2. Saving Plots as PNG] 16 | # Save a scatter plot as PNG. 17 | if load_iris: 18 | iris = load_iris() 19 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 20 | else: 21 | np.random.seed(42) 22 | df = pd.DataFrame({ 23 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 24 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150) 25 | }) 26 | plt.scatter(df['sepal length (cm)'], df['sepal width (cm)'], color='blue', alpha=0.5) 27 | plt.title('Sepal Length vs. Width') 28 | plt.xlabel('Sepal Length (cm)') 29 | plt.ylabel('Sepal Width (cm)') 30 | plt.savefig('scatter_png.png', dpi=100, bbox_inches='tight') 31 | plt.close() 32 | print("\nScatter plot saved as 'scatter_png.png'") 33 | 34 | # %% [3. Saving Plots as PDF] 35 | # Save a histogram as PDF. 36 | plt.hist(df['sepal length (cm)'], bins=20, color='green', alpha=0.7) 37 | plt.title('Sepal Length Distribution') 38 | plt.xlabel('Sepal Length (cm)') 39 | plt.ylabel('Frequency') 40 | plt.savefig('histogram_pdf.pdf', bbox_inches='tight') 41 | plt.close() 42 | print("Histogram saved as 'histogram_pdf.pdf'") 43 | 44 | # %% [4. Saving Plots as SVG] 45 | # Save a line plot as SVG. 46 | np.random.seed(42) 47 | days = np.arange(1, 31) 48 | sales = 1000 + np.random.normal(0, 50, 30).cumsum() 49 | plt.plot(days, sales, color='red', linestyle='-') 50 | plt.title('Sales Trend') 51 | plt.xlabel('Day') 52 | plt.ylabel('Sales ($)') 53 | plt.savefig('line_svg.svg', bbox_inches='tight') 54 | plt.close() 55 | print("Line plot saved as 'line_svg.svg'") 56 | 57 | # %% [5. Displaying Plots] 58 | # Display a bar plot (commented for non-interactive environments). 59 | categories = ['A', 'B', 'C'] 60 | counts = np.random.randint(50, 100, 3) 61 | plt.bar(categories, counts, color='orange') 62 | plt.title('Category Counts') 63 | plt.xlabel('Category') 64 | plt.ylabel('Count') 65 | # plt.show() # Uncomment to display in interactive environments 66 | plt.savefig('bar_display.png') 67 | plt.close() 68 | print("Bar plot saved as 'bar_display.png' (display commented)") 69 | 70 | # %% [6. Practical ML Application] 71 | # Save an ML feature plot for a report. 72 | np.random.seed(42) 73 | ml_data = pd.DataFrame({ 74 | 'feature1': np.random.normal(10, 2, 100), 75 | 'feature2': np.random.normal(5, 1, 100) 76 | }) 77 | plt.scatter(ml_data['feature1'], ml_data['feature2'], color='purple', alpha=0.6) 78 | plt.title('ML Feature Scatter') 79 | plt.xlabel('Feature1') 80 | plt.ylabel('Feature2') 81 | plt.savefig('ml_feature_report.png', dpi=150, bbox_inches='tight') 82 | plt.close() 83 | print("ML feature scatter saved as 'ml_feature_report.png'") 84 | 85 | # %% [7. Interview Scenario: Saving Plots] 86 | # Discuss saving plots for ML. 87 | print("\nInterview Scenario: Saving Plots") 88 | print("Q: How would you save a Matplotlib plot for an ML report?") 89 | print("A: Use plt.savefig with formats like PNG or PDF, adjusting dpi for quality.") 90 | print("Key: Use bbox_inches='tight' to prevent clipping.") 91 | print("Example: plt.savefig('plot.png', dpi=150, bbox_inches='tight')") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/01 Beginner Matplotlib Concepts/04 Integration with Pandas/integration_pandas.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import pandas as pd 3 | import numpy as np 4 | try: 5 | from sklearn.datasets import load_iris 6 | except ImportError: 7 | load_iris = None 8 | 9 | # %% [1. Introduction to Integration with Pandas] 10 | # Learn how to use Matplotlib with Pandas for ML data visualization. 11 | # Covers plotting DataFrame columns and visualizing grouped data. 12 | 13 | print("Matplotlib version:", plt.matplotlib.__version__) 14 | print("Pandas version:", pd.__version__) 15 | 16 | # %% [2. Plotting DataFrame Columns] 17 | # Plot Iris DataFrame columns. 18 | if load_iris: 19 | iris = load_iris() 20 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 21 | else: 22 | np.random.seed(42) 23 | df = pd.DataFrame({ 24 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 25 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150), 26 | 'petal length (cm)': np.random.normal(3.7, 1.8, 150), 27 | 'petal width (cm)': np.random.normal(1.2, 0.6, 150) 28 | }) 29 | df['sepal length (cm)'].plot(kind='hist', bins=20, color='blue', alpha=0.7, title='Sepal Length Distribution') 30 | plt.xlabel('Sepal Length (cm)') 31 | plt.ylabel('Frequency') 32 | plt.savefig('df_histogram.png') 33 | plt.close() 34 | print("\nDataFrame histogram saved as 'df_histogram.png'") 35 | 36 | # %% [3. Plotting Multiple Columns] 37 | # Plot multiple features. 38 | df[['sepal length (cm)', 'sepal width (cm)']].plot(kind='scatter', x='sepal length (cm)', y='sepal width (cm)', color='green', alpha=0.5) 39 | plt.title('Sepal Length vs. Width') 40 | plt.savefig('df_scatter.png') 41 | plt.close() 42 | print("DataFrame scatter saved as 'df_scatter.png'") 43 | 44 | # %% [4. Visualizing Grouped Data] 45 | # Group by synthetic categories and plot. 46 | np.random.seed(42) 47 | df['category'] = np.random.choice(['A', 'B', 'C'], len(df)) 48 | grouped = df.groupby('category') 49 | for name, group in grouped: 50 | plt.hist(group['sepal length (cm)'], bins=15, alpha=0.5, label=f'Category {name}') 51 | plt.title('Sepal Length by Category') 52 | plt.xlabel('Sepal Length (cm)') 53 | plt.ylabel('Frequency') 54 | plt.legend() 55 | plt.savefig('grouped_histogram.png') 56 | plt.close() 57 | print("Grouped histogram saved as 'grouped_histogram.png'") 58 | 59 | # %% [5. Practical ML Application] 60 | # Visualize ML dataset features with Pandas. 61 | np.random.seed(42) 62 | ml_data = pd.DataFrame({ 63 | 'feature1': np.random.normal(10, 2, 100), 64 | 'feature2': np.random.normal(5, 1, 100), 65 | 'target': np.random.choice([0, 1], 100) 66 | }) 67 | ml_data[ml_data['target'] == 0]['feature1'].plot(kind='hist', bins=15, alpha=0.5, color='blue', label='Class 0') 68 | ml_data[ml_data['target'] == 1]['feature1'].plot(kind='hist', bins=15, alpha=0.5, color='red', label='Class 1') 69 | plt.title('Feature1 Distribution by Class') 70 | plt.xlabel('Feature1') 71 | plt.ylabel('Frequency') 72 | plt.legend() 73 | plt.savefig('ml_class_histogram.png') 74 | plt.close() 75 | print("ML class histogram saved as 'ml_class_histogram.png'") 76 | 77 | # %% [6. Interview Scenario: Pandas Integration] 78 | # Discuss Pandas integration for ML. 79 | print("\nInterview Scenario: Pandas Integration") 80 | print("Q: How would you visualize a Pandas DataFrame column in Matplotlib?") 81 | print("A: Use df.plot(kind='hist') or df.plot(kind='scatter') for quick visualizations.") 82 | print("Key: Pandas’ plot method simplifies Matplotlib integration.") 83 | print("Example: df['col'].plot(kind='hist', bins=20, color='blue')") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/03 Advanced Matplotlib Concepts/03 Optimization for Large Datasets/optimization_large_datasets.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | 5 | # %% [1. Introduction to Optimization for Large Datasets] 6 | # Learn how to optimize Matplotlib for large ML datasets. 7 | # Covers downsampling, sparse data plotting, and blitting for animations. 8 | 9 | print("Matplotlib version:", plt.matplotlib.__version__) 10 | 11 | # %% [2. Downsampling Data] 12 | # Downsample a large dataset for faster plotting. 13 | np.random.seed(42) 14 | n_points = 100000 15 | x = np.linspace(0, 10, n_points) 16 | y = np.sin(x) + np.random.normal(0, 0.1, n_points) 17 | downsample_factor = 100 18 | x_down = x[::downsample_factor] 19 | y_down = y[::downsample_factor] 20 | plt.scatter(x_down, y_down, color='blue', alpha=0.5, s=10) 21 | plt.title('Downsampled Scatter Plot', fontsize=14) 22 | plt.xlabel('X') 23 | plt.ylabel('Y') 24 | plt.grid(True) 25 | plt.savefig('downsampled_scatter.png') 26 | plt.close() 27 | print("\nDownsampled scatter saved as 'downsampled_scatter.png'") 28 | 29 | # %% [3. Plotting Sparse Data] 30 | # Plot sparse data efficiently. 31 | np.random.seed(42) 32 | sparse_data = np.zeros(10000) 33 | indices = np.random.choice(10000, 1000, replace=False) 34 | sparse_data[indices] = np.random.normal(0, 1, 1000) 35 | plt.plot(sparse_data, color='red', linestyle='-', alpha=0.7) 36 | plt.title('Sparse Data Plot', fontsize=14) 37 | plt.xlabel('Index') 38 | plt.ylabel('Value') 39 | plt.grid(True) 40 | plt.savefig('sparse_plot.png') 41 | plt.close() 42 | print("Sparse data plot saved as 'sparse_plot.png'") 43 | 44 | # %% [4. Blitting for Animation Performance] 45 | # Use blitting to optimize animation. 46 | np.random.seed(42) 47 | t = np.linspace(0, 10, 100) 48 | fig, ax = plt.subplots(figsize=(8, 6)) 49 | line, = ax.plot([], [], color='purple', label='Wave') 50 | ax.set_xlim(0, 10) 51 | ax.set_ylim(-2, 2) 52 | ax.set_xlabel('Time') 53 | ax.set_ylabel('Amplitude') 54 | ax.set_title('Optimized Wave Animation') 55 | ax.legend() 56 | ax.grid(True) 57 | 58 | def init(): 59 | line.set_data([], []) 60 | return line, 61 | 62 | def update(frame): 63 | line.set_data(t, np.sin(t + frame * 0.1)) 64 | return line, 65 | 66 | ani = FuncAnimation(fig, update, init_func=init, frames=100, interval=50, blit=True) 67 | ani.save('wave_animation.mp4', writer='ffmpeg') 68 | plt.close() 69 | print("Optimized wave animation saved as 'wave_animation.mp4'") 70 | 71 | # %% [5. Practical ML Application] 72 | # Optimize a large ML dataset visualization. 73 | np.random.seed(42) 74 | ml_data = pd.DataFrame({ 75 | 'feature1': np.random.normal(10, 2, 100000), 76 | 'feature2': np.random.normal(5, 1, 100000), 77 | 'target': np.random.choice([0, 1], 100000) 78 | }) 79 | downsample_idx = np.random.choice(len(ml_data), 1000, replace=False) 80 | ml_down = ml_data.iloc[downsample_idx] 81 | plt.scatter(ml_down['feature1'], ml_down['feature2'], c=ml_down['target'], cmap='coolwarm', alpha=0.5, s=10) 82 | plt.title('Downsampled ML Features', fontsize=14) 83 | plt.xlabel('Feature1') 84 | plt.ylabel('Feature2') 85 | plt.colorbar(label='Class') 86 | plt.grid(True) 87 | plt.savefig('ml_downsampled.png') 88 | plt.close() 89 | print("Downsampled ML plot saved as 'ml_downsampled.png'") 90 | 91 | # %% [6. Interview Scenario: Optimization] 92 | # Discuss optimization for large datasets. 93 | print("\nInterview Scenario: Optimization") 94 | print("Q: How would you optimize a Matplotlib plot for a large dataset?") 95 | print("A: Downsample data, use sparse plotting, or enable blitting for animations.") 96 | print("Key: Downsampling reduces rendering time while preserving trends.") 97 | print("Example: x_down = x[::100]; plt.scatter(x_down, y_down)") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/01 Beginner Matplotlib Concepts/02 Plot Customization/plot_customization.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import load_iris 6 | except ImportError: 7 | load_iris = None 8 | 9 | # %% [1. Introduction to Plot Customization] 10 | # Learn how to customize Matplotlib plots for clarity in ML visualization. 11 | # Covers titles, labels, legends, colors, markers, and figure settings. 12 | 13 | print("Matplotlib version:", plt.matplotlib.__version__) 14 | 15 | # %% [2. Setting Titles, Labels, and Legends] 16 | # Customize a scatter plot with titles and labels. 17 | if load_iris: 18 | iris = load_iris() 19 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 20 | else: 21 | np.random.seed(42) 22 | df = pd.DataFrame({ 23 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 24 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150) 25 | }) 26 | plt.scatter(df['sepal length (cm)'], df['sepal width (cm)'], color='blue', alpha=0.5, label='Iris Data') 27 | plt.title('Sepal Length vs. Width', fontsize=14, pad=10) 28 | plt.xlabel('Sepal Length (cm)', fontsize=12) 29 | plt.ylabel('Sepal Width (cm)', fontsize=12) 30 | plt.legend(loc='upper right', fontsize=10) 31 | plt.grid(True, linestyle='--', alpha=0.7) 32 | plt.savefig('customized_scatter.png') 33 | plt.close() 34 | print("\nCustomized scatter plot saved as 'customized_scatter.png'") 35 | 36 | # %% [3. Customizing Colors, Markers, and Line Styles] 37 | # Customize a line plot. 38 | np.random.seed(42) 39 | days = np.arange(1, 31) 40 | sales = 1000 + np.random.normal(0, 50, 30).cumsum() 41 | plt.plot(days, sales, color='red', linestyle='--', marker='o', markersize=5, label='Sales Trend') 42 | plt.title('Customized Sales Trend', fontsize=14) 43 | plt.xlabel('Day', fontsize=12) 44 | plt.ylabel('Sales ($)', fontsize=12) 45 | plt.legend(loc='best') 46 | plt.savefig('customized_line.png') 47 | plt.close() 48 | print("Customized line plot saved as 'customized_line.png'") 49 | 50 | # %% [4. Adjusting Figure Size and Resolution] 51 | # Create a large figure with high resolution. 52 | plt.figure(figsize=(10, 6), dpi=100) 53 | plt.hist(df['sepal length (cm)'], bins=20, color='green', alpha=0.7, label='Sepal Length') 54 | plt.title('Sepal Length Distribution', fontsize=14) 55 | plt.xlabel('Sepal Length (cm)', fontsize=12) 56 | plt.ylabel('Frequency', fontsize=12) 57 | plt.legend() 58 | plt.savefig('large_histogram.png') 59 | plt.close() 60 | print("Large histogram saved as 'large_histogram.png'") 61 | 62 | # %% [5. Practical ML Application] 63 | # Customize a plot for ML feature comparison. 64 | np.random.seed(42) 65 | ml_data = pd.DataFrame({ 66 | 'feature1': np.random.normal(10, 2, 100), 67 | 'feature2': np.random.normal(5, 1, 100) 68 | }) 69 | plt.figure(figsize=(8, 5)) 70 | plt.scatter(ml_data['feature1'], ml_data['feature2'], color='purple', marker='^', alpha=0.6, label='Features') 71 | plt.title('ML Feature Comparison', fontsize=14) 72 | plt.xlabel('Feature1', fontsize=12) 73 | plt.ylabel('Feature2', fontsize=12) 74 | plt.legend(loc='upper left') 75 | plt.grid(True, linestyle=':') 76 | plt.savefig('ml_feature_scatter.png') 77 | plt.close() 78 | print("ML feature scatter saved as 'ml_feature_scatter.png'") 79 | 80 | # %% [6. Interview Scenario: Customization] 81 | # Discuss plot customization for ML. 82 | print("\nInterview Scenario: Customization") 83 | print("Q: How would you customize a Matplotlib plot for an ML report?") 84 | print("A: Set title, labels, legend with plt.title, plt.xlabel, plt.legend; adjust colors and figure size.") 85 | print("Key: Clear labels and styles enhance interpretability.") 86 | print("Example: plt.title('Feature Plot', fontsize=14); plt.figure(figsize=(8, 5))") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/01 Beginner Matplotlib Concepts/01 Basic Plotting/basic_plotting.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import load_iris 6 | except ImportError: 7 | load_iris = None 8 | 9 | # %% [1. Introduction to Basic Plotting] 10 | # Learn fundamental Matplotlib plotting for ML data visualization. 11 | # Covers line plots, scatter plots, bar plots, and histograms. 12 | 13 | print("Matplotlib version:", plt.matplotlib.__version__) 14 | 15 | # %% [2. Line Plot] 16 | # Create a line plot for a trend (e.g., synthetic time-series). 17 | np.random.seed(42) 18 | days = np.arange(1, 31) 19 | sales = 1000 + np.random.normal(0, 50, 30).cumsum() 20 | plt.plot(days, sales, color='blue', linestyle='-', label='Sales Trend') 21 | plt.xlabel('Day') 22 | plt.ylabel('Sales ($)') 23 | plt.title('Sales Trend Over 30 Days') 24 | plt.legend() 25 | plt.savefig('line_plot.png') 26 | plt.close() 27 | print("\nLine plot saved as 'line_plot.png'") 28 | 29 | # %% [3. Scatter Plot] 30 | # Create a scatter plot for feature relationships (e.g., Iris dataset). 31 | if load_iris: 32 | iris = load_iris() 33 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 34 | else: 35 | np.random.seed(42) 36 | df = pd.DataFrame({ 37 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 38 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150) 39 | }) 40 | plt.scatter(df['sepal length (cm)'], df['sepal width (cm)'], color='green', alpha=0.5, label='Data Points') 41 | plt.xlabel('Sepal Length (cm)') 42 | plt.ylabel('Sepal Width (cm)') 43 | plt.title('Sepal Length vs. Width') 44 | plt.legend() 45 | plt.savefig('scatter_plot.png') 46 | plt.close() 47 | print("Scatter plot saved as 'scatter_plot.png'") 48 | 49 | # %% [4. Bar Plot] 50 | # Create a bar plot for category comparisons. 51 | np.random.seed(42) 52 | categories = ['A', 'B', 'C'] 53 | counts = np.random.randint(50, 100, 3) 54 | plt.bar(categories, counts, color='orange', label='Category Counts') 55 | plt.xlabel('Category') 56 | plt.ylabel('Count') 57 | plt.title('Category Counts') 58 | plt.legend() 59 | plt.savefig('bar_plot.png') 60 | plt.close() 61 | print("Bar plot saved as 'bar_plot.png'") 62 | 63 | # %% [5. Histogram] 64 | # Create a histogram for distribution (e.g., Iris feature). 65 | sepal_lengths = df['sepal length (cm)'] 66 | plt.hist(sepal_lengths, bins=20, color='purple', alpha=0.7, label='Sepal Length') 67 | plt.xlabel('Sepal Length (cm)') 68 | plt.ylabel('Frequency') 69 | plt.title('Distribution of Sepal Length') 70 | plt.legend() 71 | plt.savefig('histogram.png') 72 | plt.close() 73 | print("Histogram saved as 'histogram.png'") 74 | 75 | # %% [6. Practical ML Application] 76 | # Visualize ML feature distributions. 77 | np.random.seed(42) 78 | ml_data = pd.DataFrame({ 79 | 'feature1': np.random.normal(10, 2, 100), 80 | 'feature2': np.random.normal(5, 1, 100) 81 | }) 82 | plt.hist(ml_data['feature1'], bins=15, color='blue', alpha=0.5, label='Feature1') 83 | plt.hist(ml_data['feature2'], bins=15, color='red', alpha=0.5, label='Feature2') 84 | plt.xlabel('Value') 85 | plt.ylabel('Frequency') 86 | plt.title('ML Feature Distributions') 87 | plt.legend() 88 | plt.savefig('ml_features_histogram.png') 89 | plt.close() 90 | print("ML feature histogram saved as 'ml_features_histogram.png'") 91 | 92 | # %% [7. Interview Scenario: Basic Plotting] 93 | # Discuss basic plotting for ML. 94 | print("\nInterview Scenario: Basic Plotting") 95 | print("Q: How would you visualize a feature’s distribution in Matplotlib?") 96 | print("A: Use plt.hist to create a histogram with customizable bins and colors.") 97 | print("Key: Histograms reveal data distributions for ML preprocessing.") 98 | print("Example: plt.hist(df['col'], bins=20, color='blue')") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/02 Intermediate Matplotlib Concepts/03 Advanced Customization/advanced_customization.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import load_iris 6 | except ImportError: 7 | load_iris = None 8 | 9 | # %% [1. Introduction to Advanced Customization] 10 | # Learn advanced Matplotlib customization for ML visualizations. 11 | # Covers annotations, colormaps, and axis customization. 12 | 13 | print("Matplotlib version:", plt.matplotlib.__version__) 14 | 15 | # %% [2. Annotating Plots] 16 | # Annotate key points in a scatter plot. 17 | if load_iris: 18 | iris = load_iris() 19 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 20 | else: 21 | np.random.seed(42) 22 | df = pd.DataFrame({ 23 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 24 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150) 25 | }) 26 | plt.scatter(df['sepal length (cm)'], df['sepal width (cm)'], color='blue', alpha=0.5) 27 | max_point = df.iloc[df['sepal length (cm)'].idxmax()] 28 | plt.annotate('Max Length', xy=(max_point['sepal length (cm)'], max_point['sepal width (cm)']), 29 | xytext=(max_point['sepal length (cm)'] + 0.5, max_point['sepal width (cm)'] + 0.5), 30 | arrowprops=dict(facecolor='black', shrink=0.05)) 31 | plt.title('Sepal Length vs. Width with Annotation', fontsize=14) 32 | plt.xlabel('Sepal Length (cm)') 33 | plt.ylabel('Sepal Width (cm)') 34 | plt.savefig('annotated_scatter.png') 35 | plt.close() 36 | print("\nAnnotated scatter saved as 'annotated_scatter.png'") 37 | 38 | # %% [3. Using Colormaps] 39 | # Apply a colormap to a scatter plot. 40 | np.random.seed(42) 41 | values = np.random.rand(len(df)) 42 | plt.scatter(df['sepal length (cm)'], df['sepal width (cm)'], c=values, cmap='viridis', alpha=0.5) 43 | plt.colorbar(label='Value') 44 | plt.title('Sepal Features with Viridis Colormap', fontsize=14) 45 | plt.xlabel('Sepal Length (cm)') 46 | plt.ylabel('Sepal Width (cm)') 47 | plt.savefig('colormap_scatter.png') 48 | plt.close() 49 | print("Colormap scatter saved as 'colormap_scatter.png'") 50 | 51 | # %% [4. Customizing Axes] 52 | # Customize axes with log scale and grid. 53 | np.random.seed(42) 54 | x = np.linspace(1, 100, 100) 55 | y = np.exp(0.05 * x + np.random.normal(0, 0.1, 100)) 56 | plt.plot(x, y, color='red', label='Exponential Trend') 57 | plt.yscale('log') 58 | plt.grid(True, which='both', linestyle='--', alpha=0.7) 59 | plt.title('Exponential Trend with Log Scale', fontsize=14) 60 | plt.xlabel('X') 61 | plt.ylabel('Y (log scale)') 62 | plt.legend() 63 | plt.savefig('custom_axes.png') 64 | plt.close() 65 | print("Custom axes plot saved as 'custom_axes.png'") 66 | 67 | # %% [5. Practical ML Application] 68 | # Customize an ML feature plot. 69 | np.random.seed(42) 70 | ml_data = pd.DataFrame({ 71 | 'feature1': np.random.normal(10, 2, 100), 72 | 'feature2': np.random.normal(5, 1, 100), 73 | 'target': np.random.choice([0, 1], 100) 74 | }) 75 | plt.scatter(ml_data['feature1'], ml_data['feature2'], c=ml_data['target'], cmap='coolwarm', alpha=0.6) 76 | plt.colorbar(label='Class') 77 | plt.annotate('Class 0 Cluster', xy=(10, 5), xytext=(12, 6), arrowprops=dict(facecolor='black', shrink=0.05)) 78 | plt.title('ML Features by Class', fontsize=14) 79 | plt.xlabel('Feature1') 80 | plt.ylabel('Feature2') 81 | plt.grid(True, linestyle=':') 82 | plt.savefig('ml_custom_plot.png') 83 | plt.close() 84 | print("ML custom plot saved as 'ml_custom_plot.png'") 85 | 86 | # %% [6. Interview Scenario: Customization] 87 | # Discuss advanced customization for ML. 88 | print("\nInterview Scenario: Customization") 89 | print("Q: How would you highlight a key point in a Matplotlib scatter plot?") 90 | print("A: Use plt.annotate to add text and an arrow pointing to the point.") 91 | print("Key: Annotations improve interpretability of ML visualizations.") 92 | print("Example: plt.annotate('Key', xy=(x, y), arrowprops=dict(facecolor='black'))") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/02 Intermediate Matplotlib Concepts/04 Data Exploration Visualizations/data_exploration_visualizations.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import load_iris 6 | import seaborn as sns 7 | except ImportError: 8 | load_iris, sns = None, None 9 | 10 | # %% [1. Introduction to Data Exploration Visualizations] 11 | # Learn Matplotlib visualizations for exploring ML data. 12 | # Covers box plots, pair plots, and correlation heatmaps. 13 | 14 | print("Matplotlib version:", plt.matplotlib.__version__) 15 | 16 | # %% [2. Box Plots for Outlier Detection] 17 | # Create box plots for Iris features. 18 | if load_iris: 19 | iris = load_iris() 20 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 21 | else: 22 | np.random.seed(42) 23 | df = pd.DataFrame({ 24 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 25 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150), 26 | 'petal length (cm)': np.random.normal(3.7, 1.8, 150), 27 | 'petal width (cm)': np.random.normal(1.2, 0.6, 150) 28 | }) 29 | plt.boxplot([df['sepal length (cm)'], df['sepal width (cm)'], df['petal length (cm)'], df['petal width (cm)']], 30 | labels=['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']) 31 | plt.title('Box Plots of Iris Features', fontsize=14) 32 | plt.ylabel('Value (cm)') 33 | plt.savefig('box_plots.png') 34 | plt.close() 35 | print("\nBox plots saved as 'box_plots.png'") 36 | 37 | # %% [3. Pair Plots for Feature Relationships] 38 | # Create pair plots with Seaborn. 39 | if sns and load_iris: 40 | sns.pairplot(df, diag_kind='hist', plot_kws={'alpha': 0.5}) 41 | plt.suptitle('Pair Plot of Iris Features', y=1.02, fontsize=16) 42 | plt.savefig('pair_plot.png') 43 | else: 44 | fig, ax = plt.subplots(1, 1, figsize=(6, 6)) 45 | ax.scatter(df['sepal length (cm)'], df['sepal width (cm)'], alpha=0.5, color='blue') 46 | ax.set_title('Sepal Length vs. Width (Fallback)') 47 | ax.set_xlabel('Sepal Length (cm)') 48 | ax.set_ylabel('Sepal Width (cm)') 49 | plt.savefig('pair_plot.png') 50 | plt.close() 51 | print("Pair plot saved as 'pair_plot.png'") 52 | 53 | # %% [4. Correlation Heatmaps] 54 | # Create a correlation heatmap. 55 | corr_matrix = df.corr() 56 | if sns: 57 | plt.figure(figsize=(8, 6)) 58 | sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1) 59 | plt.title('Correlation Heatmap of Iris Features', fontsize=14) 60 | plt.savefig('correlation_heatmap.png') 61 | else: 62 | plt.imshow(corr_matrix, cmap='coolwarm', vmin=-1, vmax=1) 63 | plt.colorbar() 64 | plt.title('Correlation Heatmap (Fallback)') 65 | plt.xticks(range(len(corr_matrix)), corr_matrix.columns, rotation=45) 66 | plt.yticks(range(len(corr_matrix)), corr_matrix.index) 67 | plt.savefig('correlation_heatmap.png') 68 | plt.close() 69 | print("Correlation heatmap saved as 'correlation_heatmap.png'") 70 | 71 | # %% [5. Practical ML Application] 72 | # Explore ML dataset with visualizations. 73 | np.random.seed(42) 74 | ml_data = pd.DataFrame({ 75 | 'feature1': np.random.normal(10, 2, 100), 76 | 'feature2': np.random.normal(5, 1, 100), 77 | 'feature3': np.random.normal(0, 0.5, 100), 78 | 'target': np.random.choice([0, 1], 100) 79 | }) 80 | plt.boxplot([ml_data[ml_data['target'] == 0]['feature1'], ml_data[ml_data['target'] == 1]['feature1']], 81 | labels=['Class 0', 'Class 1']) 82 | plt.title('Feature1 Box Plot by Class', fontsize=14) 83 | plt.ylabel('Feature1') 84 | plt.savefig('ml_box_plot.png') 85 | plt.close() 86 | print("ML box plot saved as 'ml_box_plot.png'") 87 | 88 | # %% [6. Interview Scenario: Data Exploration] 89 | # Discuss data exploration visualizations for ML. 90 | print("\nInterview Scenario: Data Exploration") 91 | print("Q: How would you visualize feature relationships in a dataset?") 92 | print("A: Use seaborn.pairplot for scatter and histogram plots or plt.boxplot for distributions.") 93 | print("Key: Pair plots reveal feature correlations; box plots show outliers.") 94 | print("Example: sns.pairplot(df, diag_kind='hist')") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/02 Intermediate Matplotlib Concepts/01 Subplots and Layouts/subplots_layouts.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import load_iris 6 | except ImportError: 7 | load_iris = None 8 | 9 | # %% [1. Introduction to Subplots and Layouts] 10 | # Learn how to create and manage subplots for ML visualizations. 11 | # Covers creating subplots, adjusting spacing, and sharing axes. 12 | 13 | print("Matplotlib version:", plt.matplotlib.__version__) 14 | 15 | # %% [2. Creating Multiple Plots in a Grid] 16 | # Create a 2x2 subplot grid for Iris features. 17 | if load_iris: 18 | iris = load_iris() 19 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 20 | else: 21 | np.random.seed(42) 22 | df = pd.DataFrame({ 23 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 24 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150), 25 | 'petal length (cm)': np.random.normal(3.7, 1.8, 150), 26 | 'petal width (cm)': np.random.normal(1.2, 0.6, 150) 27 | }) 28 | 29 | fig, axes = plt.subplots(2, 2, figsize=(10, 8)) 30 | axes[0, 0].hist(df['sepal length (cm)'], bins=20, color='blue', alpha=0.7) 31 | axes[0, 0].set_title('Sepal Length') 32 | axes[0, 1].hist(df['sepal width (cm)'], bins=20, color='green', alpha=0.7) 33 | axes[0, 1].set_title('Sepal Width') 34 | axes[1, 0].hist(df['petal length (cm)'], bins=20, color='red', alpha=0.7) 35 | axes[1, 0].set_title('Petal Length') 36 | axes[1, 1].hist(df['petal width (cm)'], bins=20, color='purple', alpha=0.7) 37 | axes[1, 1].set_title('Petal Width') 38 | plt.suptitle('Iris Feature Distributions', fontsize=16) 39 | plt.savefig('subplot_grid.png') 40 | plt.close() 41 | print("\nSubplot grid saved as 'subplot_grid.png'") 42 | 43 | # %% [3. Adjusting Subplot Spacing] 44 | # Create subplots with adjusted spacing. 45 | fig, axes = plt.subplots(2, 2, figsize=(10, 8)) 46 | axes[0, 0].scatter(df['sepal length (cm)'], df['sepal width (cm)'], color='blue', alpha=0.5) 47 | axes[0, 0].set_title('Sepal Length vs. Width') 48 | axes[0, 1].scatter(df['petal length (cm)'], df['petal width (cm)'], color='green', alpha=0.5) 49 | axes[0, 1].set_title('Petal Length vs. Width') 50 | axes[1, 0].plot(df['sepal length (cm)'], color='red', label='Sepal Length') 51 | axes[1, 0].set_title('Sepal Length Trend') 52 | axes[1, 1].plot(df['petal length (cm)'], color='purple', label='Petal Length') 53 | axes[1, 1].set_title('Petal Length Trend') 54 | plt.tight_layout() 55 | plt.suptitle('Mixed Subplots', fontsize=16, y=1.05) 56 | plt.savefig('subplot_spacing.png') 57 | plt.close() 58 | print("Subplot with spacing saved as 'subplot_spacing.png'") 59 | 60 | # %% [4. Sharing Axes for Consistent Scales] 61 | # Create subplots with shared axes. 62 | fig, axes = plt.subplots(1, 2, figsize=(12, 5), sharey=True) 63 | axes[0].hist(df['sepal length (cm)'], bins=20, color='blue', alpha=0.7) 64 | axes[0].set_title('Sepal Length') 65 | axes[0].set_ylabel('Frequency') 66 | axes[1].hist(df['petal length (cm)'], bins=20, color='red', alpha=0.7) 67 | axes[1].set_title('Petal Length') 68 | plt.suptitle('Shared Y-Axis Histograms', fontsize=16) 69 | plt.savefig('shared_axes.png') 70 | plt.close() 71 | print("Shared axes subplot saved as 'shared_axes.png'") 72 | 73 | # %% [5. Practical ML Application] 74 | # Visualize ML features in subplots. 75 | np.random.seed(42) 76 | ml_data = pd.DataFrame({ 77 | 'feature1': np.random.normal(10, 2, 100), 78 | 'feature2': np.random.normal(5, 1, 100), 79 | 'target': np.random.choice([0, 1], 100) 80 | }) 81 | fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True) 82 | for target in [0, 1]: 83 | subset = ml_data[ml_data['target'] == target] 84 | axes[0].hist(subset['feature1'], bins=15, alpha=0.5, label=f'Class {target}') 85 | axes[1].hist(subset['feature2'], bins=15, alpha=0.5, label=f'Class {target}') 86 | axes[0].set_title('Feature1 by Class') 87 | axes[0].set_ylabel('Frequency') 88 | axes[1].set_title('Feature2 by Class') 89 | axes[0].legend() 90 | axes[1].legend() 91 | plt.suptitle('ML Feature Distributions', fontsize=16) 92 | plt.tight_layout() 93 | plt.savefig('ml_subplots.png') 94 | plt.close() 95 | print("ML subplots saved as 'ml_subplots.png'") 96 | 97 | # %% [6. Interview Scenario: Subplots] 98 | # Discuss subplots for ML. 99 | print("\nInterview Scenario: Subplots") 100 | print("Q: How would you create a grid of plots in Matplotlib for ML features?") 101 | print("A: Use plt.subplots to create a grid and plot on each axis with axes[i, j].") 102 | print("Key: Subplots organize multiple visualizations for comparison.") 103 | print("Example: fig, axes = plt.subplots(2, 2); axes[0, 0].hist(df['col'])") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/03 Advanced Matplotlib Concepts/02 Animations and Interactive Plots/animations_interactive_plots.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | from matplotlib.animation import FuncAnimation 5 | try: 6 | import mplcursors 7 | import tkinter as tk 8 | from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 9 | except ImportError: 10 | mplcursors, tk, FigureCanvasTkAgg = None, None, None 11 | 12 | # %% [1. Introduction to Animations and Interactive Plots] 13 | # Learn how to create dynamic and interactive visualizations for ML. 14 | # Covers animations, interactive plots, and GUI embedding. 15 | 16 | print("Matplotlib version:", plt.matplotlib.__version__) 17 | 18 | # %% [2. Creating Animations with FuncAnimation] 19 | # Animate a loss curve. 20 | np.random.seed(42) 21 | epochs = np.arange(1, 21) 22 | loss = 1 / (1 + 0.5 * epochs) + np.random.normal(0, 0.02, 20) 23 | fig, ax = plt.subplots(figsize=(8, 6)) 24 | line, = ax.plot([], [], label='Training Loss', color='blue') 25 | ax.set_xlim(1, 20) 26 | ax.set_ylim(0, 1) 27 | ax.set_xlabel('Epoch') 28 | ax.set_ylabel('Loss') 29 | ax.set_title('Animated Loss Curve') 30 | ax.legend() 31 | ax.grid(True) 32 | 33 | def update(frame): 34 | line.set_data(epochs[:frame], loss[:frame]) 35 | return line, 36 | 37 | ani = FuncAnimation(fig, update, frames=range(1, 21), interval=200, blit=True) 38 | ani.save('loss_animation.mp4', writer='ffmpeg') 39 | plt.close() 40 | print("\nLoss animation saved as 'loss_animation.mp4'") 41 | 42 | # %% [3. Interactive Plots with mplcursors] 43 | # Create an interactive scatter plot. 44 | np.random.seed(42) 45 | df = pd.DataFrame({ 46 | 'feature1': np.random.normal(10, 2, 100), 47 | 'feature2': np.random.normal(5, 1, 100) 48 | }) 49 | fig, ax = plt.subplots(figsize=(8, 6)) 50 | scatter = ax.scatter(df['feature1'], df['feature2'], color='purple', alpha=0.6) 51 | ax.set_title('Interactive Feature Scatter') 52 | ax.set_xlabel('Feature1') 53 | ax.set_ylabel('Feature2') 54 | if mplcursors: 55 | cursor = mplcursors.cursor(scatter, hover=True) 56 | cursor.connect("add", lambda sel: sel.annotation.set_text( 57 | f"({df['feature1'].iloc[sel.index]:.2f}, {df['feature2'].iloc[sel.index]:.2f})")) 58 | plt.savefig('interactive_scatter.png') 59 | plt.close() 60 | print("Interactive scatter saved as 'interactive_scatter.png'") 61 | 62 | # %% [4. Embedding Plots in Tkinter GUI] 63 | # Embed a plot in a Tkinter window (commented for non-GUI environments). 64 | if tk and FigureCanvasTkAgg: 65 | root = tk.Tk() 66 | root.title("Matplotlib in Tkinter") 67 | fig, ax = plt.subplots(figsize=(6, 4)) 68 | ax.plot(epochs, loss, color='red', label='Loss') 69 | ax.set_title('Loss Curve in GUI') 70 | ax.set_xlabel('Epoch') 71 | ax.set_ylabel('Loss') 72 | ax.legend() 73 | canvas = FigureCanvasTkAgg(fig, master=root) 74 | canvas.draw() 75 | canvas.get_tk_widget().pack() 76 | # root.mainloop() # Uncomment to run GUI 77 | plt.savefig('tkinter_plot.png') 78 | plt.close() 79 | print("Tkinter plot saved as 'tkinter_plot.png'") 80 | else: 81 | print("Tkinter or FigureCanvasTkAgg unavailable; skipping GUI plot.") 82 | 83 | # %% [5. Practical ML Application] 84 | # Animate ML training process. 85 | np.random.seed(42) 86 | epochs = np.arange(1, 21) 87 | train_acc = 0.6 + 0.04 * np.arange(20) + np.random.normal(0, 0.01, 20) 88 | val_acc = 0.55 + 0.035 * np.arange(20) + np.random.normal(0, 0.015, 20) 89 | fig, ax = plt.subplots(figsize=(8, 6)) 90 | line1, = ax.plot([], [], label='Training Accuracy', color='blue') 91 | line2, = ax.plot([], [], label='Validation Accuracy', color='red') 92 | ax.set_xlim(1, 20) 93 | ax.set_ylim(0, 1) 94 | ax.set_xlabel('Epoch') 95 | ax.set_ylabel('Accuracy') 96 | ax.set_title('Animated ML Training Accuracy') 97 | ax.legend() 98 | ax.grid(True) 99 | 100 | def update(frame): 101 | line1.set_data(epochs[:frame], train_acc[:frame]) 102 | line2.set_data(epochs[:frame], val_acc[:frame]) 103 | return line1, line2 104 | 105 | ani = FuncAnimation(fig, update, frames=range(1, 21), interval=200, blit=True) 106 | ani.save('ml_training_animation.mp4', writer='ffmpeg') 107 | plt.close() 108 | print("ML training animation saved as 'ml_training_animation.mp4'") 109 | 110 | # %% [6. Interview Scenario: Animations] 111 | # Discuss animations for ML. 112 | print("\nInterview Scenario: Animations") 113 | print("Q: How would you animate a training process in Matplotlib?") 114 | print("A: Use FuncAnimation to update plot data over frames.") 115 | print("Key: Animations visualize dynamic ML processes like training.") 116 | print("Example: ani = FuncAnimation(fig, update, frames=range(20), interval=200)") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/03 Advanced Matplotlib Concepts/01 3D Visualizations/3d_visualizations.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | from mpl_toolkits.mplot3d import Axes3D 5 | try: 6 | from sklearn.datasets import load_iris 7 | from sklearn.decomposition import PCA 8 | except ImportError: 9 | load_iris, PCA = None, None 10 | 11 | # %% [1. Introduction to 3D Visualizations] 12 | # Learn how to create 3D visualizations for ML data with Matplotlib. 13 | # Covers 3D scatter plots, surface plots, and axis customization. 14 | 15 | print("Matplotlib version:", plt.matplotlib.__version__) 16 | 17 | # %% [2. 3D Scatter Plot] 18 | # Create a 3D scatter plot for Iris features. 19 | if load_iris: 20 | iris = load_iris() 21 | df = pd.DataFrame(data=iris.data, columns=iris.feature_names) 22 | labels = iris.target 23 | else: 24 | np.random.seed(42) 25 | df = pd.DataFrame({ 26 | 'sepal length (cm)': np.random.normal(5.8, 0.8, 150), 27 | 'sepal width (cm)': np.random.normal(3.0, 0.4, 150), 28 | 'petal length (cm)': np.random.normal(3.7, 1.8, 150) 29 | }) 30 | labels = np.random.choice([0, 1, 2], 150) 31 | 32 | fig = plt.figure(figsize=(8, 6)) 33 | ax = fig.add_subplot(111, projection='3d') 34 | scatter = ax.scatter(df['sepal length (cm)'], df['sepal width (cm)'], df['petal length (cm)'], 35 | c=labels, cmap='viridis', alpha=0.6) 36 | ax.set_xlabel('Sepal Length (cm)') 37 | ax.set_ylabel('Sepal Width (cm)') 38 | ax.set_zlabel('Petal Length (cm)') 39 | ax.set_title('3D Scatter of Iris Features') 40 | plt.colorbar(scatter, label='Class') 41 | plt.savefig('3d_scatter.png') 42 | plt.close() 43 | print("\n3D scatter plot saved as '3d_scatter.png'") 44 | 45 | # %% [3. 3D Surface Plot] 46 | # Create a 3D surface plot for synthetic data. 47 | np.random.seed(42) 48 | x = np.linspace(-5, 5, 50) 49 | y = np.linspace(-5, 5, 50) 50 | X, Y = np.meshgrid(x, y) 51 | Z = np.sin(np.sqrt(X**2 + Y**2)) 52 | fig = plt.figure(figsize=(8, 6)) 53 | ax = fig.add_subplot(111, projection='3d') 54 | surf = ax.plot_surface(X, Y, Z, cmap='coolwarm', alpha=0.8) 55 | ax.set_xlabel('X') 56 | ax.set_ylabel('Y') 57 | ax.set_zlabel('Z') 58 | ax.set_title('3D Surface Plot') 59 | plt.colorbar(surf, label='Value') 60 | plt.savefig('3d_surface.png') 61 | plt.close() 62 | print("3D surface plot saved as '3d_surface.png'") 63 | 64 | # %% [4. Visualizing High-Dimensional Data with PCA] 65 | # Use PCA to project data into 3D. 66 | if load_iris and PCA: 67 | pca = PCA(n_components=3) 68 | X_pca = pca.fit_transform(df) 69 | fig = plt.figure(figsize=(8, 6)) 70 | ax = fig.add_subplot(111, projection='3d') 71 | scatter = ax.scatter(X_pca[:, 0], X_pca[:, 1], X_pca[:, 2], c=labels, cmap='plasma', alpha=0.6) 72 | ax.set_xlabel('PC1') 73 | ax.set_ylabel('PC2') 74 | ax.set_zlabel('PC3') 75 | ax.set_title('PCA Projection of Iris Features') 76 | plt.colorbar(scatter, label='Class') 77 | plt.savefig('pca_3d.png') 78 | plt.close() 79 | print("PCA 3D plot saved as 'pca_3d.png'") 80 | else: 81 | print("PCA or Iris dataset unavailable; skipping PCA plot.") 82 | 83 | # %% [5. Customizing 3D Axes and Viewpoints] 84 | # Customize 3D plot with viewpoint. 85 | fig = plt.figure(figsize=(8, 6)) 86 | ax = fig.add_subplot(111, projection='3d') 87 | ax.scatter(df['sepal length (cm)'], df['sepal width (cm)'], df['petal length (cm)'], 88 | c=labels, cmap='viridis', alpha=0.6) 89 | ax.set_xlabel('Sepal Length (cm)') 90 | ax.set_ylabel('Sepal Width (cm)') 91 | ax.set_zlabel('Petal Length (cm)') 92 | ax.set_title('Custom 3D View') 93 | ax.view_init(elev=30, azim=45) 94 | plt.savefig('custom_3d_view.png') 95 | plt.close() 96 | print("Custom 3D view saved as 'custom_3d_view.png'") 97 | 98 | # %% [6. Practical ML Application] 99 | # Visualize ML features in 3D. 100 | np.random.seed(42) 101 | ml_data = pd.DataFrame({ 102 | 'feature1': np.random.normal(10, 2, 100), 103 | 'feature2': np.random.normal(5, 1, 100), 104 | 'feature3': np.random.normal(0, 0.5, 100), 105 | 'target': np.random.choice([0, 1], 100) 106 | }) 107 | fig = plt.figure(figsize=(8, 6)) 108 | ax = fig.add_subplot(111, projection='3d') 109 | for target in [0, 1]: 110 | subset = ml_data[ml_data['target'] == target] 111 | ax.scatter(subset['feature1'], subset['feature2'], subset['feature3'], 112 | label=f'Class {target}', alpha=0.6) 113 | ax.set_xlabel('Feature1') 114 | ax.set_ylabel('Feature2') 115 | ax.set_zlabel('Feature3') 116 | ax.set_title('3D ML Feature Visualization') 117 | ax.legend() 118 | plt.savefig('ml_3d_features.png') 119 | plt.close() 120 | print("ML 3D features saved as 'ml_3d_features.png'") 121 | 122 | # %% [7. Interview Scenario: 3D Visualizations] 123 | # Discuss 3D visualizations for ML. 124 | print("\nInterview Scenario: 3D Visualizations") 125 | print("Q: How would you visualize high-dimensional ML data in Matplotlib?") 126 | print("A: Use PCA to reduce dimensions and plot a 3D scatter with Axes3D.") 127 | print("Key: 3D plots reveal data structure; PCA simplifies visualization.") 128 | print("Example: ax = fig.add_subplot(111, projection='3d'); ax.scatter(x, y, z)") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/02 Intermediate Matplotlib Concepts/02 ML Evaluation Plots/ml_evaluation_plots.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import make_classification 6 | from sklearn.model_selection import train_test_split 7 | from sklearn.linear_model import LogisticRegression 8 | from sklearn.metrics import roc_curve, RocCurveDisplay, confusion_matrix 9 | import seaborn as sns 10 | except ImportError: 11 | make_classification, train_test_split, LogisticRegression, roc_curve, RocCurveDisplay, confusion_matrix, sns = None, None, None, None, None, None, None 12 | 13 | # %% [1. Introduction to ML Evaluation Plots] 14 | # Learn how to visualize ML model performance with Matplotlib. 15 | # Covers accuracy/loss curves, confusion matrices, and AUC-ROC curves. 16 | 17 | print("Matplotlib version:", plt.matplotlib.__version__) 18 | 19 | # %% [2. Accuracy and Loss Curves] 20 | # Simulate training metrics. 21 | np.random.seed(42) 22 | epochs = np.arange(1, 11) 23 | train_acc = 0.6 + 0.04 * np.arange(10) + np.random.normal(0, 0.01, 10) 24 | val_acc = 0.55 + 0.035 * np.arange(10) + np.random.normal(0, 0.015, 10) 25 | plt.plot(epochs, train_acc, label='Training Accuracy', color='blue', marker='o') 26 | plt.plot(epochs, val_acc, label='Validation Accuracy', color='red', marker='s') 27 | plt.title('Training and Validation Accuracy', fontsize=14) 28 | plt.xlabel('Epoch', fontsize=12) 29 | plt.ylabel('Accuracy', fontsize=12) 30 | plt.legend() 31 | plt.grid(True) 32 | plt.savefig('accuracy_curves.png') 33 | plt.close() 34 | print("\nAccuracy curves saved as 'accuracy_curves.png'") 35 | 36 | # %% [3. Confusion Matrix] 37 | # Create a confusion matrix for a synthetic classifier. 38 | if make_classification: 39 | X, y = make_classification(n_samples=1000, n_classes=2, random_state=42) 40 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) 41 | model = LogisticRegression().fit(X_train, y_train) 42 | y_pred = model.predict(X_test) 43 | cm = confusion_matrix(y_test, y_pred) 44 | else: 45 | np.random.seed(42) 46 | cm = np.array([[50, 10], [15, 25]]) 47 | if sns: 48 | plt.figure(figsize=(6, 5)) 49 | sns.heatmap(cm, annot=True, fmt='d', cmap='Blues') 50 | plt.title('Confusion Matrix') 51 | plt.xlabel('Predicted') 52 | plt.ylabel('True') 53 | plt.savefig('confusion_matrix.png') 54 | else: 55 | plt.imshow(cm, cmap='Blues') 56 | plt.title('Confusion Matrix') 57 | plt.xlabel('Predicted') 58 | plt.ylabel('True') 59 | plt.colorbar() 60 | plt.savefig('confusion_matrix.png') 61 | plt.close() 62 | print("Confusion matrix saved as 'confusion_matrix.png'") 63 | 64 | # %% [4. AUC-ROC Curve] 65 | # Plot an AUC-ROC curve. 66 | if make_classification and RocCurveDisplay: 67 | y_score = model.predict_proba(X_test)[:, 1] 68 | fpr, tpr, _ = roc_curve(y_test, y_score) 69 | display = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=0.5 * (tpr[-1] + 1)) 70 | display.plot() 71 | plt.title('AUC-ROC Curve', fontsize=14) 72 | plt.savefig('roc_curve.png') 73 | else: 74 | np.random.seed(42) 75 | fpr = np.linspace(0, 1, 100) 76 | tpr = np.sqrt(fpr) # Synthetic ROC 77 | plt.plot(fpr, tpr, label='ROC Curve', color='purple') 78 | plt.plot([0, 1], [0, 1], 'k--', label='Random') 79 | plt.title('Synthetic AUC-ROC Curve') 80 | plt.xlabel('False Positive Rate') 81 | plt.ylabel('True Positive Rate') 82 | plt.legend() 83 | plt.savefig('roc_curve.png') 84 | plt.close() 85 | print("AUC-ROC curve saved as 'roc_curve.png'") 86 | 87 | # %% [5. Practical ML Application] 88 | # Visualize multiple model metrics. 89 | if make_classification: 90 | models = {'Logistic Regression': LogisticRegression().fit(X_train, y_train)} 91 | plt.figure(figsize=(8, 6)) 92 | for name, model in models.items(): 93 | y_score = model.predict_proba(X_test)[:, 1] 94 | fpr, tpr, _ = roc_curve(y_test, y_score) 95 | plt.plot(fpr, tpr, label=f'{name} ROC') 96 | plt.plot([0, 1], [0, 1], 'k--', label='Random') 97 | plt.title('Model Comparison: ROC Curves') 98 | plt.xlabel('False Positive Rate') 99 | plt.ylabel('True Positive Rate') 100 | plt.legend() 101 | plt.savefig('model_roc_comparison.png') 102 | else: 103 | plt.figure(figsize=(8, 6)) 104 | plt.plot(fpr, tpr, label='Synthetic ROC', color='blue') 105 | plt.plot([0, 1], [0, 1], 'k--', label='Random') 106 | plt.title('Synthetic Model ROC') 107 | plt.xlabel('False Positive Rate') 108 | plt.ylabel('True Positive Rate') 109 | plt.legend() 110 | plt.savefig('model_roc_comparison.png') 111 | plt.close() 112 | print("Model ROC comparison saved as 'model_roc_comparison.png'") 113 | 114 | # %% [6. Interview Scenario: ML Evaluation] 115 | # Discuss ML evaluation plots. 116 | print("\nInterview Scenario: ML Evaluation") 117 | print("Q: How would you plot an AUC-ROC curve in Matplotlib?") 118 | print("A: Use sklearn.metrics.roc_curve and plot fpr vs. tpr with plt.plot.") 119 | print("Key: AUC-ROC evaluates classifier performance for imbalanced data.") 120 | print("Example: fpr, tpr, _ = roc_curve(y_test, y_score); plt.plot(fpr, tpr)") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/03 Advanced Matplotlib Concepts/04 Custom Visualizations/custom_visualizations.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | try: 5 | from sklearn.datasets import make_classification 6 | from sklearn.linear_model import LogisticRegression 7 | from sklearn.ensemble import RandomForestClassifier 8 | except ImportError: 9 | make_classification, LogisticRegression, RandomForestClassifier = None, None, None 10 | 11 | # %% [1. Introduction to Custom Visualizations] 12 | # Learn how to create custom visualizations for ML insights. 13 | # Covers decision boundaries, feature importance, and custom colormaps. 14 | 15 | print("Matplotlib version:", plt.matplotlib.__version__) 16 | 17 | # %% [2. Plotting Decision Boundaries] 18 | # Visualize decision boundaries for a classifier. 19 | if make_classification: 20 | X, y = make_classification(n_samples=1000, n_features=2, n_classes=2, n_clusters_per_class=1, random_state=42) 21 | model = LogisticRegression().fit(X, y) 22 | x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 23 | y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 24 | xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)) 25 | Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) 26 | Z = Z.reshape(xx.shape) 27 | plt.contourf(xx, yy, Z, alpha=0.3, cmap='coolwarm') 28 | plt.scatter(X[:, 0], X[:, 1], c=y, cmap='coolwarm', edgecolors='k', alpha=0.6) 29 | plt.title('Decision Boundary for Logistic Regression', fontsize=14) 30 | plt.xlabel('Feature1') 31 | plt.ylabel('Feature2') 32 | plt.savefig('decision_boundary.png') 33 | plt.close() 34 | print("\nDecision boundary saved as 'decision_boundary.png'") 35 | else: 36 | print("Scikit-learn unavailable; skipping decision boundary plot.") 37 | 38 | # %% [3. Visualizing Feature Importance] 39 | # Plot feature importance for a random forest. 40 | if make_classification and RandomForestClassifier: 41 | X, y = make_classification(n_features=5, random_state=42) 42 | model = RandomForestClassifier(random_state=42).fit(X, y) 43 | importance = model.feature_importances_ 44 | features = [f'Feature {i+1}' for i in range(5)] 45 | plt.barh(features, importance, color='green', alpha=0.7) 46 | plt.title('Feature Importance from Random Forest', fontsize=14) 47 | plt.xlabel('Importance') 48 | plt.ylabel('Feature') 49 | plt.savefig('feature_importance.png') 50 | plt.close() 51 | print("Feature importance saved as 'feature_importance.png'") 52 | else: 53 | np.random.seed(42) 54 | importance = np.random.rand(5) 55 | features = [f'Feature {i+1}' for i in range(5)] 56 | plt.barh(features, importance, color='green', alpha=0.7) 57 | plt.title('Synthetic Feature Importance', fontsize=14) 58 | plt.xlabel('Importance') 59 | plt.ylabel('Feature') 60 | plt.savefig('feature_importance.png') 61 | plt.close() 62 | print("Synthetic feature importance saved as 'feature_importance.png'") 63 | 64 | # %% [4. Creating Custom Colormaps] 65 | # Create and use a custom colormap. 66 | from matplotlib.colors import LinearSegmentedColormap 67 | np.random.seed(42) 68 | df = pd.DataFrame({ 69 | 'feature1': np.random.normal(10, 2, 100), 70 | 'feature2': np.random.normal(5, 1, 100), 71 | 'value': np.random.rand(100) 72 | }) 73 | colors = ['#FF0000', '#00FF00', '#0000FF'] 74 | cmap = LinearSegmentedColormap.from_list('custom_cmap', colors) 75 | plt.scatter(df['feature1'], df['feature2'], c=df['value'], cmap=cmap, alpha=0.6) 76 | plt.colorbar(label='Value') 77 | plt.title('Scatter with Custom Colormap', fontsize=14) 78 | plt.xlabel('Feature1') 79 | plt.ylabel('Feature2') 80 | plt.savefig('custom_colormap.png') 81 | plt.close() 82 | print("Custom colormap scatter saved as 'custom_colormap.png'") 83 | 84 | # %% [5. Practical ML Application] 85 | # Combine decision boundary and feature importance. 86 | if make_classification: 87 | X, y = make_classification(n_samples=1000, n_features=2, n_classes=2, random_state=42) 88 | model = RandomForestClassifier(random_state=42).fit(X, y) 89 | x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 90 | y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 91 | xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)) 92 | Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) 93 | Z = Z.reshape(xx.shape) 94 | plt.contourf(xx, yy, Z, alpha=0.3, cmap='viridis') 95 | plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis', edgecolors='k', alpha=0.6) 96 | plt.title('Random Forest Decision Boundary', fontsize=14) 97 | plt.xlabel('Feature1') 98 | plt.ylabel('Feature2') 99 | plt.savefig('ml_decision_boundary.png') 100 | plt.close() 101 | print("ML decision boundary saved as 'ml_decision_boundary.png'") 102 | else: 103 | print("Scikit-learn unavailable; skipping ML decision boundary plot.") 104 | 105 | # %% [6. Interview Scenario: Custom Visualizations] 106 | # Discuss custom visualizations for ML. 107 | print("\nInterview Scenario: Custom Visualizations") 108 | print("Q: How would you visualize a classifier’s decision boundary?") 109 | print("A: Use plt.contourf with a meshgrid and model predictions.") 110 | print("Key: Decision boundaries show how models separate classes.") 111 | print("Example: plt.contourf(xx, yy, Z, cmap='coolwarm')") -------------------------------------------------------------------------------- /Matplotlib Fundamentals/03 Advanced Matplotlib Concepts/README.md: -------------------------------------------------------------------------------- 1 | # 🌐 Advanced Matplotlib Concepts (`matplotlib`) 2 | 3 | ## 📖 Introduction 4 | Matplotlib is a versatile tool for creating complex visualizations in AI and machine learning (ML), enabling dynamic and optimized displays of high-dimensional data and model insights. This section explores **3D Visualizations**, **Animations and Interactive Plots**, **Optimization for Large Datasets**, and **Custom Visualizations**, building on beginner and intermediate Matplotlib skills (e.g., basic plotting, subplots, ROC curves). With practical examples and interview insights, it prepares you for advanced ML workflows and data science interviews, complementing Pandas and NumPy skills. 5 | 6 | ## 🎯 Learning Objectives 7 | - Create 3D visualizations for high-dimensional ML data. 8 | - Develop animations and interactive plots for dynamic ML processes. 9 | - Optimize Matplotlib for large datasets and performance. 10 | - Design custom visualizations like decision boundaries and feature importance plots. 11 | 12 | ## 🔑 Key Concepts 13 | - **3D Visualizations**: 14 | - 3D scatter and surface plots (`Axes3D`). 15 | - Visualizing high-dimensional ML data projections (e.g., PCA). 16 | - Customizing 3D axes and viewpoints (`view_init`). 17 | - **Animations and Interactive Plots**: 18 | - Creating animations (`FuncAnimation`) for dynamic ML processes (e.g., training). 19 | - Interactive plots with `mplcursors` or Plotly integration. 20 | - Embedding plots in GUI applications (e.g., Tkinter). 21 | - **Optimization for Large Datasets**: 22 | - Downsampling data for faster plotting. 23 | - Using `plt.plot` with sparse data. 24 | - Leveraging `blitting` for animation performance. 25 | - **Custom Visualizations**: 26 | - Plotting decision boundaries for classifiers (`plt.contourf`). 27 | - Visualizing feature importance (`plt.barh`). 28 | - Creating custom colormaps and styles (`plt.cm`). 29 | 30 | ## 📝 Example Walkthroughs 31 | The following Python files demonstrate each subsection: 32 | 33 | 1. **`3d_visualizations.py`**: 34 | - Creates a 3D scatter plot of Iris features (`Axes3D`). 35 | - Generates a 3D surface plot for synthetic data. 36 | - Visualizes PCA projections in 3D. 37 | - Customizes 3D viewpoints and visualizes ML features. 38 | 39 | Example code: 40 | ```python 41 | from mpl_toolkits.mplot3d import Axes3D 42 | fig = plt.figure() 43 | ax = fig.add_subplot(111, projection='3d') 44 | ax.scatter(x, y, z, c=labels, cmap='viridis') 45 | ``` 46 | 47 | 2. **`animations_interactive_plots.py`**: 48 | - Animates a loss curve with `FuncAnimation`. 49 | - Creates an interactive scatter plot with `mplcursors`. 50 | - Embeds a plot in a Tkinter GUI (commented). 51 | - Animates ML training accuracy curves. 52 | 53 | Example code: 54 | ```python 55 | from matplotlib.animation import FuncAnimation 56 | ani = FuncAnimation(fig, update, frames=range(20), interval=200, blit=True) 57 | ``` 58 | 59 | 3. **`optimization_large_datasets.py`**: 60 | - Downsamples a large dataset for scatter plotting. 61 | - Plots sparse data efficiently with `plt.plot`. 62 | - Uses blitting for optimized animation performance. 63 | - Visualizes downsampled ML features. 64 | 65 | Example code: 66 | ```python 67 | x_down = x[::100] 68 | plt.scatter(x_down, y_down, s=10) 69 | ``` 70 | 71 | 4. **`custom_visualizations.py`**: 72 | - Plots decision boundaries for a classifier (`plt.contourf`). 73 | - Visualizes feature importance with `plt.barh`. 74 | - Creates a custom colormap for a scatter plot. 75 | - Combines decision boundaries for ML models. 76 | 77 | Example code: 78 | ```python 79 | plt.contourf(xx, yy, Z, cmap='coolwarm') 80 | plt.barh(features, importance, color='green') 81 | ``` 82 | 83 | ## 🛠️ Practical Tasks 84 | 1. **3D Visualizations**: 85 | - Create a 3D scatter plot of PCA-transformed features. 86 | - Generate a 3D surface plot for a mathematical function. 87 | - Customize the viewpoint of a 3D plot (`view_init`). 88 | 2. **Animations and Interactive Plots**: 89 | - Animate a training loss curve with `FuncAnimation`. 90 | - Create an interactive scatter plot with hover annotations. 91 | - Embed a Matplotlib plot in a Tkinter GUI. 92 | 3. **Optimization for Large Datasets**: 93 | - Downsample a large dataset for a scatter plot. 94 | - Plot a sparse dataset efficiently. 95 | - Optimize an animation with blitting. 96 | 4. **Custom Visualizations**: 97 | - Plot decision boundaries for a logistic regression model. 98 | - Visualize feature importance for a random forest. 99 | - Create a custom colormap for a scatter plot. 100 | 101 | ## 💡 Interview Tips 102 | - **Common Questions**: 103 | - How do you create a 3D scatter plot in Matplotlib? 104 | - How would you animate a training process for an ML model? 105 | - How do you optimize Matplotlib for large datasets? 106 | - How do you visualize a classifier’s decision boundary? 107 | - **Tips**: 108 | - Explain `Axes3D` for 3D visualizations and `FuncAnimation` for dynamics. 109 | - Highlight downsampling and blitting for performance. 110 | - Be ready to code decision boundaries or feature importance plots. 111 | - **Coding Tasks**: 112 | - Create a 3D PCA plot for a dataset. 113 | - Animate a loss curve over epochs. 114 | - Plot decision boundaries for a classifier. 115 | 116 | ## 📚 Resources 117 | - [Matplotlib 3D Plotting](https://matplotlib.org/stable/gallery/mplot3d/index.html) 118 | - [Matplotlib Animations](https://matplotlib.org/stable/api/animation_api.html) 119 | - [Matplotlib Performance](https://matplotlib.org/stable/users/explain/performance.html) 120 | - [Scikit-learn Visualization](https://scikit-learn.org/stable/visualizations.html) 121 | - [Kaggle: Data Visualization](https://www.kaggle.com/learn/data-visualization) 122 | - [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/) -------------------------------------------------------------------------------- /Matplotlib Fundamentals/01 Beginner Matplotlib Concepts/README.md: -------------------------------------------------------------------------------- 1 | # 🌱 Beginner Matplotlib Concepts (`matplotlib`) 2 | 3 | ## 📖 Introduction 4 | Matplotlib is the foundational Python library for data visualization, critical for exploring datasets and communicating insights in AI and machine learning (ML). This section introduces the fundamentals of Matplotlib, focusing on **Basic Plotting**, **Plot Customization**, **Saving and Displaying Plots**, and **Integration with Pandas**. With practical examples and interview insights, it prepares beginners to visualize ML data effectively, complementing Pandas and NumPy skills. 5 | 6 | ## 🎯 Learning Objectives 7 | - Create basic plots (line, scatter, bar, histogram) for data exploration. 8 | - Customize plots with titles, labels, colors, and figure settings. 9 | - Save plots in various formats (PNG, PDF, SVG) and display them in scripts. 10 | - Integrate Matplotlib with Pandas for DataFrame visualizations. 11 | 12 | ## 🔑 Key Concepts 13 | - **Basic Plotting**: 14 | - Line plots (`plt.plot`) for trends. 15 | - Scatter plots (`plt.scatter`) for relationships. 16 | - Bar plots (`plt.bar`) for comparisons. 17 | - Histograms (`plt.hist`) for distributions. 18 | - **Plot Customization**: 19 | - Setting titles, labels, legends (`plt.title`, `plt.xlabel`, `plt.legend`). 20 | - Customizing colors, markers, line styles. 21 | - Adjusting figure size and resolution (`plt.figure(figsize)`). 22 | - **Saving and Displaying Plots**: 23 | - Saving plots (`plt.savefig`) as PNG, PDF, or SVG. 24 | - Displaying plots (`plt.show`) in Jupyter or scripts. 25 | - **Integration with Pandas**: 26 | - Plotting DataFrame columns (`df.plot`). 27 | - Visualizing grouped data (`df.groupby().plot`). 28 | 29 | ## 📝 Example Walkthroughs 30 | The following Python files demonstrate each subsection: 31 | 32 | 1. **`basic_plotting.py`**: 33 | - Creates a line plot of synthetic sales trends (`plt.plot`). 34 | - Plots a scatter of Iris sepal features (`plt.scatter`). 35 | - Generates a bar plot of category counts (`plt.bar`) and histogram of sepal lengths (`plt.hist`). 36 | - Visualizes ML feature distributions. 37 | 38 | Example code: 39 | ```python 40 | import matplotlib.pyplot as plt 41 | plt.plot(days, sales, color='blue', label='Sales') 42 | plt.hist(df['sepal length (cm)'], bins=20, color='purple') 43 | plt.savefig('plot.png') 44 | ``` 45 | 46 | 2. **`plot_customization.py`**: 47 | - Customizes a scatter plot with titles, labels, and legends (`plt.title`, `plt.legend`). 48 | - Adjusts colors, markers, and line styles in a line plot. 49 | - Sets figure size and resolution (`plt.figure(figsize)`). 50 | - Visualizes ML feature comparisons. 51 | 52 | Example code: 53 | ```python 54 | import matplotlib.pyplot as plt 55 | plt.scatter(df['col1'], df['col2'], color='blue', label='Data') 56 | plt.title('Plot', fontsize=14) 57 | plt.figure(figsize=(10, 6)) 58 | ``` 59 | 60 | 3. **`saving_displaying_plots.py`**: 61 | - Saves a scatter plot as PNG (`plt.savefig`, `dpi=100`). 62 | - Saves a histogram as PDF and a line plot as SVG. 63 | - Demonstrates displaying plots (`plt.show`, commented). 64 | - Saves an ML feature plot for a report. 65 | 66 | Example code: 67 | ```python 68 | import matplotlib.pyplot as plt 69 | plt.scatter(df['col1'], df['col2']) 70 | plt.savefig('plot.png', dpi=150, bbox_inches='tight') 71 | ``` 72 | 73 | 4. **`integration_pandas.py`**: 74 | - Plots DataFrame columns with `df.plot(kind='hist')` and `df.plot(kind='scatter')`. 75 | - Visualizes grouped data by categories (`df.groupby`). 76 | - Creates histograms of ML features by class. 77 | - Saves all plots as PNG. 78 | 79 | Example code: 80 | ```python 81 | import pandas as pd 82 | df['col'].plot(kind='hist', bins=20, color='blue') 83 | df.groupby('category')['col'].plot(kind='hist', alpha=0.5) 84 | ``` 85 | 86 | ## 🛠️ Practical Tasks 87 | 1. **Basic Plotting**: 88 | - Create a line plot of a synthetic time-series (e.g., sales over days). 89 | - Plot a histogram of an Iris feature (e.g., sepal length). 90 | - Generate a scatter plot of two ML features. 91 | 2. **Plot Customization**: 92 | - Customize a scatter plot with a title, labels, and legend. 93 | - Adjust colors and markers in a line plot. 94 | - Create a large figure (10x6 inches) for a histogram. 95 | 3. **Saving and Displaying**: 96 | - Save a bar plot as PNG with high resolution (`dpi=150`). 97 | - Save a line plot as PDF for a report. 98 | - Display a plot in a Jupyter notebook (use `plt.show`). 99 | 4. **Pandas Integration**: 100 | - Plot a DataFrame column’s distribution with `df.plot(kind='hist')`. 101 | - Create a scatter plot of two DataFrame columns. 102 | - Visualize feature distributions by ML class using `groupby`. 103 | 104 | ## 💡 Interview Tips 105 | - **Common Questions**: 106 | - How do you create a histogram in Matplotlib? 107 | - What’s the difference between `plt.plot` and `plt.scatter`? 108 | - How do you save a Matplotlib plot for a report? 109 | - How do you visualize a Pandas DataFrame column? 110 | - **Tips**: 111 | - Explain `plt.hist` for distributions and `plt.scatter` for relationships. 112 | - Highlight `plt.savefig` with `bbox_inches='tight'` to avoid clipping. 113 | - Be ready to code a simple plot (e.g., `plt.plot(x, y, color='blue')`). 114 | - **Coding Tasks**: 115 | - Plot a feature’s distribution from a DataFrame. 116 | - Customize a scatter plot with labels and colors. 117 | - Save a histogram as PNG for a presentation. 118 | 119 | ## 📚 Resources 120 | - [Matplotlib Tutorials](https://matplotlib.org/stable/tutorials/index.html) 121 | - [Matplotlib User Guide](https://matplotlib.org/stable/users/index.html) 122 | - [Matplotlib Plotting Basics](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html) 123 | - [Pandas Visualization](https://pandas.pydata.org/docs/user_guide/visualization.html) 124 | - [Kaggle: Data Visualization](https://www.kaggle.com/learn/data-visualization) 125 | - [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/) -------------------------------------------------------------------------------- /Matplotlib Fundamentals/02 Intermediate Matplotlib Concepts/README.md: -------------------------------------------------------------------------------- 1 | # 🏋️ Intermediate Matplotlib Concepts (`matplotlib`) 2 | 3 | ## 📖 Introduction 4 | Matplotlib is a powerful tool for visualizing AI and machine learning (ML) model performance and data insights. This section deepens your Matplotlib skills with **Subplots and Layouts**, **ML Evaluation Plots**, **Advanced Customization**, and **Data Exploration Visualizations**, building on beginner concepts (e.g., basic plotting, customization). With practical examples and interview insights, it prepares you to create advanced visualizations for ML evaluation and data exploration, complementing Pandas and NumPy skills. 5 | 6 | ## 🎯 Learning Objectives 7 | - Create and manage subplot grids for multi-faceted visualizations. 8 | - Visualize ML model performance with accuracy curves, confusion matrices, and AUC-ROC curves. 9 | - Apply advanced customization like annotations, colormaps, and axis scaling. 10 | - Explore datasets with box plots, pair plots, and correlation heatmaps. 11 | 12 | ## 🔑 Key Concepts 13 | - **Subplots and Layouts**: 14 | - Creating multiple plots (`plt.subplots`) in a grid. 15 | - Adjusting subplot spacing (`plt.tight_layout`). 16 | - Sharing axes for consistent scales. 17 | - **ML Evaluation Plots**: 18 | - Accuracy and loss curves for model training (`plt.plot`). 19 | - Confusion matrices using `seaborn.heatmap` or `plt.imshow`. 20 | - AUC-ROC curves with scikit-learn (`RocCurveDisplay`). 21 | - **Advanced Customization**: 22 | - Annotating plots (`plt.annotate`) for key points. 23 | - Using colormaps (`cmap`) for heatmaps and scatters. 24 | - Customizing axes (`plt.yscale`, `plt.grid`). 25 | - **Data Exploration Visualizations**: 26 | - Box plots (`plt.boxplot`) for outlier detection. 27 | - Pair plots (`seaborn.pairplot`) for feature relationships. 28 | - Correlation heatmaps (`plt.imshow`, `seaborn.heatmap`). 29 | 30 | ## 📝 Example Walkthroughs 31 | The following Python files demonstrate each subsection: 32 | 33 | 1. **`subplots_layouts.py`**: 34 | - Creates a 2x2 subplot grid of Iris feature histograms (`plt.subplots`). 35 | - Adjusts spacing with `plt.tight_layout` for mixed plots (scatter, line). 36 | - Uses shared axes for consistent scales in histograms. 37 | - Visualizes ML feature distributions by class in subplots. 38 | 39 | Example code: 40 | ```python 41 | import matplotlib.pyplot as plt 42 | fig, axes = plt.subplots(2, 2, figsize=(10, 8)) 43 | axes[0, 0].hist(df['col'], bins=20) 44 | plt.tight_layout() 45 | ``` 46 | 47 | 2. **`ml_evaluation_plots.py`**: 48 | - Plots training/validation accuracy curves (`plt.plot`). 49 | - Creates a confusion matrix for a classifier (`seaborn.heatmap` or `plt.imshow`). 50 | - Generates an AUC-ROC curve using scikit-learn (`RocCurveDisplay`). 51 | - Compares ROC curves for multiple models. 52 | 53 | Example code: 54 | ```python 55 | import matplotlib.pyplot as plt 56 | from sklearn.metrics import roc_curve 57 | fpr, tpr, _ = roc_curve(y_test, y_score) 58 | plt.plot(fpr, tpr, label='ROC') 59 | ``` 60 | 61 | 3. **`advanced_customization.py`**: 62 | - Annotates a scatter plot to highlight key points (`plt.annotate`). 63 | - Applies a colormap to a scatter plot (`cmap='viridis'`). 64 | - Customizes axes with log scale and grid (`plt.yscale`, `plt.grid`). 65 | - Visualizes ML features by class with customizations. 66 | 67 | Example code: 68 | ```python 69 | import matplotlib.pyplot as plt 70 | plt.scatter(df['col1'], df['col2'], c=values, cmap='viridis') 71 | plt.annotate('Key', xy=(x, y), arrowprops=dict(facecolor='black')) 72 | ``` 73 | 74 | 4. **`data_exploration_visualizations.py`**: 75 | - Creates box plots for Iris features (`plt.boxplot`). 76 | - Generates pair plots with Seaborn (`seaborn.pairplot`) or fallback scatter. 77 | - Plots a correlation heatmap (`seaborn.heatmap` or `plt.imshow`). 78 | - Visualizes ML feature distributions by class with box plots. 79 | 80 | Example code: 81 | ```python 82 | import matplotlib.pyplot as plt 83 | plt.boxplot([df['col1'], df['col2']], labels=['Col1', 'Col2']) 84 | sns.heatmap(df.corr(), annot=True, cmap='coolwarm') 85 | ``` 86 | 87 | ## 🛠️ Practical Tasks 88 | 1. **Subplots and Layouts**: 89 | - Create a 2x2 subplot grid of feature histograms. 90 | - Plot scatter and line plots with adjusted spacing (`plt.tight_layout`). 91 | - Use shared axes to compare two feature distributions. 92 | 2. **ML Evaluation Plots**: 93 | - Plot training and validation accuracy curves for a model. 94 | - Create a confusion matrix heatmap for a classifier. 95 | - Generate an AUC-ROC curve using scikit-learn. 96 | 3. **Advanced Customization**: 97 | - Annotate a scatter plot to highlight the maximum value. 98 | - Apply a colormap to a scatter plot of ML features. 99 | - Use a log scale for a plot with exponential data. 100 | 4. **Data Exploration Visualizations**: 101 | - Create box plots for all features in a dataset. 102 | - Generate a pair plot to explore feature relationships. 103 | - Plot a correlation heatmap for a DataFrame. 104 | 105 | ## 💡 Interview Tips 106 | - **Common Questions**: 107 | - How do you create a grid of subplots in Matplotlib? 108 | - How would you plot an AUC-ROC curve for a classifier? 109 | - What’s the benefit of using a colormap in a scatter plot? 110 | - How do you visualize feature correlations in a dataset? 111 | - **Tips**: 112 | - Explain `plt.subplots` for organizing multiple plots. 113 | - Highlight AUC-ROC for evaluating imbalanced classifiers. 114 | - Be ready to code a confusion matrix or pair plot. 115 | - **Coding Tasks**: 116 | - Create a subplot grid of feature distributions. 117 | - Plot a ROC curve for a binary classifier. 118 | - Generate a correlation heatmap for ML features. 119 | 120 | ## 📚 Resources 121 | - [Matplotlib Subplots](https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html) 122 | - [Scikit-learn Visualization](https://scikit-learn.org/stable/visualizations.html) 123 | - [Seaborn Documentation](https://seaborn.pydata.org/) 124 | - [Matplotlib Colormaps](https://matplotlib.org/stable/tutorials/colors/colormaps.html) 125 | - [Kaggle: Data Visualization](https://www.kaggle.com/learn/data-visualization) 126 | - [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🚀 Matplotlib for AI/ML Roadmap 2 | 3 | ## 📖 Introduction 4 | Matplotlib is the cornerstone Python library for data visualization, essential for exploring datasets and evaluating AI and machine learning (ML) models. Integrated with Pandas, NumPy, and ML frameworks like scikit-learn, TensorFlow, and PyTorch, it enables clear and customizable plots for data analysis and model performance. This roadmap provides a structured path to master Matplotlib for AI/ML, from basic plotting to advanced visualizations like AUC-ROC curves and confusion matrices, with a focus on practical applications and interview preparation. 5 | 6 | ## 🎯 Learning Objectives 7 | - **Master Basic Plotting**: Create and customize line, scatter, bar, and histogram plots for data exploration. 8 | - **Visualize ML Metrics**: Plot accuracy, AUC-ROC curves, confusion matrices, and loss curves for model evaluation. 9 | - **Apply Advanced Techniques**: Build complex visualizations like 3D plots, animations, and interactive dashboards. 10 | - **Prepare for Interviews**: Gain hands-on experience with ML visualizations and insights for data science roles. 11 | 12 | ## 🛠️ Prerequisites 13 | - **Python**: Familiarity with Python programming (lists, functions, loops). 14 | - **NumPy and Pandas**: Basic understanding of arrays (`np.array`) and DataFrames (`pd.DataFrame`). 15 | - **ML Concepts**: Optional knowledge of classification, regression, and evaluation metrics (e.g., AUC-ROC, accuracy). 16 | - **Development Environment**: Install Matplotlib (`pip install matplotlib`), NumPy (`pip install numpy`), Pandas (`pip install pandas`), and optional ML libraries (e.g., scikit-learn, TensorFlow). 17 | 18 | ## 📈 Matplotlib for AI/ML Learning Roadmap 19 | 20 | ### 🌱 Beginner Matplotlib Concepts 21 | Start with the fundamentals of Matplotlib for data visualization. 22 | 23 | - **Basic Plotting** 24 | - Line plots (`plt.plot`) for trends. 25 | - Scatter plots (`plt.scatter`) for relationships. 26 | - Bar plots (`plt.bar`) for comparisons. 27 | - Histograms (`plt.hist`) for distributions. 28 | - **Plot Customization** 29 | - Setting titles, labels, and legends (`plt.title`, `plt.xlabel`, `plt.legend`). 30 | - Customizing colors, markers, and line styles. 31 | - Adjusting figure size and resolution (`plt.figure(figsize)`). 32 | - **Saving and Displaying Plots** 33 | - Saving plots (`plt.savefig`) as PNG, PDF, or SVG. 34 | - Displaying plots (`plt.show`) in Jupyter or scripts. 35 | - **Integration with Pandas** 36 | - Plotting DataFrame columns (`df.plot`). 37 | - Visualizing grouped data (`df.groupby().plot`). 38 | 39 | **Practical Tasks**: 40 | - Create a line plot of a time-series dataset (e.g., synthetic sales data). 41 | - Plot a histogram of a feature from the Iris dataset. 42 | - Customize a scatter plot with colors and labels for two ML features. 43 | - Save a bar plot of category counts as a PNG file. 44 | 45 | **Resources**: 46 | - [Matplotlib Tutorials](https://matplotlib.org/stable/tutorials/index.html) 47 | - [Matplotlib User Guide](https://matplotlib.org/stable/users/index.html) 48 | - [Matplotlib Plotting Basics](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html) 49 | 50 | ### 🏋️ Intermediate Matplotlib Concepts 51 | Deepen your skills with advanced visualizations for ML evaluation. 52 | 53 | - **Subplots and Layouts** 54 | - Creating multiple plots (`plt.subplots`) in a grid. 55 | - Adjusting subplot spacing (`plt.tight_layout`). 56 | - Sharing axes for consistent scales. 57 | - **ML Evaluation Plots** 58 | - Accuracy and loss curves for model training (`plt.plot`). 59 | - Confusion matrices using `seaborn.heatmap` or `plt.imshow`. 60 | - AUC-ROC curves with scikit-learn (`RocCurveDisplay`). 61 | - **Advanced Customization** 62 | - Annotating plots (`plt.annotate`) for key points. 63 | - Using colormaps (`cmap`) for heatmaps and scatters. 64 | - Customizing axes (`plt.yscale`, `plt.grid`). 65 | - **Data Exploration Visualizations** 66 | - Box plots (`plt.boxplot`) for outlier detection. 67 | - Pair plots (`seaborn.pairplot`) for feature relationships. 68 | - Correlation heatmaps (`plt.imshow`, `seaborn.heatmap`). 69 | 70 | **Practical Tasks**: 71 | - Create a 2x2 subplot grid with histograms of four features. 72 | - Plot an AUC-ROC curve for a binary classifier using scikit-learn. 73 | - Visualize a confusion matrix for a classification model. 74 | - Generate a correlation heatmap for a Pandas DataFrame. 75 | 76 | **Resources**: 77 | - [Matplotlib Subplots](https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html) 78 | - [Scikit-learn Visualization](https://scikit-learn.org/stable/visualizations.html) 79 | - [Seaborn Documentation](https://seaborn.pydata.org/) 80 | 81 | ### 🌐 Advanced Matplotlib Concepts 82 | Tackle complex visualizations and optimization for large-scale ML workflows. 83 | 84 | - **3D Visualizations** 85 | - 3D scatter and surface plots (`Axes3D`). 86 | - Visualizing high-dimensional ML data projections. 87 | - Customizing 3D axes and viewpoints. 88 | - **Animations and Interactive Plots** 89 | - Creating animations (`FuncAnimation`) for dynamic ML processes (e.g., training). 90 | - Interactive plots with `mplcursors` or Plotly integration. 91 | - Embedding plots in GUI applications (e.g., Tkinter). 92 | - **Optimization for Large Datasets** 93 | - Downsampling data for faster plotting. 94 | - Using `plt.plot` with sparse data. 95 | - Leveraging `blitting` for animation performance. 96 | - **Custom Visualizations** 97 | - Plotting decision boundaries for classifiers. 98 | - Visualizing feature importance (`plt.barh`). 99 | - Creating custom colormaps and styles (`plt.cm`). 100 | 101 | **Practical Tasks**: 102 | - Create a 3D scatter plot of PCA-transformed ML features. 103 | - Animate a loss curve over training epochs using `FuncAnimation`. 104 | - Plot decision boundaries for a logistic regression model. 105 | - Optimize a scatter plot for a large dataset (>100,000 points). 106 | 107 | **Resources**: 108 | - [Matplotlib 3D Plotting](https://matplotlib.org/stable/gallery/mplot3d/index.html) 109 | - [Matplotlib Animations](https://matplotlib.org/stable/api/animation_api.html) 110 | - [Plotly for Interactive Plots](https://plotly.com/python/) 111 | 112 | ### 🧬 Matplotlib in AI/ML Applications 113 | Apply Matplotlib to real-world ML tasks and pipelines. 114 | 115 | - **Model Evaluation** 116 | - Plotting precision-recall curves (`PrecisionRecallDisplay`). 117 | - Visualizing training vs. validation metrics (`plt.plot`). 118 | - Comparing multiple model ROC curves in one plot. 119 | - **Feature Analysis** 120 | - Visualizing feature distributions across classes (`plt.hist`). 121 | - Plotting feature importance for tree-based models (`plt.barh`). 122 | - Scatter plots of t-SNE or PCA embeddings. 123 | - **Data Preprocessing Insights** 124 | - Visualizing missing data patterns (`seaborn.heatmap`). 125 | - Plotting outlier distributions (`plt.boxplot`). 126 | - Comparing pre- and post-normalized features (`plt.hist`). 127 | - **Pipeline Integration** 128 | - Embedding plots in ML reports (`plt.savefig`). 129 | - Automating visualization in scikit-learn pipelines. 130 | - Exporting plots for dashboards or presentations. 131 | 132 | **Practical Tasks**: 133 | - Plot ROC curves for three different classifiers on the same figure. 134 | - Visualize feature importance for a random forest model. 135 | - Create a pair plot of features colored by class labels. 136 | - Automate a pipeline to save a confusion matrix plot. 137 | 138 | **Resources**: 139 | - [Scikit-learn Metrics Visualization](https://scikit-learn.org/stable/modules/model_evaluation.html#visualizations) 140 | - [Matplotlib for Data Science](https://matplotlib.org/stable/gallery/index.html) 141 | - [Kaggle: Data Visualization](https://www.kaggle.com/learn/data-visualization) 142 | 143 | ### 📦 Optimization and Best Practices 144 | Optimize Matplotlib for production ML workflows and clarity. 145 | 146 | - **Performance Optimization** 147 | - Reducing plot rendering time for large datasets (`downsample`). 148 | - Using `Agg` backend for non-interactive scripts (`matplotlib.use('Agg')`). 149 | - Caching plot components for repeated visualizations. 150 | - **Code Efficiency** 151 | - Structuring reusable plotting functions. 152 | - Using style sheets (`plt.style.use`) for consistent aesthetics. 153 | - Avoiding redundant plot commands (`plt.clf`). 154 | - **Production Integration** 155 | - Saving high-resolution plots for reports (`dpi=300`). 156 | - Embedding plots in web apps (e.g., Flask, Streamlit). 157 | - Automating plot generation in ML pipelines. 158 | - **Clarity and Accessibility** 159 | - Choosing colorblind-friendly colormaps (`viridis`, `plasma`). 160 | - Adding clear annotations and legends. 161 | - Ensuring readable font sizes and layouts. 162 | 163 | **Practical Tasks**: 164 | - Optimize a scatter plot for a large dataset with downsampling. 165 | - Create a reusable function to plot ROC curves for any classifier. 166 | - Save a high-resolution plot for a presentation (`dpi=300`). 167 | - Use a colorblind-friendly colormap for a heatmap. 168 | 169 | **Resources**: 170 | - [Matplotlib Performance](https://matplotlib.org/stable/users/explain/performance.html) 171 | - [Matplotlib Style Sheets](https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html) 172 | - [Matplotlib Backends](https://matplotlib.org/stable/users/explain/backends.html) 173 | 174 | ## 💡 Learning Tips 175 | - **Hands-On Practice**: Code each section’s tasks in a Jupyter notebook. Use datasets like Iris, Titanic, or synthetic data from `np.random`. 176 | - **Experiment**: Modify plot styles, colormaps, or layouts (e.g., try `seaborn` styles) and analyze impacts on clarity. 177 | - **Portfolio Projects**: Build projects like an ML model evaluation dashboard, feature analysis report, or animated training visualization to showcase skills. 178 | - **Community**: Engage with Matplotlib forums, Stack Overflow, and Kaggle for examples and support. 179 | 180 | ## 🛠️ Practical Tasks 181 | 1. **Beginner**: Plot a histogram of a feature and customize its title and colors. 182 | 2. **Intermediate**: Create a subplot with an AUC-ROC curve and confusion matrix. 183 | 3. **Advanced**: Animate a 3D scatter plot of PCA components over iterations. 184 | 4. **ML Applications**: Visualize feature importance and ROC curves for a classifier. 185 | 5. **Optimization**: Optimize a large dataset scatter plot and save as high-resolution PNG. 186 | 187 | ## 💼 Interview Preparation 188 | - **Common Questions**: 189 | - How do you plot an AUC-ROC curve for a classifier? 190 | - What’s the difference between `plt.plot` and `plt.scatter`? 191 | - How would you visualize a confusion matrix in Matplotlib? 192 | - How do you optimize Matplotlib for large datasets? 193 | - **Coding Tasks**: 194 | - Plot a loss curve for a neural network. 195 | - Create a confusion matrix heatmap for a classification model. 196 | - Visualize feature distributions across classes. 197 | - **Tips**: 198 | - Explain the importance of AUC-ROC for imbalanced datasets. 199 | - Highlight Matplotlib’s integration with scikit-learn for metrics. 200 | - Practice debugging common issues (e.g., overlapping labels). 201 | 202 | ## 📚 Resources 203 | - **Official Documentation**: 204 | - [Matplotlib Official Site](https://matplotlib.org/) 205 | - [Matplotlib User Guide](https://matplotlib.org/stable/users/index.html) 206 | - [Matplotlib API Reference](https://matplotlib.org/stable/api/index.html) 207 | - **Tutorials**: 208 | - [Matplotlib Tutorials](https://matplotlib.org/stable/tutorials/index.html) 209 | - [Kaggle: Data Visualization](https://www.kaggle.com/learn/data-visualization) 210 | - [DataCamp: Matplotlib Tutorial](https://www.datacamp.com/community/tutorials/matplotlib) 211 | - **Books**: 212 | - *Python Data Science Handbook* by Jake VanderPlas 213 | - *Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow* by Aurélien Géron 214 | - *Matplotlib for Python Developers* by Aldrin Yim 215 | - **Community**: 216 | - [Matplotlib GitHub](https://github.com/matplotlib/matplotlib) 217 | - [Stack Overflow: Matplotlib Tag](https://stackoverflow.com/questions/tagged/matplotlib) 218 | - [Matplotlib Discourse](https://discourse.matplotlib.org/) 219 | 220 | ## 📅 Suggested Timeline 221 | - **Week 1-2**: Beginner Concepts (Basic Plotting, Customization) 222 | - **Week 3-4**: Intermediate Concepts (Subplots, ML Evaluation Plots) 223 | - **Week 5-6**: Advanced Concepts (3D Plots, Animations) 224 | - **Week 7**: ML Applications and Optimization 225 | - **Week 8**: Portfolio project and interview prep 226 | 227 | ## 🚀 Get Started 228 | Clone this repository and start with the Beginner Concepts section. Run the example code in a Jupyter notebook, experiment with tasks, and build a portfolio project (e.g., an ML evaluation dashboard with AUC-ROC and confusion matrix plots) to showcase your skills. Happy visualizing, and good luck with your AI/ML journey! -------------------------------------------------------------------------------- /Matplotlib Interview Questions/README.md: -------------------------------------------------------------------------------- 1 | # Matplotlib Interview Questions for AI/ML Roles (Data Visualization) 2 | 3 | This README provides 170 Matplotlib interview questions tailored for AI/ML roles, focusing on data visualization for machine learning tasks. The questions cover **core Matplotlib concepts** (e.g., plotting, customization, integration with ML libraries) and their use in visualizing model performance, data distributions, and analysis results. Questions are categorized by topic and divided into **Basic**, **Intermediate**, and **Advanced** levels to support candidates preparing for roles requiring Matplotlib in AI/ML workflows. 4 | 5 | ## Matplotlib Basics 6 | 7 | ### Basic 8 | 1. **What is Matplotlib, and how is it used in AI/ML?** 9 | Matplotlib is a Python library for creating static, animated, and interactive visualizations, used in AI/ML for plotting data and model metrics. 10 | ```python 11 | import matplotlib.pyplot as plt 12 | plt.plot([1, 2, 3], [4, 5, 6]) 13 | plt.savefig('basic_plot.png') 14 | ``` 15 | 16 | 2. **How do you install Matplotlib and its dependencies?** 17 | Installs Matplotlib via pip, typically with NumPy. 18 | ```python 19 | !pip install matplotlib numpy 20 | ``` 21 | 22 | 3. **What is the difference between Matplotlib’s pyplot and object-oriented APIs?** 23 | Pyplot is a simple, MATLAB-like interface; object-oriented API offers more control. 24 | ```python 25 | fig, ax = plt.subplots() 26 | ax.plot([1, 2, 3], [4, 5, 6]) 27 | plt.savefig('oo_plot.png') 28 | ``` 29 | 30 | 4. **How do you create a simple line plot in Matplotlib?** 31 | Plots data points connected by lines for ML trends. 32 | ```python 33 | import matplotlib.pyplot as plt 34 | plt.plot([1, 2, 3], [4, 5, 6], label='Line') 35 | plt.legend() 36 | plt.savefig('line_plot.png') 37 | ``` 38 | 39 | 5. **What is the role of `plt.savefig()` in Matplotlib?** 40 | Saves plots to files for reports or analysis. 41 | ```python 42 | plt.plot([1, 2, 3], [4, 5, 6]) 43 | plt.savefig('saved_plot.png') 44 | ``` 45 | 46 | 6. **How do you set labels and titles in a Matplotlib plot?** 47 | Adds descriptive text for clarity in ML visualizations. 48 | ```python 49 | plt.plot([1, 2, 3], [4, 5, 6]) 50 | plt.xlabel('X-axis') 51 | plt.ylabel('Y-axis') 52 | plt.title('Sample Plot') 53 | plt.savefig('labeled_plot.png') 54 | ``` 55 | 56 | #### Intermediate 57 | 7. **Write a function to create a Matplotlib line plot with multiple lines.** 58 | Visualizes multiple ML model metrics. 59 | ```python 60 | import matplotlib.pyplot as plt 61 | def plot_multiple_lines(x, y_list, labels): 62 | for y, label in zip(y_list, labels): 63 | plt.plot(x, y, label=label) 64 | plt.legend() 65 | plt.savefig('multi_line_plot.png') 66 | ``` 67 | 68 | 8. **How do you customize line styles and colors in Matplotlib?** 69 | Enhances plot readability for ML data. 70 | ```python 71 | plt.plot([1, 2, 3], [4, 5, 6], 'r--', label='Dashed Red') 72 | plt.legend() 73 | plt.savefig('styled_plot.png') 74 | ``` 75 | 76 | 9. **Write a function to save a Matplotlib plot with specific resolution.** 77 | Ensures high-quality ML visualizations. 78 | ```python 79 | def save_high_res_plot(x, y, filename='plot.png', dpi=300): 80 | plt.plot(x, y) 81 | plt.savefig(filename, dpi=dpi) 82 | ``` 83 | 84 | 10. **How do you create a subplot grid in Matplotlib?** 85 | Displays multiple ML metrics side by side. 86 | ```python 87 | fig, (ax1, ax2) = plt.subplots(1, 2) 88 | ax1.plot([1, 2, 3], [4, 5, 6]) 89 | ax2.plot([1, 2, 3], [6, 5, 4]) 90 | plt.savefig('subplot_grid.png') 91 | ``` 92 | 93 | 11. **Write a function to plot data with a logarithmic scale.** 94 | Visualizes ML data with wide ranges. 95 | ```python 96 | import matplotlib.pyplot as plt 97 | def plot_log_scale(x, y): 98 | plt.plot(x, y) 99 | plt.yscale('log') 100 | plt.savefig('log_plot.png') 101 | ``` 102 | 103 | 12. **How do you add a grid to a Matplotlib plot?** 104 | Improves readability of ML visualizations. 105 | ```python 106 | plt.plot([1, 2, 3], [4, 5, 6]) 107 | plt.grid(True) 108 | plt.savefig('grid_plot.png') 109 | ``` 110 | 111 | #### Advanced 112 | 13. **Write a function to create a custom Matplotlib style for ML plots.** 113 | Ensures consistent visualization aesthetics. 114 | ```python 115 | import matplotlib.pyplot as plt 116 | def set_custom_style(): 117 | plt.style.use({ 118 | 'lines.linewidth': 2, 119 | 'axes.grid': True, 120 | 'font.size': 12 121 | }) 122 | plt.plot([1, 2, 3], [4, 5, 6]) 123 | plt.savefig('custom_style_plot.png') 124 | ``` 125 | 126 | 14. **How do you handle large datasets in Matplotlib for efficient plotting?** 127 | Uses sampling or aggregation for ML data. 128 | ```python 129 | import numpy as np 130 | x = np.linspace(0, 10, 10000) 131 | y = np.sin(x) 132 | plt.plot(x[::10], y[::10]) # Downsample 133 | plt.savefig('large_data_plot.png') 134 | ``` 135 | 136 | 15. **Write a function to create an animated line plot in Matplotlib.** 137 | Visualizes ML training dynamics. 138 | ```python 139 | import matplotlib.pyplot as plt 140 | import matplotlib.animation as animation 141 | def animate_line_plot(x, y): 142 | fig, ax = plt.subplots() 143 | line, = ax.plot([], []) 144 | def update(i): 145 | line.set_data(x[:i], y[:i]) 146 | return line, 147 | ani = animation.FuncAnimation(fig, update, frames=len(x), interval=100) 148 | ani.save('animated_plot.gif', writer='pillow') 149 | ``` 150 | 151 | 16. **How do you implement a custom Matplotlib backend for ML workflows?** 152 | Configures rendering for specific environments. 153 | ```python 154 | import matplotlib 155 | matplotlib.use('Agg') # Non-interactive backend 156 | import matplotlib.pyplot as plt 157 | plt.plot([1, 2, 3], [4, 5, 6]) 158 | plt.savefig('backend_plot.png') 159 | ``` 160 | 161 | 17. **Write a function to handle missing data in Matplotlib plots.** 162 | Ensures robust ML visualizations. 163 | ```python 164 | import numpy as np 165 | def plot_with_missing_data(x, y): 166 | mask = ~np.isnan(y) 167 | plt.plot(x[mask], y[mask]) 168 | plt.savefig('missing_data_plot.png') 169 | ``` 170 | 171 | 18. **How do you optimize Matplotlib plots for web display?** 172 | Saves lightweight, high-quality images. 173 | ```python 174 | plt.plot([1, 2, 3], [4, 5, 6]) 175 | plt.savefig('web_plot.png', dpi=100, bbox_inches='tight') 176 | ``` 177 | 178 | ## Plotting Types 179 | 180 | ### Basic 181 | 19. **How do you create a scatter plot in Matplotlib?** 182 | Visualizes ML data points or clusters. 183 | ```python 184 | import matplotlib.pyplot as plt 185 | plt.scatter([1, 2, 3], [4, 5, 6], c='blue', label='Points') 186 | plt.legend() 187 | plt.savefig('scatter_plot.png') 188 | ``` 189 | 190 | 20. **What is a bar plot, and how is it created in Matplotlib?** 191 | Compares categorical ML metrics. 192 | ```python 193 | plt.bar(['A', 'B', 'C'], [10, 20, 15]) 194 | plt.savefig('bar_plot.png') 195 | ``` 196 | 197 | 21. **How do you create a histogram in Matplotlib?** 198 | Visualizes ML data distributions. 199 | ```python 200 | import numpy as np 201 | data = np.random.randn(1000) 202 | plt.hist(data, bins=30) 203 | plt.savefig('histogram.png') 204 | ``` 205 | 206 | 22. **What is a box plot, and how is it used in ML?** 207 | Shows data spread and outliers in ML datasets. 208 | ```python 209 | plt.boxplot([1, 2, 3, 10, 20]) 210 | plt.savefig('box_plot.png') 211 | ``` 212 | 213 | 23. **How do you create a pie chart in Matplotlib?** 214 | Visualizes ML class proportions. 215 | ```python 216 | plt.pie([30, 40, 30], labels=['A', 'B', 'C']) 217 | plt.savefig('pie_chart.png') 218 | ``` 219 | 220 | 24. **How do you plot a heatmap in Matplotlib?** 221 | Visualizes ML correlation matrices. 222 | ```python 223 | import numpy as np 224 | data = np.random.rand(10, 10) 225 | plt.imshow(data, cmap='viridis') 226 | plt.colorbar() 227 | plt.savefig('heatmap.png') 228 | ``` 229 | 230 | #### Intermediate 231 | 25. **Write a function to create a stacked bar plot.** 232 | Compares multiple ML metrics across categories. 233 | ```python 234 | import matplotlib.pyplot as plt 235 | def stacked_bar_plot(categories, data1, data2): 236 | plt.bar(categories, data1, label='Series 1') 237 | plt.bar(categories, data2, bottom=data1, label='Series 2') 238 | plt.legend() 239 | plt.savefig('stacked_bar.png') 240 | ``` 241 | 242 | 26. **How do you create a violin plot in Matplotlib?** 243 | Visualizes ML data distributions with density. 244 | ```python 245 | import seaborn as sns 246 | data = [np.random.randn(100) for _ in range(3)] 247 | plt.violinplot(data) 248 | plt.savefig('violin_plot.png') 249 | ``` 250 | 251 | 27. **Write a function to plot a contour plot for ML model performance.** 252 | Visualizes decision boundaries or loss surfaces. 253 | ```python 254 | import numpy as np 255 | def contour_plot(X, Y, Z): 256 | plt.contourf(X, Y, Z, cmap='viridis') 257 | plt.colorbar() 258 | plt.savefig('contour_plot.png') 259 | ``` 260 | 261 | 28. **How do you create a 3D scatter plot in Matplotlib?** 262 | Visualizes high-dimensional ML data. 263 | ```python 264 | from mpl_toolkits.mplot3d import Axes3D 265 | fig = plt.figure() 266 | ax = fig.add_subplot(111, projection='3d') 267 | ax.scatter([1, 2, 3], [4, 5, 6], [7, 8, 9]) 268 | plt.savefig('3d_scatter.png') 269 | ``` 270 | 271 | 29. **Write a function to create a pair plot for ML features.** 272 | Visualizes feature relationships in datasets. 273 | ```python 274 | import pandas as pd 275 | import seaborn as sns 276 | def pair_plot(df): 277 | sns.pairplot(df) 278 | plt.savefig('pair_plot.png') 279 | ``` 280 | 281 | 30. **How do you plot a time series in Matplotlib?** 282 | Visualizes ML model predictions over time. 283 | ```python 284 | import pandas as pd 285 | dates = pd.date_range('2023-01-01', periods=5) 286 | plt.plot(dates, [1, 2, 3, 4, 5]) 287 | plt.savefig('time_series.png') 288 | ``` 289 | 290 | #### Advanced 291 | 31. **Write a function to create a hexbin plot for large ML datasets.** 292 | Visualizes dense data with hexagonal bins. 293 | ```python 294 | import numpy as np 295 | def hexbin_plot(x, y): 296 | plt.hexbin(x, y, gridsize=50, cmap='Blues') 297 | plt.colorbar() 298 | plt.savefig('hexbin_plot.png') 299 | ``` 300 | 301 | 32. **How do you implement a streamplot in Matplotlib?** 302 | Visualizes ML vector fields or gradients. 303 | ```python 304 | import numpy as np 305 | x, y = np.meshgrid(np.linspace(-3, 3, 20), np.linspace(-3, 3, 20)) 306 | u, v = y, -x 307 | plt.streamplot(x, y, u, v) 308 | plt.savefig('streamplot.png') 309 | ``` 310 | 311 | 33. **Write a function to create a polar plot for ML data.** 312 | Visualizes cyclic or angular data. 313 | ```python 314 | import numpy as np 315 | def polar_plot(theta, r): 316 | ax = plt.subplot(111, projection='polar') 317 | ax.plot(theta, r) 318 | plt.savefig('polar_plot.png') 319 | ``` 320 | 321 | 34. **How do you create a dendrogram for ML clustering?** 322 | Visualizes hierarchical clustering results. 323 | ```python 324 | from scipy.cluster.hierarchy import dendrogram, linkage 325 | import numpy as np 326 | data = np.random.rand(10, 2) 327 | Z = linkage(data, 'ward') 328 | dendrogram(Z) 329 | plt.savefig('dendrogram.png') 330 | ``` 331 | 332 | 35. **Write a function to plot a Sankey diagram for ML workflows.** 333 | Visualizes data flow or model pipelines. 334 | ```python 335 | from matplotlib.sankey import Sankey 336 | def sankey_diagram(): 337 | Sankey(flows=[0.25, 0.15, -0.4], labels=['A', 'B', 'C']).finish() 338 | plt.savefig('sankey.png') 339 | ``` 340 | 341 | 36. **How do you create an interactive plot with Matplotlib for ML exploration?** 342 | Enables dynamic data inspection. 343 | ```python 344 | from matplotlib.widgets import Slider 345 | fig, ax = plt.subplots() 346 | x = np.linspace(0, 10, 100) 347 | line, = ax.plot(x, np.sin(x)) 348 | ax_slider = plt.axes([0.1, 0.01, 0.65, 0.03]) 349 | slider = Slider(ax_slider, 'Freq', 0.1, 10.0, valinit=1) 350 | def update(val): 351 | line.set_ydata(np.sin(slider.val * x)) 352 | fig.canvas.draw_idle() 353 | slider.on_changed(update) 354 | plt.savefig('interactive_plot.png') 355 | ``` 356 | 357 | ## Customization and Styling 358 | 359 | ### Basic 360 | 37. **How do you customize Matplotlib plot colors?** 361 | Enhances ML visualization clarity. 362 | ```python 363 | plt.plot([1, 2, 3], [4, 5, 6], color='green') 364 | plt.savefig('color_plot.png') 365 | ``` 366 | 367 | 38. **What is a Matplotlib style sheet, and how is it applied?** 368 | Sets consistent plot aesthetics for ML. 369 | ```python 370 | plt.style.use('ggplot') 371 | plt.plot([1, 2, 3], [4, 5, 6]) 372 | plt.savefig('styled_plot.png') 373 | ``` 374 | 375 | 39. **How do you add annotations to a Matplotlib plot?** 376 | Highlights key ML data points. 377 | ```python 378 | plt.plot([1, 2, 3], [4, 5, 6]) 379 | plt.annotate('Max', xy=(2, 5), xytext=(2.5, 5.5), arrowprops=dict(facecolor='black')) 380 | plt.savefig('annotated_plot.png') 381 | ``` 382 | 383 | 40. **How do you customize Matplotlib axis ticks?** 384 | Improves readability of ML plots. 385 | ```python 386 | plt.plot([1, 2, 3], [4, 5, 6]) 387 | plt.xticks([1, 2, 3], ['A', 'B', 'C']) 388 | plt.savefig('custom_ticks.png') 389 | ``` 390 | 391 | 41. **What is the role of legends in Matplotlib plots?** 392 | Identifies plot elements in ML visualizations. 393 | ```python 394 | plt.plot([1, 2, 3], [4, 5, 6], label='Data') 395 | plt.legend() 396 | plt.savefig('legend_plot.png') 397 | ``` 398 | 399 | 42. **How do you set figure size in Matplotlib?** 400 | Controls plot dimensions for ML reports. 401 | ```python 402 | plt.figure(figsize=(8, 6)) 403 | plt.plot([1, 2, 3], [4, 5, 6]) 404 | plt.savefig('figure_size.png') 405 | ``` 406 | 407 | #### Intermediate 408 | 43. **Write a function to customize Matplotlib axis limits.** 409 | Focuses on relevant ML data ranges. 410 | ```python 411 | def set_axis_limits(x, y, xlims, ylims): 412 | plt.plot(x, y) 413 | plt.xlim(xlims) 414 | plt.ylim(ylims) 415 | plt.savefig('axis_limits.png') 416 | ``` 417 | 418 | 44. **How do you create a twin axis plot in Matplotlib?** 419 | Visualizes multiple ML metrics with different scales. 420 | ```python 421 | fig, ax1 = plt.subplots() 422 | ax1.plot([1, 2, 3], [4, 5, 6], 'b-') 423 | ax2 = ax1.twinx() 424 | ax2.plot([1, 2, 3], [60, 50, 40], 'r-') 425 | plt.savefig('twin_axis.png') 426 | ``` 427 | 428 | 45. **Write a function to add custom text to a Matplotlib plot.** 429 | Labels ML plot features. 430 | ```python 431 | def add_custom_text(x, y, text, position): 432 | plt.plot(x, y) 433 | plt.text(position[0], position[1], text) 434 | plt.savefig('custom_text.png') 435 | ``` 436 | 437 | 46. **How do you customize Matplotlib colorbars?** 438 | Enhances heatmap readability for ML. 439 | ```python 440 | import numpy as np 441 | data = np.random.rand(10, 10) 442 | im = plt.imshow(data, cmap='viridis') 443 | cbar = plt.colorbar(im) 444 | cbar.set_label('Intensity') 445 | plt.savefig('custom_colorbar.png') 446 | ``` 447 | 448 | 47. **Write a function to create a Matplotlib plot with transparent elements.** 449 | Improves ML visualization layering. 450 | ```python 451 | def transparent_plot(x, y): 452 | plt.plot(x, y, alpha=0.5) 453 | plt.savefig('transparent_plot.png') 454 | ``` 455 | 456 | 48. **How do you customize Matplotlib fonts for professional ML reports?** 457 | Sets font styles and sizes. 458 | ```python 459 | plt.rcParams.update({'font.family': 'Arial', 'font.size': 12}) 460 | plt.plot([1, 2, 3], [4, 5, 6]) 461 | plt.savefig('custom_font.png') 462 | ``` 463 | 464 | #### Advanced 465 | 49. **Write a function to create a Matplotlib plot with custom markers.** 466 | Highlights specific ML data points. 467 | ```python 468 | def custom_marker_plot(x, y, marker='*'): 469 | plt.scatter(x, y, marker=marker, s=100) 470 | plt.savefig('custom_marker.png') 471 | ``` 472 | 473 | 50. **How do you implement a dynamic Matplotlib layout for ML dashboards?** 474 | Adjusts plot spacing automatically. 475 | ```python 476 | fig, axs = plt.subplots(2, 2) 477 | for ax in axs.flat: 478 | ax.plot([1, 2, 3], [4, 5, 6]) 479 | plt.tight_layout() 480 | plt.savefig('dynamic_layout.png') 481 | ``` 482 | 483 | 51. **Write a function to create a Matplotlib plot with gradient fills.** 484 | Visualizes ML data trends with color gradients. 485 | ```python 486 | import numpy as np 487 | def gradient_fill_plot(x, y): 488 | plt.plot(x, y) 489 | plt.fill_between(x, y, color='blue', alpha=0.3) 490 | plt.savefig('gradient_fill.png') 491 | ``` 492 | 493 | 52. **How do you create a Matplotlib plot with custom tick formatters?** 494 | Formats axis ticks for ML data. 495 | ```python 496 | from matplotlib.ticker import FuncFormatter 497 | def custom_formatter(x, _): 498 | return f'${x:.2f}' 499 | plt.plot([1, 2, 3], [4, 5, 6]) 500 | plt.gca().yaxis.set_major_formatter(FuncFormatter(custom_formatter)) 501 | plt.savefig('custom_ticks.png') 502 | ``` 503 | 504 | 53. **Write a function to create a Matplotlib plot with inset axes.** 505 | Zooms in on ML data details. 506 | ```python 507 | from mpl_toolkits.axes_grid1.inset_locator import inset_axes 508 | def inset_plot(x, y): 509 | fig, ax = plt.subplots() 510 | ax.plot(x, y) 511 | axins = inset_axes(ax, width=1.5, height=1.5, loc='upper right') 512 | axins.plot(x, y) 513 | plt.savefig('inset_plot.png') 514 | ``` 515 | 516 | 54. **How do you implement a Matplotlib theme for consistent ML visualizations?** 517 | Defines reusable plot styles. 518 | ```python 519 | import matplotlib.pyplot as plt 520 | def apply_ml_theme(): 521 | plt.rcParams.update({ 522 | 'axes.facecolor': '#f0f0f0', 523 | 'grid.color': 'white', 524 | 'grid.linestyle': '-' 525 | }) 526 | plt.plot([1, 2, 3], [4, 5, 6]) 527 | plt.savefig('theme_plot.png') 528 | ``` 529 | 530 | ## Integration with ML Libraries 531 | 532 | ### Basic 533 | 55. **How do you use Matplotlib with NumPy for ML data visualization?** 534 | Plots NumPy arrays for model inputs/outputs. 535 | ```python 536 | import numpy as np 537 | x = np.linspace(0, 10, 100) 538 | y = np.sin(x) 539 | plt.plot(x, y) 540 | plt.savefig('numpy_plot.png') 541 | ``` 542 | 543 | 56. **What is the role of Pandas in Matplotlib visualizations?** 544 | Simplifies plotting ML datasets. 545 | ```python 546 | import pandas as pd 547 | df = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) 548 | plt.plot(df['x'], df['y']) 549 | plt.savefig('pandas_plot.png') 550 | ``` 551 | 552 | 57. **How do you plot Scikit-learn model performance with Matplotlib?** 553 | Visualizes metrics like accuracy or loss. 554 | ```python 555 | from sklearn.linear_model import LogisticRegression 556 | import numpy as np 557 | X = np.random.rand(100, 1) 558 | y = (X > 0.5).ravel() 559 | model = LogisticRegression().fit(X, y) 560 | plt.plot(X, model.predict_proba(X)[:, 1]) 561 | plt.savefig('sklearn_plot.png') 562 | ``` 563 | 564 | 58. **How do you visualize a confusion matrix with Matplotlib?** 565 | Evaluates ML classification performance. 566 | ```python 567 | from sklearn.metrics import confusion_matrix 568 | import numpy as np 569 | y_true = [0, 1, 1, 0] 570 | y_pred = [0, 1, 0, 0] 571 | cm = confusion_matrix(y_true, y_pred) 572 | plt.imshow(cm, cmap='Blues') 573 | plt.colorbar() 574 | plt.savefig('confusion_matrix.png') 575 | ``` 576 | 577 | 59. **What is Seaborn, and how does it integrate with Matplotlib?** 578 | Enhances Matplotlib for ML visualizations. 579 | ```python 580 | import seaborn as sns 581 | import numpy as np 582 | data = np.random.randn(100) 583 | sns.histplot(data) 584 | plt.savefig('seaborn_plot.png') 585 | ``` 586 | 587 | 60. **How do you plot ML model training history with Matplotlib?** 588 | Visualizes loss and accuracy curves. 589 | ```python 590 | history = {'loss': [0.5, 0.3, 0.2], 'accuracy': [0.8, 0.85, 0.9]} 591 | plt.plot(history['loss'], label='Loss') 592 | plt.plot(history['accuracy'], label='Accuracy') 593 | plt.legend() 594 | plt.savefig('training_history.png') 595 | ``` 596 | 597 | #### Intermediate 598 | 61. **Write a function to plot a ROC curve using Matplotlib and Scikit-learn.** 599 | Evaluates ML classifier performance. 600 | ```python 601 | from sklearn.metrics import roc_curve 602 | import numpy as np 603 | def plot_roc_curve(y_true, y_scores): 604 | fpr, tpr, _ = roc_curve(y_true, y_scores) 605 | plt.plot(fpr, tpr) 606 | plt.plot([0, 1], [0, 1], 'k--') 607 | plt.savefig('roc_curve.png') 608 | ``` 609 | 610 | 62. **How do you visualize Pandas DataFrame correlations with Matplotlib?** 611 | Plots feature relationships for ML. 612 | ```python 613 | import pandas as pd 614 | df = pd.DataFrame(np.random.rand(10, 3), columns=['A', 'B', 'C']) 615 | corr = df.corr() 616 | plt.imshow(corr, cmap='coolwarm') 617 | plt.colorbar() 618 | plt.savefig('correlation_plot.png') 619 | ``` 620 | 621 | 63. **Write a function to plot ML feature importance with Matplotlib.** 622 | Visualizes model feature contributions. 623 | ```python 624 | def plot_feature_importance(features, importances): 625 | plt.bar(features, importances) 626 | plt.xticks(rotation=45) 627 | plt.savefig('feature_importance.png') 628 | ``` 629 | 630 | 64. **How do you integrate Matplotlib with TensorFlow for ML visualizations?** 631 | Plots training metrics from TensorFlow models. 632 | ```python 633 | import tensorflow as tf 634 | history = tf.keras.Sequential().fit(np.random.rand(100, 10), np.random.randint(0, 2, 100), epochs=3).history 635 | plt.plot(history['loss']) 636 | plt.savefig('tensorflow_loss.png') 637 | ``` 638 | 639 | 65. **Write a function to visualize ML decision boundaries with Matplotlib.** 640 | Shows classifier behavior. 641 | ```python 642 | from sklearn.svm import SVC 643 | import numpy as np 644 | def plot_decision_boundary(X, y): 645 | model = SVC().fit(X, y) 646 | h = .02 647 | x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 648 | y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 649 | xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) 650 | Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape) 651 | plt.contourf(xx, yy, Z, cmap='viridis', alpha=0.8) 652 | plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k') 653 | plt.savefig('decision_boundary.png') 654 | ``` 655 | 656 | 66. **How do you plot ML cross-validation results with Matplotlib?** 657 | Visualizes model stability. 658 | ```python 659 | from sklearn.model_selection import cross_val_score 660 | from sklearn.linear_model import LogisticRegression 661 | scores = cross_val_score(LogisticRegression(), np.random.rand(100, 5), np.random.randint(0, 2, 100), cv=5) 662 | plt.plot(range(1, 6), scores, marker='o') 663 | plt.savefig('cross_val_scores.png') 664 | ``` 665 | 666 | #### Advanced 667 | 67. **Write a function to visualize ML model predictions with uncertainty.** 668 | Plots prediction intervals for regression. 669 | ```python 670 | import numpy as np 671 | def plot_with_uncertainty(x, y, y_std): 672 | plt.plot(x, y, label='Prediction') 673 | plt.fill_between(x, y - y_std, y + y_std, alpha=0.3, label='Uncertainty') 674 | plt.legend() 675 | plt.savefig('uncertainty_plot.png') 676 | ``` 677 | 678 | 68. **How do you integrate Matplotlib with PyTorch for ML visualizations?** 679 | Plots training metrics from PyTorch models. 680 | ```python 681 | import torch 682 | import torch.nn as nn 683 | model = nn.Linear(10, 1) 684 | losses = [] 685 | for _ in range(3): 686 | loss = nn.MSELoss()(model(torch.randn(100, 10)), torch.randn(100, 1)) 687 | losses.append(loss.item()) 688 | plt.plot(losses) 689 | plt.savefig('pytorch_loss.png') 690 | ``` 691 | 692 | 69. **Write a function to plot a learning curve for ML model evaluation.** 693 | Visualizes model performance vs. data size. 694 | ```python 695 | from sklearn.model_selection import learning_curve 696 | from sklearn.linear_model import LogisticRegression 697 | import numpy as np 698 | def plot_learning_curve(X, y): 699 | train_sizes, train_scores, test_scores = learning_curve(LogisticRegression(), X, y, cv=5) 700 | plt.plot(train_sizes, train_scores.mean(axis=1), label='Train') 701 | plt.plot(train_sizes, test_scores.mean(axis=1), label='Test') 702 | plt.legend() 703 | plt.savefig('learning_curve.png') 704 | ``` 705 | 706 | 70. **How do you visualize high-dimensional ML data with Matplotlib?** 707 | Uses PCA for dimensionality reduction. 708 | ```python 709 | from sklearn.decomposition import PCA 710 | import numpy as np 711 | X = np.random.rand(100, 10) 712 | pca = PCA(n_components=2) 713 | X_reduced = pca.fit_transform(X) 714 | plt.scatter(X_reduced[:, 0], X_reduced[:, 1]) 715 | plt.savefig('pca_plot.png') 716 | ``` 717 | 718 | 71. **Write a function to plot ML model residuals with Matplotlib.** 719 | Visualizes prediction errors. 720 | ```python 721 | def plot_residuals(y_true, y_pred): 722 | residuals = y_true - y_pred 723 | plt.scatter(y_pred, residuals) 724 | plt.axhline(0, color='r', linestyle='--') 725 | plt.savefig('residuals_plot.png') 726 | ``` 727 | 728 | 72. **How do you create a Matplotlib plot for ML hyperparameter tuning?** 729 | Visualizes grid search results. 730 | ```python 731 | from sklearn.model_selection import GridSearchCV 732 | from sklearn.svm import SVC 733 | param_grid = {'C': [0.1, 1, 10]} 734 | grid = GridSearchCV(SVC(), param_grid, cv=3).fit(np.random.rand(100, 2), np.random.randint(0, 2, 100)) 735 | plt.plot(param_grid['C'], grid.cv_results_['mean_test_score']) 736 | plt.xscale('log') 737 | plt.savefig('grid_search.png') 738 | ``` 739 | 740 | ## Evaluation and Metrics Visualization 741 | 742 | ### Basic 743 | 73. **How do you visualize ML model accuracy with Matplotlib?** 744 | Plots accuracy over epochs or folds. 745 | ```python 746 | accuracies = [0.8, 0.85, 0.9] 747 | plt.plot(accuracies, marker='o') 748 | plt.savefig('accuracy_plot.png') 749 | ``` 750 | 751 | 74. **What is a precision-recall curve, and how is it plotted?** 752 | Evaluates ML classifier performance. 753 | ```python 754 | from sklearn.metrics import precision_recall_curve 755 | precision, recall, _ = precision_recall_curve([0, 1, 1, 0], [0.1, 0.9, 0.8, 0.2]) 756 | plt.plot(recall, precision) 757 | plt.savefig('pr_curve.png') 758 | ``` 759 | 760 | 75. **How do you plot a loss curve for ML model training?** 761 | Visualizes training convergence. 762 | ```python 763 | losses = [0.5, 0.3, 0.2] 764 | plt.plot(losses, label='Loss') 765 | plt.legend() 766 | plt.savefig('loss_curve.png') 767 | ``` 768 | 769 | 76. **How do you visualize ML model AUC with Matplotlib?** 770 | Plots ROC curve with AUC annotation. 771 | ```python 772 | from sklearn.metrics import roc_auc_score 773 | y_true = [0, 1, 1, 0] 774 | y_scores = [0.1, 0.9, 0.8, 0.2] 775 | auc = roc_auc_score(y_true, y_scores) 776 | fpr, tpr, _ = roc_curve(y_true, y_scores) 777 | plt.plot(fpr, tpr, label=f'AUC = {auc:.2f}') 778 | plt.legend() 779 | plt.savefig('auc_plot.png') 780 | ``` 781 | 782 | 77. **What is a Matplotlib plot for ML error bars?** 783 | Shows uncertainty in model metrics. 784 | ```python 785 | means = [0.8, 0.85, 0.9] 786 | stds = [0.05, 0.04, 0.03] 787 | plt.errorbar(range(3), means, yerr=stds, fmt='o') 788 | plt.savefig('error_bars.png') 789 | ``` 790 | 791 | 78. **How do you plot a Matplotlib bar plot for ML metric comparison?** 792 | Compares model performances. 793 | ```python 794 | models = ['Model A', 'Model B'] 795 | accuracies = [0.85, 0.9] 796 | plt.bar(models, accuracies) 797 | plt.savefig('metric_comparison.png') 798 | ``` 799 | 800 | #### Intermediate 801 | 79. **Write a function to plot ML model validation metrics.** 802 | Compares train and validation performance. 803 | ```python 804 | def plot_validation_metrics(train_metrics, val_metrics, metric_name): 805 | plt.plot(train_metrics, label='Train') 806 | plt.plot(val_metrics, label='Validation') 807 | plt.legend() 808 | plt.ylabel(metric_name) 809 | plt.savefig('validation_metrics.png') 810 | ``` 811 | 812 | 80. **How do you visualize ML model calibration with Matplotlib?** 813 | Plots predicted vs. actual probabilities. 814 | ```python 815 | from sklearn.calibration import calibration_curve 816 | prob_true, prob_pred = calibration_curve([0, 1, 1, 0], [0.1, 0.9, 0.8, 0.2], n_bins=5) 817 | plt.plot(prob_pred, prob_true) 818 | plt.plot([0, 1], [0, 1], 'k--') 819 | plt.savefig('calibration_curve.png') 820 | ``` 821 | 822 | 81. **Write a function to plot ML model performance across folds.** 823 | Visualizes cross-validation results. 824 | ```python 825 | def plot_cv_results(scores): 826 | plt.boxplot(scores) 827 | plt.savefig('cv_results.png') 828 | ``` 829 | 830 | 82. **How do you create a Matplotlib plot for ML model comparison?** 831 | Compares multiple models’ metrics. 832 | ```python 833 | models = ['A', 'B', 'C'] 834 | metrics = [[0.8, 0.85, 0.9], [0.7, 0.75, 0.8]] 835 | for i, metric in enumerate(metrics): 836 | plt.plot(models, metric, label=f'Metric {i+1}') 837 | plt.legend() 838 | plt.savefig('model_comparison.png') 839 | ``` 840 | 841 | 83. **Write a function to visualize ML model bias-variance tradeoff.** 842 | Plots error vs. model complexity. 843 | ```python 844 | def plot_bias_variance(complexity, train_error, test_error): 845 | plt.plot(complexity, train_error, label='Train Error') 846 | plt.plot(complexity, test_error, label='Test Error') 847 | plt.legend() 848 | plt.savefig('bias_variance.png') 849 | ``` 850 | 851 | 84. **How do you plot a Matplotlib heatmap for ML model errors?** 852 | Visualizes error patterns. 853 | ```python 854 | import numpy as np 855 | errors = np.random.rand(5, 5) 856 | plt.imshow(errors, cmap='Reds') 857 | plt.colorbar() 858 | plt.savefig('error_heatmap.png') 859 | ``` 860 | 861 | #### Advanced 862 | 85. **Write a function to plot ML model performance with confidence intervals.** 863 | Visualizes metric uncertainty. 864 | ```python 865 | import numpy as np 866 | def plot_with_ci(x, means, stds): 867 | plt.plot(x, means, label='Mean') 868 | plt.fill_between(x, means - stds, means + stds, alpha=0.3, label='CI') 869 | plt.legend() 870 | plt.savefig('confidence_intervals.png') 871 | ``` 872 | 873 | 86. **How do you visualize ML model performance drift with Matplotlib?** 874 | Plots metrics over time. 875 | ```python 876 | metrics = [0.9, 0.89, 0.87, 0.85] 877 | plt.plot(metrics, marker='o') 878 | plt.savefig('performance_drift.png') 879 | ``` 880 | 881 | 87. **Write a function to plot ML model partial dependence.** 882 | Visualizes feature effects on predictions. 883 | ```python 884 | from sklearn.ensemble import GradientBoostingClassifier 885 | from sklearn.inspection import partial_dependence 886 | def plot_partial_dependence(X, y, feature): 887 | model = GradientBoostingClassifier().fit(X, y) 888 | pdp = partial_dependence(model, X, features=[feature], grid_resolution=50) 889 | plt.plot(pdp['grid_values'][0], pdp['average'][0]) 890 | plt.savefig('partial_dependence.png') 891 | ``` 892 | 893 | 88. **How do you create a Matplotlib plot for ML model ensemble performance?** 894 | Compares ensemble vs. individual models. 895 | ```python 896 | ensemble_scores = [0.9, 0.91, 0.92] 897 | single_scores = [0.85, 0.86, 0.87] 898 | plt.plot(ensemble_scores, label='Ensemble') 899 | plt.plot(single_scores, label='Single') 900 | plt.legend() 901 | plt.savefig('ensemble_performance.png') 902 | ``` 903 | 904 | 89. **Write a function to visualize ML model sensitivity analysis.** 905 | Plots metric changes with parameter tweaks. 906 | ```python 907 | def plot_sensitivity(params, metrics): 908 | plt.plot(params, metrics, marker='o') 909 | plt.savefig('sensitivity_analysis.png') 910 | ``` 911 | 912 | 90. **How do you plot a Matplotlib violin plot for ML metric distributions?** 913 | Visualizes metric variability. 914 | ```python 915 | import numpy as np 916 | metrics = [np.random.randn(100) for _ in range(3)] 917 | plt.violinplot(metrics) 918 | plt.savefig('metric_violin.png') 919 | ``` 920 | 921 | ## Debugging and Error Handling 922 | 923 | ### Basic 924 | 91. **How do you debug a Matplotlib plot that doesn’t display correctly?** 925 | Checks data and plot settings. 926 | ```python 927 | import numpy as np 928 | x = np.array([1, 2, 3]) 929 | y = np.array([4, 5, 6]) 930 | if x.shape == y.shape: 931 | plt.plot(x, y) 932 | plt.savefig('debug_plot.png') 933 | ``` 934 | 935 | 92. **What is a try-except block in Matplotlib visualizations?** 936 | Handles plotting errors gracefully. 937 | ```python 938 | try: 939 | plt.plot([1, 2, 3], [4, 5, '6']) # Invalid data 940 | plt.savefig('try_plot.png') 941 | except ValueError as e: 942 | print(f"Error: {e}") 943 | ``` 944 | 945 | 93. **How do you validate Matplotlib input data?** 946 | Ensures data compatibility for ML plots. 947 | ```python 948 | def validate_data(x, y): 949 | if len(x) != len(y): 950 | raise ValueError("Mismatched data lengths") 951 | plt.plot(x, y) 952 | plt.savefig('validated_plot.png') 953 | ``` 954 | 955 | 94. **How do you handle missing data in Matplotlib plots?** 956 | Filters NaN values for ML visualizations. 957 | ```python 958 | import numpy as np 959 | x = np.array([1, 2, 3]) 960 | y = np.array([4, np.nan, 6]) 961 | mask = ~np.isnan(y) 962 | plt.plot(x[mask], y[mask]) 963 | plt.savefig('missing_data_plot.png') 964 | ``` 965 | 966 | 95. **What is the role of Matplotlib’s warning suppression?** 967 | Avoids clutter in ML visualization logs. 968 | ```python 969 | import warnings 970 | warnings.filterwarnings('ignore', category=UserWarning) 971 | plt.plot([1, 2, 3], [4, 5, 6]) 972 | plt.savefig('warning_suppressed.png') 973 | ``` 974 | 975 | 96. **How do you log Matplotlib errors?** 976 | Records issues for debugging. 977 | ```python 978 | import logging 979 | logging.basicConfig(filename='plot.log', level=logging.ERROR) 980 | try: 981 | plt.plot([1, 2, 3], [4, 5, '6']) 982 | except Exception as e: 983 | logging.error(f"Plot error: {e}") 984 | ``` 985 | 986 | #### Intermediate 987 | 97. **Write a function to retry Matplotlib plotting on failure.** 988 | Handles transient errors in ML visualizations. 989 | ```python 990 | def retry_plot(x, y, max_attempts=3): 991 | for attempt in range(max_attempts): 992 | try: 993 | plt.plot(x, y) 994 | plt.savefig('retry_plot.png') 995 | return 996 | except Exception as e: 997 | if attempt == max_attempts - 1: 998 | raise 999 | print(f"Attempt {attempt+1} failed: {e}") 1000 | ``` 1001 | 1002 | 98. **How do you debug Matplotlib axis scaling issues?** 1003 | Inspects axis limits and data ranges. 1004 | ```python 1005 | x = [1, 2, 3] 1006 | y = [1e10, 2e10, 3e10] 1007 | plt.plot(x, y) 1008 | plt.yscale('log') 1009 | print(f"Axis limits: {plt.gca().get_ylim()}") 1010 | plt.savefig('axis_debug.png') 1011 | ``` 1012 | 1013 | 99. **Write a function to handle Matplotlib memory errors for large plots.** 1014 | Downsamples data for efficiency. 1015 | ```python 1016 | import numpy as np 1017 | def plot_large_data(x, y, max_points=1000): 1018 | if len(x) > max_points: 1019 | indices = np.random.choice(len(x), max_points, replace=False) 1020 | x, y = x[indices], y[indices] 1021 | plt.plot(x, y) 1022 | plt.savefig('large_data_plot.png') 1023 | ``` 1024 | 1025 | 100. **How do you validate Matplotlib plot aesthetics?** 1026 | Ensures style consistency for ML reports. 1027 | ```python 1028 | def validate_aesthetics(): 1029 | style = plt.rcParams['axes.grid'] 1030 | if not style: 1031 | raise ValueError("Grid not enabled") 1032 | plt.plot([1, 2, 3], [4, 5, 6]) 1033 | plt.savefig('aesthetics_plot.png') 1034 | ``` 1035 | 1036 | 101. **Write a function to debug Matplotlib subplot layouts.** 1037 | Checks subplot alignment and spacing. 1038 | ```python 1039 | def debug_subplots(): 1040 | fig, axs = plt.subplots(2, 2) 1041 | for ax in axs.flat: 1042 | ax.plot([1, 2, 3], [4, 5, 6]) 1043 | plt.tight_layout() 1044 | print(f"Figure size: {fig.get_size_inches()}") 1045 | plt.savefig('subplot_debug.png') 1046 | ``` 1047 | 1048 | 102. **How do you handle Matplotlib backend errors?** 1049 | Switches to compatible backends for ML environments. 1050 | ```python 1051 | import matplotlib 1052 | try: 1053 | matplotlib.use('TkAgg') 1054 | plt.plot([1, 2, 3], [4, 5, 6]) 1055 | plt.savefig('backend_plot.png') 1056 | except: 1057 | matplotlib.use('Agg') 1058 | plt.plot([1, 2, 3], [4, 5, 6]) 1059 | plt.savefig('fallback_plot.png') 1060 | ``` 1061 | 1062 | #### Advanced 1063 | 103. **Write a function to implement a custom Matplotlib error handler.** 1064 | Logs specific plotting errors for ML workflows. 1065 | ```python 1066 | import logging 1067 | def custom_error_handler(x, y): 1068 | logging.basicConfig(filename='plot.log', level=logging.ERROR) 1069 | try: 1070 | plt.plot(x, y) 1071 | plt.savefig('custom_error_plot.png') 1072 | except Exception as e: 1073 | logging.error(f"Custom plot error: {e}") 1074 | raise 1075 | ``` 1076 | 1077 | 104. **How do you debug Matplotlib performance bottlenecks?** 1078 | Profiles plotting time for large ML datasets. 1079 | ```python 1080 | import time 1081 | def profile_plot(x, y): 1082 | start = time.time() 1083 | plt.plot(x, y) 1084 | plt.savefig('profile_plot.png') 1085 | print(f"Plotting time: {time.time() - start}s") 1086 | ``` 1087 | 1088 | 105. **Write a function to handle Matplotlib version compatibility.** 1089 | Checks for deprecated features in ML visualizations. 1090 | ```python 1091 | import matplotlib 1092 | def check_version(): 1093 | if matplotlib.__version__ < '3.5': 1094 | raise ValueError("Unsupported Matplotlib version") 1095 | plt.plot([1, 2, 3], [4, 5, 6]) 1096 | plt.savefig('version_plot.png') 1097 | ``` 1098 | 1099 | 106. **How do you debug Matplotlib rendering issues in non-interactive environments?** 1100 | Ensures proper backend and output. 1101 | ```python 1102 | import matplotlib 1103 | matplotlib.use('Agg') 1104 | plt.plot([1, 2, 3], [4, 5, 6]) 1105 | plt.savefig('non_interactive_plot.png') 1106 | print(f"Backend: {matplotlib.get_backend()}") 1107 | ``` 1108 | 1109 | 107. **Write a function to handle Matplotlib font rendering errors.** 1110 | Falls back to default fonts for ML plots. 1111 | ```python 1112 | def safe_font_plot(x, y): 1113 | try: 1114 | plt.rcParams['font.family'] = 'InvalidFont' 1115 | plt.plot(x, y) 1116 | except: 1117 | plt.rcParams['font.family'] = 'sans-serif' 1118 | plt.plot(x, y) 1119 | plt.savefig('font_plot.png') 1120 | ``` 1121 | 1122 | 108. **How do you implement Matplotlib error handling for invalid data types?** 1123 | Validates inputs for ML visualizations. 1124 | ```python 1125 | def plot_with_type_check(x, y): 1126 | if not all(isinstance(i, (int, float)) for i in x + y): 1127 | raise TypeError("Invalid data types") 1128 | plt.plot(x, y) 1129 | plt.savefig('type_check_plot.png') 1130 | ``` 1131 | 1132 | ## Deployment and Integration 1133 | 1134 | ### Basic 1135 | 109. **How do you deploy Matplotlib plots in a web application?** 1136 | Saves plots for web display. 1137 | ```python 1138 | plt.plot([1, 2, 3], [4, 5, 6]) 1139 | plt.savefig('web_plot.png', dpi=100, bbox_inches='tight') 1140 | ``` 1141 | 1142 | 110. **What is the role of Matplotlib in ML dashboards?** 1143 | Generates visualizations for model monitoring. 1144 | ```python 1145 | plt.plot([1, 2, 3], [0.8, 0.85, 0.9]) 1146 | plt.savefig('dashboard_plot.png') 1147 | ``` 1148 | 1149 | 111. **How do you integrate Matplotlib with Flask for ML visualizations?** 1150 | Serves plots via a web API. 1151 | ```python 1152 | from flask import Flask, send_file 1153 | app = Flask(__name__) 1154 | @app.route('/plot') 1155 | def serve_plot(): 1156 | plt.plot([1, 2, 3], [4, 5, 6]) 1157 | plt.savefig('flask_plot.png') 1158 | return send_file('flask_plot.png') 1159 | ``` 1160 | 1161 | 112. **How do you save Matplotlib plots for ML reports?** 1162 | Exports high-quality images or PDFs. 1163 | ```python 1164 | plt.plot([1, 2, 3], [4, 5, 6]) 1165 | plt.savefig('report_plot.pdf') 1166 | ``` 1167 | 1168 | 113. **What is the role of Matplotlib’s Agg backend in deployment?** 1169 | Enables non-interactive plotting for servers. 1170 | ```python 1171 | import matplotlib 1172 | matplotlib.use('Agg') 1173 | plt.plot([1, 2, 3], [4, 5, 6]) 1174 | plt.savefig('agg_plot.png') 1175 | ``` 1176 | 1177 | 114. **How do you automate Matplotlib plot generation for ML pipelines?** 1178 | Scripts plot creation for batch processing. 1179 | ```python 1180 | def generate_plots(data_list): 1181 | for i, data in enumerate(data_list): 1182 | plt.plot(data['x'], data['y']) 1183 | plt.savefig(f'plot_{i}.png') 1184 | plt.clf() 1185 | ``` 1186 | 1187 | #### Intermediate 1188 | 115. **Write a function to integrate Matplotlib with FastAPI for ML visualizations.** 1189 | Serves dynamic plots via API. 1190 | ```python 1191 | from fastapi import FastAPI 1192 | from fastapi.responses import FileResponse 1193 | app = FastAPI() 1194 | @app.get('/plot') 1195 | async def get_plot(): 1196 | plt.plot([1, 2, 3], [4, 5, 6]) 1197 | plt.savefig('fastapi_plot.png') 1198 | return FileResponse('fastapi_plot.png') 1199 | ``` 1200 | 1201 | 116. **How do you optimize Matplotlib plots for large-scale ML deployments?** 1202 | Uses efficient formats and downsampling. 1203 | ```python 1204 | import numpy as np 1205 | x = np.linspace(0, 10, 10000) 1206 | y = np.sin(x) 1207 | plt.plot(x[::10], y[::10]) 1208 | plt.savefig('optimized_plot.png', dpi=100) 1209 | ``` 1210 | 1211 | 117. **Write a function to generate Matplotlib plots in parallel.** 1212 | Speeds up ML visualization pipelines. 1213 | ```python 1214 | from concurrent.futures import ThreadPoolExecutor 1215 | def parallel_plots(data_list): 1216 | def plot_single(data, i): 1217 | plt.figure() 1218 | plt.plot(data['x'], data['y']) 1219 | plt.savefig(f'parallel_plot_{i}.png') 1220 | plt.close() 1221 | with ThreadPoolExecutor() as executor: 1222 | executor.map(lambda x: plot_single(*x), [(d, i) for i, d in enumerate(data_list)]) 1223 | ``` 1224 | 1225 | 118. **How do you integrate Matplotlib with Jupyter for ML notebooks?** 1226 | Embeds plots in notebook outputs. 1227 | ```python 1228 | %matplotlib inline 1229 | plt.plot([1, 2, 3], [4, 5, 6]) 1230 | plt.savefig('jupyter_plot.png') 1231 | ``` 1232 | 1233 | 119. **Write a function to version Matplotlib plots for ML experiments.** 1234 | Organizes plots by experiment ID. 1235 | ```python 1236 | def save_versioned_plot(x, y, version): 1237 | plt.plot(x, y) 1238 | plt.savefig(f'plot_v{version}.png') 1239 | ``` 1240 | 1241 | 120. **How do you secure Matplotlib plot generation in production?** 1242 | Validates inputs and restricts file access. 1243 | ```python 1244 | import os 1245 | def secure_plot(x, y, filename): 1246 | if not filename.endswith('.png'): 1247 | raise ValueError("Invalid file format") 1248 | plt.plot(x, y) 1249 | plt.savefig(os.path.join('secure_dir', filename)) 1250 | ``` 1251 | 1252 | #### Advanced 1253 | 121. **Write a function to stream Matplotlib plots for real-time ML monitoring.** 1254 | Updates plots dynamically. 1255 | ```python 1256 | import time 1257 | def stream_plot(data_stream): 1258 | plt.ion() 1259 | fig, ax = plt.subplots() 1260 | for data in data_stream: 1261 | ax.clear() 1262 | ax.plot(data['x'], data['y']) 1263 | plt.savefig('stream_plot.png') 1264 | time.sleep(1) 1265 | ``` 1266 | 1267 | 122. **How do you implement Matplotlib plot caching for ML deployments?** 1268 | Reuses plots to reduce computation. 1269 | ```python 1270 | import hashlib 1271 | def cached_plot(x, y): 1272 | key = hashlib.md5(str(x + y).encode()).hexdigest() 1273 | if not os.path.exists(f'cache/{key}.png'): 1274 | plt.plot(x, y) 1275 | plt.savefig(f'cache/{key}.png') 1276 | return f'cache/{key}.png' 1277 | ``` 1278 | 1279 | 123. **Write a function to handle Matplotlib plot failover in production.** 1280 | Switches to fallback formats on failure. 1281 | ```python 1282 | def failover_plot(x, y): 1283 | try: 1284 | plt.plot(x, y) 1285 | plt.savefig('plot.png') 1286 | except: 1287 | plt.plot(x, y) 1288 | plt.savefig('plot.jpg') 1289 | ``` 1290 | 1291 | 124. **How do you integrate Matplotlib with cloud storage for ML visualizations?** 1292 | Saves plots to cloud services. 1293 | ```python 1294 | import boto3 1295 | def save_to_s3(x, y, bucket, key): 1296 | plt.plot(x, y) 1297 | plt.savefig('temp_plot.png') 1298 | s3 = boto3.client('s3') 1299 | s3.upload_file('temp_plot.png', bucket, key) 1300 | ``` 1301 | 1302 | 125. **Write a function to optimize Matplotlib plot rendering for ML pipelines.** 1303 | Minimizes rendering overhead. 1304 | ```python 1305 | import matplotlib 1306 | matplotlib.use('Agg') 1307 | def optimized_plot(x, y): 1308 | plt.figure(figsize=(6, 4)) 1309 | plt.plot(x, y) 1310 | plt.savefig('optimized_plot.png', dpi=100) 1311 | plt.close() 1312 | ``` 1313 | 1314 | 126. **How do you implement Matplotlib plot versioning with metadata?** 1315 | Tracks plot configurations for ML experiments. 1316 | ```python 1317 | import json 1318 | def versioned_plot_with_metadata(x, y, metadata): 1319 | plt.plot(x, y) 1320 | plt.savefig('versioned_plot.png') 1321 | with open('metadata.json', 'w') as f: 1322 | json.dump(metadata, f) 1323 | ``` 1324 | 1325 | ## Best Practices and Optimization 1326 | 1327 | ### Basic 1328 | 127. **What are best practices for structuring Matplotlib code?** 1329 | Modularizes plotting functions for ML workflows. 1330 | ```python 1331 | def create_plot(x, y): 1332 | plt.plot(x, y) 1333 | plt.savefig('structured_plot.png') 1334 | ``` 1335 | 1336 | 128. **How do you ensure reproducibility in Matplotlib visualizations?** 1337 | Sets seeds and versions for consistency. 1338 | ```python 1339 | import numpy as np 1340 | np.random.seed(42) 1341 | plt.plot(np.random.rand(3)) 1342 | plt.savefig('reproducible_plot.png') 1343 | ``` 1344 | 1345 | 129. **What is the role of Matplotlib’s rcParams?** 1346 | Configures default plot settings for ML. 1347 | ```python 1348 | plt.rcParams['figure.dpi'] = 100 1349 | plt.plot([1, 2, 3], [4, 5, 6]) 1350 | plt.savefig('rcparams_plot.png') 1351 | ``` 1352 | 1353 | 130. **How do you handle large-scale data in Matplotlib visualizations?** 1354 | Uses downsampling or aggregation for ML datasets. 1355 | ```python 1356 | import numpy as np 1357 | x = np.linspace(0, 10, 10000) 1358 | y = np.sin(x) 1359 | plt.plot(x[::10], y[::10]) 1360 | plt.savefig('large_scale_plot.png') 1361 | ``` 1362 | 1363 | 131. **What is the role of Matplotlib’s tight_layout?** 1364 | Optimizes subplot spacing for ML visualizations. 1365 | ```python 1366 | fig, axs = plt.subplots(2, 2) 1367 | for ax in axs.flat: 1368 | ax.plot([1, 2, 3], [4, 5, 6]) 1369 | plt.tight_layout() 1370 | plt.savefig('tight_layout.png') 1371 | ``` 1372 | 1373 | 132. **How do you document Matplotlib plotting functions?** 1374 | Uses docstrings for clarity in ML workflows. 1375 | ```python 1376 | def plot_data(x, y): 1377 | """Plots x vs. y with labels and saves to file.""" 1378 | plt.plot(x, y) 1379 | plt.savefig('documented_plot.png') 1380 | ``` 1381 | 1382 | #### Intermediate 1383 | 133. **Write a function to optimize Matplotlib plot memory usage.** 1384 | Closes figures to free memory in ML pipelines. 1385 | ```python 1386 | def memory_optimized_plot(x, y): 1387 | plt.figure() 1388 | plt.plot(x, y) 1389 | plt.savefig('memory_plot.png') 1390 | plt.close() 1391 | ``` 1392 | 1393 | 134. **How do you implement Matplotlib plot testing?** 1394 | Validates plot outputs for ML visualizations. 1395 | ```python 1396 | import unittest 1397 | class TestPlot(unittest.TestCase): 1398 | def test_plot_output(self): 1399 | plt.plot([1, 2, 3], [4, 5, 6]) 1400 | plt.savefig('test_plot.png') 1401 | self.assertTrue(os.path.exists('test_plot.png')) 1402 | ``` 1403 | 1404 | 135. **Write a function to create reusable Matplotlib templates.** 1405 | Standardizes ML plot formats. 1406 | ```python 1407 | def plot_template(x, y, title=''): 1408 | plt.figure(figsize=(8, 6)) 1409 | plt.plot(x, y) 1410 | plt.title(title) 1411 | plt.savefig(f'{title}_template.png') 1412 | ``` 1413 | 1414 | 136. **How do you optimize Matplotlib for batch plotting in ML?** 1415 | Minimizes overhead for multiple plots. 1416 | ```python 1417 | def batch_plot(data_list): 1418 | for i, data in enumerate(data_list): 1419 | plt.figure() 1420 | plt.plot(data['x'], data['y']) 1421 | plt.savefig(f'batch_plot_{i}.png') 1422 | plt.close() 1423 | ``` 1424 | 1425 | 137. **Write a function to handle Matplotlib plot configuration.** 1426 | Centralizes plot settings for ML workflows. 1427 | ```python 1428 | def configure_plot(): 1429 | plt.rcParams.update({ 1430 | 'figure.figsize': (8, 6), 1431 | 'axes.grid': True 1432 | }) 1433 | plt.plot([1, 2, 3], [4, 5, 6]) 1434 | plt.savefig('configured_plot.png') 1435 | ``` 1436 | 1437 | 138. **How do you ensure Matplotlib plot consistency across environments?** 1438 | Standardizes backends and styles. 1439 | ```python 1440 | import matplotlib 1441 | matplotlib.use('Agg') 1442 | plt.style.use('seaborn') 1443 | plt.plot([1, 2, 3], [4, 5, 6]) 1444 | plt.savefig('consistent_plot.png') 1445 | ``` 1446 | 1447 | #### Advanced 1448 | 139. **Write a function to implement Matplotlib plot caching for ML pipelines.** 1449 | Reuses plots to save time. 1450 | ```python 1451 | import hashlib 1452 | def cache_plot(x, y): 1453 | key = hashlib.md5(str(x + y).encode()).hexdigest() 1454 | if not os.path.exists(f'cache/{key}.png'): 1455 | plt.plot(x, y) 1456 | plt.savefig(f'cache/{key}.png') 1457 | return f'cache/{key}.png' 1458 | ``` 1459 | 1460 | 140. **How do you optimize Matplotlib for high-throughput ML visualizations?** 1461 | Uses efficient rendering and batching. 1462 | ```python 1463 | import matplotlib 1464 | matplotlib.use('Agg') 1465 | def high_throughput_plot(data_list): 1466 | for i, data in enumerate(data_list): 1467 | plt.figure(figsize=(6, 4)) 1468 | plt.plot(data['x'], data['y']) 1469 | plt.savefig(f'high_throughput_{i}.png', dpi=100) 1470 | plt.close() 1471 | ``` 1472 | 1473 | 141. **Write a function to implement Matplotlib plot versioning for ML experiments.** 1474 | Tracks plot changes systematically. 1475 | ```python 1476 | def version_plot(x, y, version): 1477 | plt.plot(x, y) 1478 | plt.savefig(f'plots/plot_v{version}.png') 1479 | ``` 1480 | 1481 | 142. **How do you implement Matplotlib plot monitoring in production?** 1482 | Logs plot generation metrics. 1483 | ```python 1484 | import logging 1485 | def monitored_plot(x, y): 1486 | logging.basicConfig(filename='plot.log', level=logging.INFO) 1487 | start = time.time() 1488 | plt.plot(x, y) 1489 | plt.savefig('monitored_plot.png') 1490 | logging.info(f"Plot generated in {time.time() - start}s") 1491 | ``` 1492 | 1493 | 143. **Write a function to handle Matplotlib plot scalability for ML dashboards.** 1494 | Generates plots for large datasets efficiently. 1495 | ```python 1496 | import numpy as np 1497 | def scalable_plot(x, y, max_points=1000): 1498 | if len(x) > max_points: 1499 | indices = np.random.choice(len(x), max_points, replace=False) 1500 | x, y = x[indices], y[indices] 1501 | plt.plot(x, y) 1502 | plt.savefig('scalable_plot.png') 1503 | ``` 1504 | 1505 | 144. **How do you implement Matplotlib plot automation for ML workflows?** 1506 | Scripts plot generation for pipelines. 1507 | ```python 1508 | def auto_plot_pipeline(data_list): 1509 | for i, data in enumerate(data_list): 1510 | plt.figure() 1511 | plt.plot(data['x'], data['y']) 1512 | plt.savefig(f'auto_plot_{i}.png') 1513 | plt.close() 1514 | ``` 1515 | 1516 | ## Visualization Interpretation 1517 | 1518 | ### Basic 1519 | 145. **How do you interpret a Matplotlib line plot in ML?** 1520 | Analyzes trends in model performance. 1521 | ```python 1522 | plt.plot([1, 2, 3], [0.8, 0.85, 0.9]) 1523 | plt.savefig('interpret_line_plot.png') 1524 | ``` 1525 | 1526 | 146. **What insights can a Matplotlib scatter plot provide in ML?** 1527 | Reveals data clusters or outliers. 1528 | ```python 1529 | plt.scatter([1, 2, 3], [4, 5, 6]) 1530 | plt.savefig('interpret_scatter.png') 1531 | ``` 1532 | 1533 | 147. **How do you interpret a Matplotlib histogram in ML?** 1534 | Shows data distribution for feature analysis. 1535 | ```python 1536 | import numpy as np 1537 | plt.hist(np.random.randn(1000), bins=30) 1538 | plt.savefig('interpret_histogram.png') 1539 | ``` 1540 | 1541 | 148. **What is the role of Matplotlib heatmaps in ML interpretation?** 1542 | Visualizes correlations or errors. 1543 | ```python 1544 | import numpy as np 1545 | plt.imshow(np.random.rand(5, 5), cmap='viridis') 1546 | plt.colorbar() 1547 | plt.savefig('interpret_heatmap.png') 1548 | ``` 1549 | 1550 | 149. **How do you interpret a Matplotlib box plot in ML?** 1551 | Identifies outliers and data spread. 1552 | ```python 1553 | plt.boxplot([1, 2, 3, 10, 20]) 1554 | plt.savefig('interpret_boxplot.png') 1555 | ``` 1556 | 1557 | 150. **What insights can Matplotlib bar plots provide in ML?** 1558 | Compares model or feature metrics. 1559 | ```python 1560 | plt.bar(['A', 'B'], [0.85, 0.9]) 1561 | plt.savefig('interpret_bar.png') 1562 | ``` 1563 | 1564 | #### Intermediate 1565 | 151. **Write a function to interpret Matplotlib plot trends for ML.** 1566 | Analyzes metric changes over time. 1567 | ```python 1568 | def interpret_trends(x, y): 1569 | plt.plot(x, y) 1570 | slope = (y[-1] - y[0]) / (x[-1] - x[0]) 1571 | plt.savefig('trend_plot.png') 1572 | return {'slope': slope} 1573 | ``` 1574 | 1575 | 152. **How do you interpret Matplotlib ROC curves in ML?** 1576 | Evaluates classifier performance. 1577 | ```python 1578 | from sklearn.metrics import roc_curve 1579 | fpr, tpr, _ = roc_curve([0, 1, 1, 0], [0.1, 0.9, 0.8, 0.2]) 1580 | plt.plot(fpr, tpr) 1581 | plt.savefig('interpret_roc.png') 1582 | ``` 1583 | 1584 | 153. **Write a function to interpret Matplotlib confusion matrices.** 1585 | Analyzes classification errors. 1586 | ```python 1587 | from sklearn.metrics import confusion_matrix 1588 | def interpret_confusion_matrix(y_true, y_pred): 1589 | cm = confusion_matrix(y_true, y_pred) 1590 | plt.imshow(cm, cmap='Blues') 1591 | plt.colorbar() 1592 | plt.savefig('interpret_cm.png') 1593 | return {'accuracy': np.trace(cm) / np.sum(cm)} 1594 | ``` 1595 | 1596 | 154. **How do you interpret Matplotlib learning curves in ML?** 1597 | Assesses model overfitting or underfitting. 1598 | ```python 1599 | train_scores = [0.9, 0.92, 0.94] 1600 | test_scores = [0.8, 0.81, 0.82] 1601 | plt.plot(train_scores, label='Train') 1602 | plt.plot(test_scores, label='Test') 1603 | plt.legend() 1604 | plt.savefig('interpret_learning_curve.png') 1605 | ``` 1606 | 1607 | 155. **Write a function to interpret Matplotlib feature importance plots.** 1608 | Identifies key predictors in ML models. 1609 | ```python 1610 | def interpret_feature_importance(features, importances): 1611 | plt.bar(features, importances) 1612 | plt.xticks(rotation=45) 1613 | plt.savefig('interpret_feature_importance.png') 1614 | return {'top_feature': features[np.argmax(importances)]} 1615 | ``` 1616 | 1617 | 156. **How do you interpret Matplotlib residual plots in ML?** 1618 | Detects model prediction biases. 1619 | ```python 1620 | y_true = [1, 2, 3] 1621 | y_pred = [1.1, 2.2, 2.9] 1622 | residuals = np.array(y_true) - np.array(y_pred) 1623 | plt.scatter(y_pred, residuals) 1624 | plt.axhline(0, color='r', linestyle='--') 1625 | plt.savefig('interpret_residuals.png') 1626 | ``` 1627 | 1628 | #### Advanced 1629 | 157. **Write a function to interpret Matplotlib partial dependence plots.** 1630 | Analyzes feature effects on ML predictions. 1631 | ```python 1632 | from sklearn.inspection import partial_dependence 1633 | def interpret_partial_dependence(X, y, feature): 1634 | model = GradientBoostingClassifier().fit(X, y) 1635 | pdp = partial_dependence(model, X, features=[feature], grid_resolution=50) 1636 | plt.plot(pdp['grid_values'][0], pdp['average'][0]) 1637 | plt.savefig('interpret_pdp.png') 1638 | return {'effect': pdp['average'][0].mean()} 1639 | ``` 1640 | 1641 | 158. **How do you interpret Matplotlib plots for ML model drift?** 1642 | Detects changes in performance over time. 1643 | ```python 1644 | metrics = [0.9, 0.89, 0.87, 0.85] 1645 | plt.plot(metrics, marker='o') 1646 | plt.savefig('interpret_drift.png') 1647 | ``` 1648 | 1649 | 159. **Write a function to interpret Matplotlib plots for ML uncertainty.** 1650 | Analyzes prediction confidence. 1651 | ```python 1652 | def interpret_uncertainty(x, y, y_std): 1653 | plt.plot(x, y, label='Prediction') 1654 | plt.fill_between(x, y - y_std, y + y_std, alpha=0.3, label='Uncertainty') 1655 | plt.legend() 1656 | plt.savefig('interpret_uncertainty.png') 1657 | return {'avg_uncertainty': y_std.mean()} 1658 | ``` 1659 | 1660 | 160. **How do you interpret Matplotlib plots for ML ensemble performance?** 1661 | Compares ensemble vs. individual models. 1662 | ```python 1663 | ensemble_scores = [0.9, 0.91, 0.92] 1664 | single_scores = [0.85, 0.86, 0.87] 1665 | plt.plot(ensemble_scores, label='Ensemble') 1666 | plt.plot(single_scores, label='Single') 1667 | plt.legend() 1668 | plt.savefig('interpret_ensemble.png') 1669 | ``` 1670 | 1671 | 161. **Write a function to interpret Matplotlib plots for ML bias analysis.** 1672 | Visualizes model fairness metrics. 1673 | ```python 1674 | def interpret_bias(y_true, y_pred, groups): 1675 | errors = np.abs(np.array(y_true) - np.array(y_pred)) 1676 | plt.boxplot([errors[groups == g] for g in np.unique(groups)]) 1677 | plt.savefig('interpret_bias.png') 1678 | return {'group_errors': [errors[groups == g].mean() for g in np.unique(groups)]} 1679 | ``` 1680 | 1681 | 162. **How do you interpret Matplotlib plots for ML hyperparameter tuning?** 1682 | Analyzes parameter impact on performance. 1683 | ```python 1684 | params = [0.1, 1, 10] 1685 | scores = [0.8, 0.85, 0.83] 1686 | plt.plot(params, scores, marker='o') 1687 | plt.xscale('log') 1688 | plt.savefig('interpret_tuning.png') 1689 | ``` 1690 | 1691 | ## Advanced Visualization Techniques 1692 | 1693 | ### Basic 1694 | 163. **How do you create a Matplotlib plot with multiple axes for ML?** 1695 | Visualizes correlated metrics. 1696 | ```python 1697 | fig, ax1 = plt.subplots() 1698 | ax1.plot([1, 2, 3], [4, 5, 6], 'b-') 1699 | ax2 = ax1.twinx() 1700 | ax2.plot([1, 2, 3], [60, 50, 40], 'r-') 1701 | plt.savefig('multi_axes.png') 1702 | ``` 1703 | 1704 | 164. **What is a Matplotlib animation, and how is it used in ML?** 1705 | Visualizes dynamic model behavior. 1706 | ```python 1707 | import matplotlib.animation as animation 1708 | fig, ax = plt.subplots() 1709 | line, = ax.plot([], []) 1710 | def update(i): 1711 | line.set_data(range(i), np.sin(np.linspace(0, 10, i))) 1712 | return line, 1713 | ani = animation.FuncAnimation(fig, update, frames=100, interval=100) 1714 | ani.save('ml_animation.gif', writer='pillow') 1715 | ``` 1716 | 1717 | 165. **How do you create a Matplotlib plot with custom colormaps?** 1718 | Enhances ML visualization aesthetics. 1719 | ```python 1720 | from matplotlib.colors import LinearSegmentedColormap 1721 | cmap = LinearSegmentedColormap.from_list('custom', ['blue', 'red']) 1722 | plt.imshow(np.random.rand(10, 10), cmap=cmap) 1723 | plt.colorbar() 1724 | plt.savefig('custom_cmap.png') 1725 | ``` 1726 | 1727 | 166. **How do you use Matplotlib to visualize ML model gradients?** 1728 | Plots gradient magnitudes for debugging. 1729 | ```python 1730 | import numpy as np 1731 | x, y = np.meshgrid(np.linspace(-3, 3, 20), np.linspace(-3, 3, 20)) 1732 | u, v = y, -x 1733 | plt.quiver(x, y, u, v) 1734 | plt.savefig('gradient_plot.png') 1735 | ``` 1736 | 1737 | 167. **What is a Matplotlib 3D surface plot, and how is it used in ML?** 1738 | Visualizes loss surfaces or feature spaces. 1739 | ```python 1740 | from mpl_toolkits.mplot3d import Axes3D 1741 | x, y = np.meshgrid(np.linspace(-3, 3, 20), np.linspace(-3, 3, 20)) 1742 | z = x**2 + y**2 1743 | fig = plt.figure() 1744 | ax = fig.add_subplot(111, projection='3d') 1745 | ax.plot_surface(x, y, z, cmap='viridis') 1746 | plt.savefig('surface_plot.png') 1747 | ``` 1748 | 1749 | 168. **How do you create a Matplotlib plot with interactive widgets for ML?** 1750 | Enables dynamic parameter exploration. 1751 | ```python 1752 | from matplotlib.widgets import Slider 1753 | fig, ax = plt.subplots() 1754 | x = np.linspace(0, 10, 100) 1755 | line, = ax.plot(x, np.sin(x)) 1756 | ax_slider = plt.axes([0.1, 0.01, 0.65, 0.03]) 1757 | slider = Slider(ax_slider, 'Freq', 0.1, 10.0, valinit=1) 1758 | def update(val): 1759 | line.set_ydata(np.sin(slider.val * x)) 1760 | fig.canvas.draw_idle() 1761 | slider.on_changed(update) 1762 | plt.savefig('widget_plot.png') 1763 | ``` 1764 | 1765 | #### Intermediate 1766 | 169. **Write a function to create a Matplotlib plot with dynamic annotations.** 1767 | Highlights key ML data points dynamically. 1768 | ```python 1769 | def dynamic_annotations(x, y, labels): 1770 | plt.plot(x, y) 1771 | for i, label in enumerate(labels): 1772 | plt.annotate(label, (x[i], y[i])) 1773 | plt.savefig('dynamic_annotations.png') 1774 | ``` 1775 | 1776 | 170. **How do you implement a Matplotlib plot for ML model interpretability?** 1777 | Visualizes SHAP or LIME explanations. 1778 | ```python 1779 | import numpy as np 1780 | def plot_shap_values(features, shap_values): 1781 | plt.bar(features, shap_values) 1782 | plt.xticks(rotation=45) 1783 | plt.savefig('shap_plot.png') 1784 | ``` --------------------------------------------------------------------------------