Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+210 -253
View File
@@ -1,306 +1,263 @@
---
description: This rule provides guidelines and best practices for developing robust, maintainable, and performant data visualizations using Matplotlib in Python. It covers aspects from code organization to testing and security considerations.
globs: *.py
description: This guide outlines definitive best practices for writing clean, performant, and maintainable matplotlib code, emphasizing the object-oriented API and modern data science workflows.
globs: **/*.py
---
# Matplotlib Best Practices
# matplotlib Best Practices
This document outlines best practices for using Matplotlib, a powerful Python library for creating static, interactive, and animated visualizations. Adhering to these guidelines will help you write cleaner, more maintainable, and performant code for your data visualization projects.
Matplotlib is the bedrock of Python data visualization. Adhering to these guidelines ensures your plots are not just visually appealing, but also robust, performant, and easily integrated into production AI/ML and data science pipelines.
Library Information:
- Name: matplotlib
- Tags: ai, ml, data-science, python, data-visualization
## 1. Code Organization & Structure
## 1. Code Organization and Structure
### 1.1 Standard Imports
Proper code organization is essential for maintainability and collaboration. For visualization projects using Matplotlib, the organization becomes crucial due to the complexity that can arise from numerous plotting options and data manipulation.
Always use the conventional aliases. This improves readability and consistency across projects.
### 1.1 Directory Structure
❌ BAD:
```python
import numpy
import matplotlib.pyplot
import matplotlib
```
Adopt a structured directory layout to separate source code, data, and output. A typical project structure might look like this:
✅ GOOD:
```python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
```
### 1.2 Object-Oriented API First
project_name/
├── src/
│ ├── __init__.py
│ ├── data_processing.py # Data loading, cleaning, and transformation functions
│ ├── plotting_functions.py # Reusable plotting functions using Matplotlib
│ ├── main.py # Main script to orchestrate the visualization
├── data/
│ ├── raw/ # Original, unmodified data
│ ├── processed/ # Data that has been cleaned and transformed
├── output/
│ ├── images/ # Saved plot images
│ ├── reports/ # Generated reports or summaries
├── notebooks/ # Jupyter notebooks for exploration
├── tests/ # Testing directory
│ ├── __init__.py
│ ├── test_data_processing.py
│ ├── test_plotting_functions.py
├── README.md
├── requirements.txt
├── .gitignore
Prioritize the object-oriented API (`Figure` and `Axes` objects) over `pyplot`'s stateful interface. This leads to more explicit, reproducible, and testable code, especially in functions or classes.
❌ BAD (Stateful `pyplot`):
```python
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("My Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show() # Blocks execution
```
### 1.2 File Naming Conventions
✅ GOOD (Object-Oriented):
```python
from typing import List
import matplotlib.figure as mpl_figure
import matplotlib.axes as mpl_axes
* Use descriptive and consistent file names.
* Python files: `snake_case.py` (e.g., `data_processing.py`, `plotting_functions.py`).
* Image files: `descriptive_name.png`, `descriptive_name.jpg`, or `descriptive_name.svg`.
* Data files: `descriptive_name.csv`, `descriptive_name.json`.
def create_my_plot(data_x: List[float], data_y: List[float], title: str, x_label: str, y_label: str) -> mpl_figure.Figure:
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(data_x, data_y)
ax.set_title(title)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
return fig
### 1.3 Module Organization
# Usage in a script or notebook
if __name__ == "__main__":
x = [1, 2, 3]
y = [4, 5, 6]
my_figure = create_my_plot(x, y, "My Plot", "X-axis", "Y-axis")
my_figure.savefig("my_plot.png") # Non-blocking save
# If interactive display is needed, use:
# plt.show()
```
* Group related functions and classes into modules.
* Use meaningful module names that reflect their purpose (e.g., `data_processing`, `plotting_utils`).
* Import modules using explicit relative imports when working within the same package (e.g., `from . import data_processing`).
* Avoid circular dependencies between modules.
### 1.3 Consistent Variable Naming
### 1.4 Component Architecture
Use the standard variable names for `Figure` and `Axes` objects.
* **Data Abstraction Layer:** Create modules dedicated to data loading, cleaning, and transformation. This isolates the visualization logic from the data source.
* **Plotting Abstraction Layer:** Develop reusable plotting functions that encapsulate common Matplotlib configurations. This promotes code reuse and consistency.
* **Configuration Management:** Store plot configurations (colors, styles, labels) in a separate configuration file (e.g., a JSON or YAML file). This allows for easy modification of plot aesthetics without changing the code.
* **Orchestration Layer:** A main script responsible for calling the data loading, processing and plotting functions and assembling the final visualization, like `main.py` in the example structure above.
❌ BAD:
```python
my_figure_object, my_axis_object = plt.subplots()
```
### 1.5 Code Splitting Strategies
✅ GOOD:
```python
fig, ax = plt.subplots()
# For multiple axes
fig, axs = plt.subplots(2, 2)
```
* **Functional Decomposition:** Break down complex plotting tasks into smaller, manageable functions.
* **Class-Based Organization:** Use classes to represent complex plot structures (e.g., a custom chart type) or to manage plot state (see section on State Management below).
* **Separate Data Handling:** Ensure functions which load and clean data are distinct from plotting functionalities.
* **Configuration Driven:** Define plot styles and settings in external configuration files (JSON, YAML) which are then read by plotting functions, allowing easy style modifications.
### 1.4 `rcParams` Access
## 2. Common Patterns and Anti-patterns
Always access `rcParams` via `mpl.rcParams` to avoid import issues in early-loaded modules.
Recognizing and applying appropriate design patterns can significantly improve the quality of your Matplotlib code. Similarly, awareness of anti-patterns can help you avoid common pitfalls.
❌ BAD:
```python
from matplotlib import rcParams
rcParams['figure.figsize'] = (10, 6)
```
### 2.1 Design Patterns
✅ GOOD:
```python
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (10, 6)
```
* **Object-Oriented API:** Prioritize using Matplotlib's object-oriented API (`fig, ax = plt.subplots()`) over the stateful `pyplot` interface. The object-oriented API provides greater flexibility and control, especially for complex plots.
* **Template Method:** Create a base class for common plot types with abstract methods for data loading and customization. Subclasses can then implement these methods to create specific plots.
* **Strategy Pattern:** Define different plotting strategies as separate classes. The main plotting function can then dynamically choose the appropriate strategy based on the data or user input.
* **Observer Pattern:** Useful in interactive plots where changes in one element (e.g., a slider) trigger updates in other elements of the plot. Matplotlib's event handling system can be leveraged for this pattern.
## 2. Common Patterns & Anti-patterns
### 2.2 Recommended Approaches for Common Tasks
### 2.1 Handling Dense Data
* **Creating Subplots:** Use `plt.subplots()` to create figures with multiple subplots. Specify the number of rows and columns, and unpack the returned figure and axes objects for individual manipulation.
* **Customizing Plots:** Use the `ax.set()` method to set plot titles, axis labels, legends, and other properties. For more fine-grained control, use methods like `ax.set_xlabel()`, `ax.set_ylabel()`, `ax.set_title()`, etc.
* **Adding Legends:** Use `ax.legend()` to add a legend to the plot. Customize the legend appearance and location as needed.
* **Saving Plots:** Use `fig.savefig()` to save plots to files. Specify the desired file format, resolution (dpi), and other options.
* **Working with Color Maps:** Use `cmap` argument in plotting functions to apply color maps to your data. Consider using perceptually uniform color maps to avoid visual distortions.
* **Handling Date Data:** Matplotlib provides excellent support for date data. Use `matplotlib.dates` module to format and manipulate dates on the axes. Use `dateutil.parser` for flexible date parsing.
* **Interactive plots:** Utilize Matplotlib's interactive capabilities with `plt.ion()` and `plt.ioff()` to update plots dynamically in a loop. Use widgets such as sliders and buttons for user interaction.
For dense scatter plots, use `alpha` for transparency. For very large collections, rasterize them to keep file sizes manageable.
### 2.3 Anti-patterns and Code Smells
✅ GOOD:
```python
# Dense scatter plot with transparency
ax.scatter(X, Y, s=40, c='C1', alpha=0.1)
* **Excessive Use of `pyplot` Interface:** Relying heavily on the `pyplot` interface can lead to tightly coupled and less maintainable code. Embrace the object-oriented API for better structure and flexibility.
* **Hardcoding Plot Configurations:** Avoid hardcoding plot configurations (colors, styles, labels) directly in the code. Use configuration files or dictionaries to manage these settings.
* **Copy-Pasting Plotting Code:** Duplicating plotting code across multiple scripts leads to redundancy and makes it difficult to maintain consistency. Create reusable plotting functions instead.
* **Ignoring Performance Considerations:** Creating very complex plots with huge amount of data without considering optimization can result in slow rendering and high memory usage.
* **Not Handling Exceptions:** Failure to handle potential exceptions (e.g., file not found, invalid data) can lead to unexpected program termination.
* **Overplotting:** In scatter plots, overlapping data points can obscure the underlying distribution. Use techniques like transparency (`alpha`) or jitter to mitigate overplotting.
# Rasterizing large collections for PDF/SVG output
ax.scatter(X_large, Y_large, rasterized=True)
fig.savefig("rasterized_figure.pdf", dpi=600)
```
### 2.4 State Management
### 2.2 Publication-Quality Styling
* **Encapsulate Plot State:** Use classes or functions to encapsulate the state of a plot. This makes it easier to manage complex plot configurations and ensures that plots are rendered consistently.
* **Avoid Global State:** Minimize the use of global variables to store plot state. This can lead to unexpected side effects and make it difficult to reason about the code.
* **Use Configuration Objects:** Create configuration objects to store plot settings. This allows you to easily modify the appearance of your plots without changing the code.
Use custom linestyles with rounded caps and text path effects for professional figures.
### 2.5 Error Handling
✅ GOOD:
```python
# Rounded dotted lines
ax.plot([0, 1], [0, 0], "C1", linestyle=(0, (0.01, 1)), dash_capstyle="round")
* **Use `try...except` Blocks:** Wrap potentially problematic code (e.g., file I/O, data processing) in `try...except` blocks to handle exceptions gracefully.
* **Log Errors:** Use the `logging` module to log errors and warnings. This helps you identify and diagnose problems in your code.
* **Provide Informative Error Messages:** When raising exceptions, provide informative error messages that help the user understand the cause of the problem.
* **Validate Inputs:** Before using data in your plots, validate the data to ensure it is in the expected format and range. Handle invalid data appropriately.
* **Custom Exceptions:** Create custom exception classes for specific error conditions in your plotting code. This improves code readability and makes it easier to handle errors.
# Text outline for visibility
import matplotlib.patheffects as fx
text = ax.text(0.5, 0.1, "Label", transform=ax.transAxes)
text.set_path_effects([
fx.Stroke(linewidth=3, foreground='white'),
fx.Normal()
])
```
### 2.3 Managing Plot Margins
Always call `fig.tight_layout()` before saving or showing a figure to automatically adjust subplot parameters for a tight layout.
❌ BAD:
```python
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig("plot_with_margins.png") # May have excessive whitespace
```
✅ GOOD:
```python
fig, ax = plt.subplots()
ax.plot(x, y)
fig.tight_layout() # Adjusts subplot params for tight layout
fig.savefig("plot_tight.png")
```
### 2.4 `**kwargs` Usage
Reserve `**kwargs` for pass-through arguments. Explicitly define keyword-only arguments for parameters consumed locally.
❌ BAD:
```python
def my_plot_function(x, y, **kwargs):
label = kwargs.pop('label', 'default') # Consuming from kwargs
ax.plot(x, y, label=label, **kwargs)
```
✅ GOOD:
```python
def my_plot_function(x, y, *, label='default', **kwargs): # Keyword-only 'label'
ax.plot(x, y, label=label, **kwargs)
```
## 3. Performance Considerations
Performance is a crucial aspect when dealing with large datasets or complex visualizations. Optimizing your code can significantly improve rendering speed and reduce memory consumption.
### 3.1 Avoid `plt.show()` in Libraries/Functions
### 3.1 Optimization Techniques
`plt.show()` blocks execution and is intended for interactive display. In reusable code, return the `Figure` object or save it directly. This allows callers to decide how to handle the figure.
* **Vectorization:** Use NumPy's vectorized operations to perform calculations on entire arrays instead of looping through individual elements. This can significantly improve performance.
* **Data Aggregation:** Aggregate data before plotting it to reduce the number of data points. This can be especially helpful for large datasets.
* **Use Efficient Plot Types:** Choose plot types that are appropriate for the data and the visualization goal. For example, use histograms to visualize distributions instead of scatter plots.
* **Limit Data Points:** Plot only the necessary data points. Avoid plotting unnecessary details that do not contribute to the visualization.
* **Caching:** Cache intermediate results to avoid redundant calculations. This can be helpful for computationally intensive tasks.
* **Simplify Complex Geometries:** Reduce the complexity of plot elements by simplifying geometries or using approximations.
❌ BAD:
```python
def generate_plot(data):
fig, ax = plt.subplots()
ax.plot(data)
plt.show() # Blocks execution, not suitable for libraries
```
### 3.2 Memory Management
✅ GOOD:
```python
def generate_plot(data) -> mpl_figure.Figure:
fig, ax = plt.subplots()
ax.plot(data)
return fig
* **Release Unused Memory:** Explicitly release memory occupied by large data structures when they are no longer needed. Use `del` statement to remove references to objects.
* **Use Generators:** Use generators to process large datasets in chunks instead of loading the entire dataset into memory. This can significantly reduce memory consumption.
* **Avoid Creating Copies:** Avoid creating unnecessary copies of data. Use in-place operations whenever possible.
* **Sparse Data Structures:** For sparse datasets, consider using sparse data structures to reduce memory consumption.
# In main script:
if __name__ == "__main__":
my_data = [1, 5, 2, 8]
plot_fig = generate_plot(my_data)
plot_fig.savefig("my_generated_plot.pdf")
# If interactive display is desired:
# plt.show()
```
### 3.3 Rendering Optimization
### 3.2 Explicitly Close Figures
* **Use Blitting:** Use blitting to redraw only the parts of the plot that have changed. This can significantly improve rendering speed for interactive plots.
* **Reduce Figure Size:** Reduce the size of the figure to improve rendering speed. Smaller figures require less memory and processing power.
* **Rasterize Vector Graphics:** For very complex plots, consider rasterizing vector graphics to reduce rendering time. Use the `rasterized=True` option in plotting functions.
* **Use Hardware Acceleration:** Ensure that Matplotlib is using hardware acceleration if available. This can significantly improve rendering speed.
When creating many figures programmatically (e.g., in loops or batch processing), explicitly close them to prevent memory leaks.
### 3.4 Bundle Size Optimization
✅ GOOD:
```python
for i in range(100):
fig, ax = plt.subplots()
ax.plot([0, i], [i, 0])
fig.savefig(f"plot_{i}.png")
plt.close(fig) # Essential for memory management
```
* This is generally less relevant for Matplotlib as it's a backend library, not typically bundled for frontend use.
* However, if integrating Matplotlib plots into web applications, pre-render the images on the server-side and serve static images to the client.
## 4. Type Hints
### 3.5 Lazy Loading
Embrace type hints for all new and modified public APIs. Use stub files (`.pyi`) where appropriate, or inline for simpler modules like `pyplot.py`.
* Delay loading large datasets until they are needed. This can improve the startup time of your application.
* Load only the data that is visible in the plot. As the user zooms or pans, load additional data as needed.
* Use lazy loading techniques to create thumbnails or previews of plots without rendering the full plot.
✅ GOOD:
```python
from typing import List
import matplotlib.figure as mpl_figure
import matplotlib.axes as mpl_axes
## 4. Security Best Practices
def plot_data(x: List[float], y: List[float], color: str = 'blue') -> mpl_figure.Figure:
"""Plots x and y data on a new figure."""
fig, ax = plt.subplots()
ax.plot(x, y, color=color)
return fig
```
While Matplotlib is primarily a visualization library, security considerations are important when dealing with untrusted data or integrating Matplotlib into web applications.
## 5. Virtual Environments & Packaging
### 4.1 Common Vulnerabilities
Always use virtual environments (`venv`, `conda`) for dependency management. Pin Matplotlib to a specific stable version in `requirements.txt` or `pyproject.toml` to ensure reproducibility.
* **Code Injection:** If user-provided data is used to construct plot commands, it could lead to code injection vulnerabilities. Always sanitize user input before using it in plot commands.
* **Denial of Service (DoS):** Creating extremely complex plots with large datasets can consume excessive resources and lead to DoS attacks. Implement resource limits and input validation to prevent this.
* **Cross-Site Scripting (XSS):** If integrating Matplotlib plots into web applications, be careful about how the plots are displayed. Sanitize any user-provided data that is displayed in the plot to prevent XSS attacks.
* **Data Leakage:** Be cautious about displaying sensitive data in plots. Ensure that the data is properly anonymized or obfuscated before plotting it.
✅ GOOD (`requirements.txt`):
```
matplotlib==3.10.0
numpy==1.26.0
pandas==2.1.0
seaborn==0.13.0
```
### 4.2 Input Validation
## 6. Testing Approaches
* **Validate Data Types:** Ensure that the data used for plotting is of the expected type (e.g., numeric, date).
* **Validate Data Ranges:** Ensure that the data falls within the expected range. This can prevent unexpected behavior and potential errors.
* **Sanitize User Input:** If user input is used to generate plot commands, sanitize the input to prevent code injection vulnerabilities.
* **Limit Input Size:** Limit the size of the input data to prevent DoS attacks.
Integrate `ruff check --fix` into your CI/CD pipeline. Add specific tests for new features and bug fixes, especially image comparison tests for visual changes.
### 4.3 Authentication and Authorization
✅ GOOD (Example test structure):
```python
# tests/test_plotting.py
import matplotlib.pyplot as plt
import numpy as np
from my_module import generate_plot # Assuming generate_plot is in my_module
* This section is generally less relevant for Matplotlib, as it doesn't typically handle user authentication directly.
* If the visualizations are part of a larger application that has authentication, ensure that the correct users are authenticated before showing the plots.
* Implement authorization to control which users have access to specific plots or data.
def test_generate_plot_output():
data = np.array([1, 2, 3, 4, 5])
fig = generate_plot(data)
assert isinstance(fig, plt.Figure)
assert len(fig.axes) == 1
# Add more assertions, e.g., check title, labels, data points
plt.close(fig) # Clean up figure after test
### 4.4 Data Protection
* **Anonymize Sensitive Data:** Before plotting sensitive data, anonymize or obfuscate it to protect user privacy.
* **Use Secure Storage:** Store sensitive data securely, using encryption and access controls.
* **Comply with Regulations:** Ensure that your data handling practices comply with relevant regulations (e.g., GDPR, HIPAA).
### 4.5 Secure API Communication
* If integrating Matplotlib plots into web applications, use secure API communication protocols (e.g., HTTPS).
* Validate and sanitize data received from APIs before using it in plots.
* Implement rate limiting to prevent API abuse.
## 5. Testing Approaches
Thorough testing is essential to ensure the quality and reliability of your Matplotlib code. Unit tests, integration tests, and end-to-end tests all play a role in validating different aspects of your code.
### 5.1 Unit Testing
* **Test Individual Functions and Classes:** Write unit tests to verify the behavior of individual functions and classes in your plotting code.
* **Use Assertions:** Use assertions to check that the output of your functions is as expected.
* **Test Edge Cases:** Test edge cases and boundary conditions to ensure that your code handles unexpected input gracefully.
* **Test Error Handling:** Test that your code handles exceptions correctly.
* **Use Mocking:** Use mocking to isolate units of code from their dependencies. This makes it easier to test units of code in isolation.
* **Parameterize Tests:** Write parameterized tests to test the same function with different inputs and expected outputs. This reduces code duplication and improves test coverage.
### 5.2 Integration Testing
* **Test Interactions Between Modules:** Write integration tests to verify that different modules in your plotting code work together correctly.
* **Test Data Flow:** Test the flow of data through your plotting pipeline to ensure that data is processed correctly.
* **Test Plot Rendering:** Test that plots are rendered correctly and that they contain the expected elements.
* **Test Data Integration:** Test the integration with data sources, ensuring that the data is loaded correctly.
### 5.3 End-to-End Testing
* **Test the Entire Plotting Workflow:** Write end-to-end tests to verify that the entire plotting workflow works correctly, from data loading to plot rendering.
* **Simulate User Interactions:** Simulate user interactions (e.g., zooming, panning, clicking) to test the behavior of interactive plots.
* **Verify Visual Output:** Use visual testing tools to compare the rendered plots against baseline images. This can help you detect visual regressions.
### 5.4 Test Organization
* **Create a Separate Test Directory:** Create a separate directory for your tests.
* **Use Descriptive Test Names:** Use descriptive test names that indicate what the test is verifying.
* **Organize Tests by Module:** Organize your tests by module to make it easier to find and run tests.
* **Use Test Runners:** Use test runners (e.g., `pytest`, `unittest`) to run your tests and generate reports.
### 5.5 Mocking and Stubbing
* **Mock External Dependencies:** Mock external dependencies (e.g., data sources, APIs) to isolate units of code from their dependencies.
* **Stub Function Calls:** Stub function calls to control the behavior of functions that are called by the code under test.
* **Use Mocking Libraries:** Use mocking libraries (e.g., `unittest.mock`, `pytest-mock`) to simplify the process of creating mocks and stubs.
## 6. Common Pitfalls and Gotchas
Knowing common pitfalls and gotchas can save you a lot of time and frustration when working with Matplotlib.
### 6.1 Frequent Mistakes
* **Incorrect Axis Limits:** Forgetting to set the correct axis limits can result in plots that are difficult to interpret.
* **Missing Labels and Titles:** Forgetting to add labels and titles to plots makes them less informative and harder to understand.
* **Overlapping Labels:** Overlapping labels can make plots difficult to read. Use techniques like rotation or alignment to avoid overlapping labels.
* **Incorrect Color Maps:** Using inappropriate color maps can distort the data and make it difficult to interpret the plots.
* **Not Handling Missing Data:** Not handling missing data can result in unexpected errors or incorrect plots. Use appropriate techniques to handle missing data (e.g., imputation, filtering).
* **Ignoring Aspect Ratio:** Ignoring the aspect ratio of the plot can result in distorted visualizations. Use `ax.set_aspect('equal')` to ensure that the aspect ratio is correct.
### 6.2 Edge Cases
* **Empty Datasets:** Handle the case where the dataset is empty gracefully.
* **Non-Numeric Data:** Handle the case where the data contains non-numeric values.
* **Infinite Values:** Handle the case where the data contains infinite values.
* **NaN Values:** Handle the case where the data contains NaN (Not a Number) values.
* **Large Datasets:** Handle the case where the dataset is very large. Use techniques like data aggregation and lazy loading to improve performance.
### 6.3 Version-Specific Issues
* **API Changes:** Be aware of API changes between different versions of Matplotlib. Consult the documentation for the specific version you are using.
* **Compatibility Issues:** Be aware of compatibility issues between Matplotlib and other libraries (e.g., NumPy, Pandas).
* **Bug Fixes:** Be aware of bug fixes in newer versions of Matplotlib. Upgrading to the latest version may resolve known issues.
### 6.4 Compatibility Concerns
* **NumPy Version:** Ensure that your NumPy version is compatible with your Matplotlib version.
* **Pandas Version:** Ensure that your Pandas version is compatible with your Matplotlib version.
* **Operating System:** Be aware of potential compatibility issues between Matplotlib and different operating systems (e.g., Windows, macOS, Linux).
* **Backend Rendering Engine:** Be aware of potential compatibility issues between Matplotlib and different backend rendering engines (e.g., Agg, TkAgg, WebAgg).
### 6.5 Debugging Strategies
* **Use Print Statements:** Use print statements to inspect the values of variables and data structures.
* **Use Debuggers:** Use debuggers (e.g., `pdb`, `ipdb`) to step through your code and examine the state of the program.
* **Use Logging:** Use logging to record events and errors in your code. This can help you identify and diagnose problems.
* **Simplify the Plot:** Simplify the plot to isolate the source of the problem. Remove unnecessary elements or reduce the amount of data being plotted.
* **Check the Documentation:** Consult the Matplotlib documentation for information about specific functions and methods.
* **Search Online:** Search online for solutions to common problems. Stack Overflow and other online forums can be valuable resources.
* **Isolate the issue**: Comment out portions of the plotting routine to isolate which sections are causing unexpected behavior.
## 7. Tooling and Environment
Using the right tools and environment can significantly improve your productivity and the quality of your Matplotlib code.
### 7.1 Recommended Development Tools
* **IDE:** Use an Integrated Development Environment (IDE) such as VS Code, PyCharm, or Spyder. These tools provide features such as code completion, debugging, and testing.
* **Jupyter Notebooks:** Use Jupyter Notebooks for interactive exploration and development.
* **Virtual Environments:** Use virtual environments to isolate project dependencies.
* **Version Control:** Use version control systems (e.g., Git) to track changes to your code.
### 7.2 Build Configuration
* **Use `requirements.txt`:** Create a `requirements.txt` file to specify project dependencies. Use `pip freeze > requirements.txt` to generate the file.
* **Use `setup.py`:** Create a `setup.py` file to package your plotting code into a reusable library. This file defines the metadata about the library (name, version, author, etc.) and the dependencies required to install it.
* **Use `pyproject.toml`:** For modern projects, consider using `pyproject.toml` to manage build dependencies and configurations. It offers a more standardized and flexible approach compared to `setup.py`.
### 7.3 Linting and Formatting
* **Use Linters:** Use linters (e.g., `flake8`, `pylint`) to identify potential errors and style violations in your code.
* **Use Formatters:** Use formatters (e.g., `black`, `autopep8`) to automatically format your code according to PEP 8 style guidelines.
* **Configure IDE:** Configure your IDE to automatically run linters and formatters when you save your code.
### 7.4 Deployment Best Practices
* **Containerization:** Use containerization technologies (e.g., Docker) to create portable and reproducible deployment environments. This ensures that your plotting code runs consistently across different platforms.
* **Cloud Platforms:** Deploy your plotting code to cloud platforms (e.g., AWS, Azure, Google Cloud) for scalability and reliability.
* **Serverless Functions:** Consider using serverless functions to deploy individual plotting tasks as independent units. This can be a cost-effective way to run plotting code on demand.
### 7.5 CI/CD Integration
* **Use CI/CD Pipelines:** Set up Continuous Integration/Continuous Deployment (CI/CD) pipelines to automate the build, test, and deployment process. This helps ensure that your code is always in a releasable state.
* **Run Tests Automatically:** Configure your CI/CD pipeline to automatically run tests whenever you commit code. This helps you catch errors early.
* **Deploy Automatically:** Configure your CI/CD pipeline to automatically deploy your code to production environments after it has passed all tests.
# For visual regression testing (requires specific tooling like pytest-mpl)
# def test_plot_visual_regression(mpl_image_compare):
# fig = generate_plot(np.array([1, 5, 2, 8]))
# assert mpl_image_compare(fig, "expected_plot_output")
```