🎨 Drawing Pictures with Numbers: Line Plots & Markers
The Story of the Magic Pencil
Imagine you have a magic pencil that can draw pictures using numbers. This pencil is called Matplotlib! Instead of drawing freehand, you tell it exactly where to put dots and lines using coordinates—like playing connect-the-dots, but you’re the one deciding where the dots go!
🖊️ Basic Line Plotting: Your First Drawing
What is a Line Plot?
Think of it like this: You’re tracking how many cookies you eat each day of the week.
| Day | Cookies |
|---|---|
| Mon | 2 |
| Tue | 4 |
| Wed | 3 |
| Thu | 5 |
| Fri | 7 |
A line plot connects these points with a line, showing how things change over time!
Your First Line Plot
import matplotlib.pyplot as plt
days = [1, 2, 3, 4, 5]
cookies = [2, 4, 3, 5, 7]
plt.plot(days, cookies)
plt.show()
What happens:
days= positions on the horizontal line (x-axis)cookies= positions going up (y-axis)plt.plot()= draws the line connecting all pointsplt.show()= displays your drawing!
graph TD A[Your Data] --> B[plt.plot x, y] B --> C[Line Connects Points] C --> D[plt.show] D --> E[See Your Graph!]
📊 Plotting Multiple Lines: Many Stories, One Picture
Why Multiple Lines?
What if you want to compare YOUR cookie eating with your friend’s? Put both on the same picture!
Example: You vs Your Friend
import matplotlib.pyplot as plt
days = [1, 2, 3, 4, 5]
my_cookies = [2, 4, 3, 5, 7]
friend_cookies = [3, 2, 4, 6, 5]
plt.plot(days, my_cookies)
plt.plot(days, friend_cookies)
plt.show()
Magic! Each plt.plot() adds a new line. Matplotlib automatically picks different colors!
Adding Labels So We Know Who’s Who
plt.plot(days, my_cookies, label='Me')
plt.plot(days, friend_cookies, label='Friend')
plt.legend()
plt.show()
The label gives each line a name. plt.legend() shows a little box explaining which line is which!
✨ Line Styling: Making Lines Beautiful
The 3 Magic Properties
Every line has three things you can change:
| Property | What It Does | Example |
|---|---|---|
| color | Line color | 'red', 'blue', 'green' |
| linestyle | Solid, dashed, dotted | '-', '--', ':', '-.' |
| linewidth | Thin or thick | 1, 2, 3, etc. |
Line Style Cheat Sheet
'-' = ────────── (solid)
'--' = - - - - - (dashed)
':' = ........... (dotted)
'-.' = -·-·-·-·- (dash-dot)
Example: A Styled Line
plt.plot(days, cookies,
color='red',
linestyle='--',
linewidth=2)
plt.show()
This creates a red, dashed, thick line!
Color Options
You can use:
- Names:
'red','blue','green','orange' - Short codes:
'r','b','g','k'(black) - Hex codes:
'#FF5733'
⭐ Marker Customization: Highlighting Your Points
What Are Markers?
Markers are the dots at each data point. Without markers, you only see the line. With markers, you see exactly where each value is!
Basic Markers
plt.plot(days, cookies, marker='o')
plt.show()
Now each point has a circle on it!
Marker Style Menu
| Symbol | Shape |
|---|---|
'o' |
Circle ● |
's' |
Square ■ |
'^' |
Triangle ▲ |
'*' |
Star ★ |
'D' |
Diamond ◆ |
'x' |
X mark ✕ |
'+' |
Plus + |
Customizing Markers Further
plt.plot(days, cookies,
marker='o',
markersize=10,
markerfacecolor='yellow',
markeredgecolor='red',
markeredgewidth=2)
plt.show()
What each does:
marker='o'→ Circle shapemarkersize=10→ How bigmarkerfacecolor='yellow'→ Fill colormarkeredgecolor='red'→ Border colormarkeredgewidth=2→ Border thickness
graph TD A[Marker] --> B[Shape: o, s, ^, *] A --> C[Size: markersize] A --> D[Fill: markerfacecolor] A --> E[Border: markeredgecolor]
🎯 Format Strings: The Shortcut Masters
The One-Line Wonder
Instead of writing color=, linestyle=, marker= separately, you can use a format string—a tiny code that does it all!
The Formula
plt.plot(x, y, 'color + marker + linestyle')
Examples
| Format String | Meaning |
|---|---|
'ro' |
Red circles (no line) |
'b-' |
Blue solid line |
'g--' |
Green dashed line |
'k^' |
Black triangles |
'm*-' |
Magenta stars with solid line |
'c:s' |
Cyan dotted line with squares |
Color Codes
| Code | Color |
|---|---|
'r' |
Red |
'g' |
Green |
'b' |
Blue |
'c' |
Cyan |
'm' |
Magenta |
'y' |
Yellow |
'k' |
Black |
'w' |
White |
Practical Example
# The long way
plt.plot(x, y, color='red',
marker='o', linestyle='--')
# The shortcut way (same result!)
plt.plot(x, y, 'ro--')
Both create a red line with circles and dashes!
Combining Everything
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
plt.plot(x, y1, 'ro-', label='Squares')
plt.plot(x, y2, 'bs--', label='Linear')
plt.legend()
plt.show()
'ro-'= Red circles with solid line'bs--'= Blue squares with dashed line
🎪 Putting It All Together
The Complete Recipe
import matplotlib.pyplot as plt
# Your data
days = [1, 2, 3, 4, 5]
temp_morning = [15, 16, 14, 17, 18]
temp_evening = [20, 22, 19, 23, 25]
# Plot with full styling
plt.plot(days, temp_morning,
'b^-', # Blue triangles, solid
label='Morning',
linewidth=2,
markersize=8)
plt.plot(days, temp_evening,
'ro--', # Red circles, dashed
label='Evening',
linewidth=2,
markersize=8)
# Make it pretty
plt.xlabel('Day')
plt.ylabel('Temperature')
plt.title('Daily Temperatures')
plt.legend()
plt.show()
🌟 Quick Reference
graph TD A[plt.plot] --> B[Basic: x, y] B --> C[+ Format String] B --> D[+ Style Parameters] C --> E["'ro--' = red circle dashed"] D --> F[color, linestyle, marker] D --> G[linewidth, markersize]
Remember These Magic Words
| What You Want | Code |
|---|---|
| Draw a line | plt.plot(x, y) |
| Multiple lines | Call plt.plot() multiple times |
| Change color | color='red' or 'r' |
| Change line style | linestyle='--' |
| Add markers | marker='o' |
| Quick styling | 'ro--' (color+marker+line) |
| Show the graph | plt.show() |
🚀 You Did It!
You now know how to:
- Draw basic lines with
plt.plot(x, y) - Compare data with multiple lines
- Style lines with colors and dashes
- Add markers to highlight points
- Use shortcuts with format strings
Your magic pencil is ready. Go draw some amazing data pictures! 🎨