Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matplotlib #3339

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions python/python-matplotlib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Matplotlib
description: Create beautiful data visualizations in Python

sections:
'0':
# Introduction to plotting
- python-matplotlib-intro
- python-matplotlib-intro-exercise

# Basic plot types
- python-matplotlib-line-plots
- python-matplotlib-line-plots-exercise

# Basic plot types (increasing complexity)
- python-matplotlib-scatter-plots
- python-matplotlib-scatter-plots-exercise

# More plot types
- python-matplotlib-bar-plots
- python-matplotlib-bar-plots-exercise
- python-matplotlib-histograms
- python-matplotlib-histograms-exercise

# Layout and organization
- python-matplotlib-subplots
- python-matplotlib-subplots-exercise
- python-matplotlib-legends-grids
- python-matplotlib-legends-grids-exercise

# Advanced features
- python-matplotlib-styles
- python-matplotlib-styles-exercise
- python-matplotlib-annotations
- python-matplotlib-annotations-exercise
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Annotation Practice
description: Apply your annotation skills to create informative data stories

insights:
- python-matplotlib-annotation-practice

aspects:
- workout
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
author: enki-ai
type: normal
category: coding
setupCode:
startingPoint: |
# Welcome to the Annotation Exercise!
# You'll create an annotated analysis of website traffic

import matplotlib.pyplot as plt
import numpy as np

# Website traffic data (visitors per hour)
hours = np.arange(0, 24)
traffic = [
30, 20, 15, 10, 8, 15, # 0-5
40, 100, 150, 180, 170, 160, # 6-11
200, 190, 180, 170, 160, 150, # 12-17
120, 100, 80, 60, 45, 35 # 18-23
]

# Your tasks:
# 1. Create a line plot of hourly traffic
# 2. Annotate the morning traffic peak with an arrow
# 3. Annotate the afternoon peak with a box
# 4. Add a text box explaining the daily pattern
# 5. Use different colors and styles for annotations
# 6. Add appropriate title and axis labels

# Write your code here:
---

# Website Traffic Analysis

---
## Content

> 👩‍💻 Create an informative visualization of website traffic patterns!

This exercise will help you practice:
- Adding informative annotations
- Using different annotation styles
- Creating text boxes
- Working with arrows
- Making data tell a story

Example of what you might create:
```python
plt.figure(figsize=(12, 6))
plt.plot(hours, traffic)

plt.annotate('Morning Peak',
xy=(9, 180),
xytext=(7, 220),
arrowprops=dict(facecolor='red'))

plt.show()
```

> 💡 Remember:
> - Place annotations where they don't overlap
> - Use clear, concise text
> - Make arrows point to exact features
> - Consider using different styles for different types of annotations
11 changes: 11 additions & 0 deletions python/python-matplotlib/python-matplotlib-annotations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Plot Annotations
description: Learn to add informative text and arrows to your plots

insights:
- python-matplotlib-annotation-basics
- python-matplotlib-annotation-arrows
- python-matplotlib-annotation-advanced
- python-matplotlib-annotation-discussion

aspects:
- workout
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
author: enki-ai
type: normal
category: must-know
practiceQuestion:
formats:
- fill-in-the-gap
context: standalone
revisionQuestion:
formats:
- fill-in-the-gap
context: standalone
---

# Advanced Annotations

---
## Content

Let's explore more complex annotation features:

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

# Create data
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(12, 6))
plt.plot(x, y)

# Add text box with multiple lines
plt.text(1, 0.5, 'Key Features:\n' +
'• Periodic\n' +
'• Amplitude = 1\n' +
'• Period = 2π',
bbox=dict(facecolor='white',
edgecolor='blue',
boxstyle='round,pad=0.5'),
fontsize=10)

# Add rotated text
plt.text(5, -0.5, 'Rotated Label',
rotation=45,
fontsize=12)

plt.title('Advanced Text Annotations')
plt.show()
```

> 💡 Use `\n` for multi-line text and `rotation` for angled text!

You can also create custom annotation styles:

```python
# Custom annotation styles
plt.figure(figsize=(10, 6))
plt.plot(x, y)

# Annotation with custom box
plt.annotate('Important Region',
xy=(3, 0),
xytext=(4, 0.5),
bbox=dict(boxstyle='round,pad=0.5',
fc='yellow',
alpha=0.5),
arrowprops=dict(
arrowstyle='->',
connectionstyle='arc3,rad=-0.2',
color='red',
lw=2
))

# Add mathematical expression
plt.text(7, 0.5, r'$\sin(x)=\frac{e^{ix}-e^{-ix}}{2i}$',
fontsize=14)

plt.show()
```

> 🎯 Use LaTeX formatting with `r'$formula$'` for mathematical expressions!

---
## Practice

Add a multi-line text box:

```python
plt.text(x, y, ???,
bbox=dict(???='round',
???='white'))
```

- `'Line 1\nLine 2'`
- `boxstyle`
- `facecolor`
- `'Line 1/Line 2'`
- `style`
- `color`

---
## Revision

To add a mathematical expression:

```python
plt.text(x, y, ???'$\sqrt{x^2}$')
```

- `r`
- `math`
- `latex`
- `f`
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
author: enki-ai
type: normal
category: must-know
practiceQuestion:
formats:
- fill-in-the-gap
context: standalone
revisionQuestion:
formats:
- fill-in-the-gap
context: standalone
---

# Arrows and Connectors

---
## Content

Let's explore how to add arrows to annotations:

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

# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(10, 6))
plt.plot(x, y)

# Add annotation with arrow
plt.annotate('Peak',
xy=(1.5, 1.0), # Point to annotate
xytext=(3, 0.5), # Text position
arrowprops=dict(
facecolor='black',
shrink=0.05, # Shorten arrow
width=2, # Arrow width
headwidth=8 # Arrow head width
))

plt.title('Sine Wave with Arrow Annotation')
plt.show()
```

> 💡 Use `arrowprops` to customize arrow appearance!

You can create different arrow styles:

```python
# Multiple arrow styles
plt.figure(figsize=(10, 6))
plt.plot(x, y)

# Curved arrow
plt.annotate('Curved Arrow',
xy=(4.5, -0.9),
xytext=(6, -0.3),
arrowprops=dict(
facecolor='red',
connectionstyle='arc3,rad=.2'
))

# Fancy arrow
plt.annotate('Fancy Arrow',
xy=(7.5, 0.9),
xytext=(6, 0.3),
arrowprops=dict(
facecolor='green',
arrowstyle='fancy'
))

plt.show()
```

> 🎯 Different arrow styles help emphasize different features!

---
## Practice

Create a basic arrow annotation:

```python
plt.annotate('Label',
xy=(x, y),
xytext=(x2, y2),
???=dict(
???='black',
???=0.05
))
```

- `arrowprops`
- `facecolor`
- `shrink`
- `arrow`
- `color`
- `size`

---
## Revision

To create a curved arrow, use:

```python
arrowprops=dict(
???='arc3',
???=0.2
)
```

- `connectionstyle`
- `rad`
- `curve`
- `bend`
Loading