# Workflow: Creating and Displaying a Matplotlib Plot

## Overview

**Goal**: Create a simple matplotlib visualization using Python, save it as an image file, and display it inline within the chat interface.

**Use Case**: This workflow demonstrates how to generate dummy data visualizations, which is useful for testing, demonstrations, prototyping, or creating sample charts for documentation.

**Date Created**: 2026-02-11

---

## Prerequisites

- Python environment with the following packages installed:
  - `matplotlib`
  - `numpy`
- Access to a writable workspace directory
- Jupyter-like code execution environment (or Python REPL)

---

## Workflow Steps

### Step 1: Import Required Libraries

```python
import matplotlib.pyplot as plt
import numpy as np
```

**Description**: Import matplotlib for plotting and numpy for generating numerical data.

---

### Step 2: Generate Dummy Data

```python
# Create some dummy data
x = np.linspace(0, 10, 100)
y = np.sin(x) * np.exp(-x/10)
```

**Description**: 
- Generate 100 evenly-spaced points from 0 to 10 for the x-axis
- Create a damped sine wave by multiplying a sine function with an exponential decay
- This creates an interesting, visually appealing curve

**Parameters**:
- `np.linspace(start, stop, num)`: Creates evenly spaced values
  - `start`: 0
  - `stop`: 10
  - `num`: 100 points
- Damping factor: `exp(-x/10)` controls the rate of amplitude decay

---

### Step 3: Create the Plot

```python
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label='Damped Sine Wave')
plt.xlabel('X axis', fontsize=12)
plt.ylabel('Y axis', fontsize=12)
plt.title('Dummy Matplotlib Plot', fontsize=14, fontweight='bold')
plt.grid(True, alpha=0.3)
plt.legend()
```

**Description**: Configure and style the matplotlib plot with various visual elements.

**Configuration Parameters**:
- `figsize=(10, 6)`: Figure dimensions in inches (width, height)
- `'b-'`: Line style (blue solid line)
- `linewidth=2`: Thickness of the line
- `fontsize=12`: Size of axis labels
- `fontsize=14`: Size of title text
- `fontweight='bold'`: Makes title bold
- `alpha=0.3`: Grid transparency (0=invisible, 1=opaque)

---

### Step 4: Save the Plot

```python
# Save the plot to workspace
plt.savefig('dummy_plot.png', dpi=150, bbox_inches='tight')
```

**Description**: Export the plot as a PNG image file.

**Parameters**:
- `filename`: 'dummy_plot.png' (save to current workspace)
- `dpi=150`: Resolution (dots per inch) - higher values = better quality
- `bbox_inches='tight'`: Removes extra whitespace around the plot

**Important Note**: Avoid saving to root directory (`/`) as it may be read-only. Use relative paths or workspace directories.

---

### Step 5: Display the Plot

```python
plt.show()
print("Plot created successfully!")
```

**Description**: 
- `plt.show()`: Renders the plot (may show warning in non-interactive environments)
- Print confirmation message

---

### Step 6: Display Inline (Optional)

If using a tool with inline display capabilities:

```python
display_inline('dummy_plot.png', title='Dummy Matplotlib Plot')
```

**Description**: Show the saved image directly in the chat interface.

---

## Complete Code

Here's the complete code in one block:

```python
import matplotlib.pyplot as plt
import numpy as np

# Create some dummy data
x = np.linspace(0, 10, 100)
y = np.sin(x) * np.exp(-x/10)

# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label='Damped Sine Wave')
plt.xlabel('X axis', fontsize=12)
plt.ylabel('Y axis', fontsize=12)
plt.title('Dummy Matplotlib Plot', fontsize=14, fontweight='bold')
plt.grid(True, alpha=0.3)
plt.legend()

# Save the plot to workspace
plt.savefig('dummy_plot.png', dpi=150, bbox_inches='tight')
plt.show()

print("Plot created successfully!")
```

---

## Expected Output

### Console Output
```
Plot created successfully!
```

### File Output
- **File**: `dummy_plot.png`
- **Location**: Current workspace directory
- **Type**: PNG image
- **Resolution**: 150 DPI
- **Dimensions**: Approximately 1500 x 900 pixels

### Visual Output
A professional-looking line chart showing:
- A blue damped sine wave
- X-axis ranging from 0 to 10
- Y-axis showing the amplitude (roughly -0.5 to 1.0)
- Grid lines for easier reading
- Title: "Dummy Matplotlib Plot"
- Legend indicating "Damped Sine Wave"

---

## Troubleshooting

### Issue: "Read-only file system" Error

**Problem**: Attempting to save to root directory `/dummy_plot.png`

**Solution**: Use relative path `dummy_plot.png` or save to a writable directory

```python
# ❌ Don't do this
plt.savefig('/dummy_plot.png')

# ✅ Do this instead
plt.savefig('dummy_plot.png')
# or
plt.savefig('./output/dummy_plot.png')
```

### Issue: "FigureCanvasAgg is non-interactive" Warning

**Problem**: Warning message when calling `plt.show()` in non-interactive environment

**Solution**: This is expected behavior in script/notebook environments. The warning is harmless and doesn't affect plot generation. The plot is still saved correctly.

---

## Customization Options

### Different Plot Types

Replace the sine wave with other functions:

```python
# Linear
y = 2 * x + 1

# Quadratic
y = x ** 2

# Multiple curves
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label='Sine')
plt.plot(x, y2, label='Cosine')
```

### Color Options

```python
# Named colors
plt.plot(x, y, 'r-')  # red
plt.plot(x, y, 'g-')  # green

# Hex colors
plt.plot(x, y, color='#FF5733', linewidth=2)

# RGB tuples
plt.plot(x, y, color=(0.2, 0.4, 0.8), linewidth=2)
```

### Style Options

```python
# Different line styles
plt.plot(x, y, '--')   # dashed
plt.plot(x, y, ':')    # dotted
plt.plot(x, y, '-.')   # dash-dot

# Markers
plt.plot(x, y, 'o-')   # circles
plt.plot(x, y, 's-')   # squares
plt.plot(x, y, '^-')   # triangles
```

### Figure Size and DPI

```python
# Larger figure
plt.figure(figsize=(15, 10))

# Higher resolution for publication
plt.savefig('plot.png', dpi=300)

# Lower file size
plt.savefig('plot.png', dpi=72)
```

---

## Related Workflows

- Creating Plotly interactive charts
- Exporting matplotlib plots to PDF
- Creating subplots and multi-panel figures
- Customizing matplotlib themes and styles
- Animating matplotlib plots

---

## Tags

`matplotlib`, `visualization`, `python`, `plotting`, `numpy`, `data-visualization`, `dummy-data`, `png-export`

---

## Version History

- **v1.0** (2026-02-11): Initial workflow documentation
