Hello everyone, today I want to share a super practical Python plotting technique! As a data analyst, I deal with charts every day, but I really get tired of the same old lines! In fact, just mastering a few parameters can instantly elevate your line charts! 📈
🌟 Line Style Trio
1️⃣ Custom Line Types
Use the <span>linestyle</span> parameter (abbreviated as <span>ls</span>) to easily switch styles:
- Dotted line:
<span>ls='--'</span> - Dashed line:
<span>ls=':'</span> - Dash-dot line:
<span>ls='-.'</span>
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6,2,13,10])
plt.plot(ypoints, linestyle='dotted') # Cute dotted line!
plt.show()
2️⃣ Choose from Sixteen Colors
<span>color</span> parameter (abbreviated as <span>c</span>) enables color selection:
| Color Code | Color |
|---|---|
| ‘r’ | Red |
| ‘g’ | Green |
| ‘b’ | Blue |
| ‘c’ | Cyan |
| ‘m’ | Magenta |
| ‘y’ | Yellow |
| ‘k’ | Black |
| ‘w’ | White |
For advanced techniques, see below 👇
# Create an Instagram-style color
plt.plot(ypoints, c='#8FBC8F')
# Directly inputting color names is super convenient
plt.plot(ypoints, c='SeaGreen')
3️⃣ Adjust Line Thickness Freely
<span>linewidth</span> parameter (abbreviated as <span>lw</span>) controls the line’s presence:
plt.plot(ypoints, linewidth='12.5') # Bold important data for high visibility!
🎨 Multi-Line Plotting Secrets
Want to display multiple datasets on the same canvas? Choose from two methods:
Method 1: Automatically Generate X-Axis
y1 = np.array([3,7,5,9])
y2 = np.array([6,2,13,10])
plt.plot(y1)
plt.plot(y2) # x is automatically set to [0,1,2,3]
Method 2: Custom Coordinate Axes
x1 = np.array([0,1,2,3])
y1 = np.array([3,7,5,9])
x2 = np.array([0,1,2,3])
y2 = np.array([6,2,13,10])
plt.plot(x1,y1,x2,y2) # Precisely control each point!
💡 Practical Tips
✅ Colors support HTML color values, go collect your favorite color palettes!✅ Combining line styles yields better effects: <span>plt.plot(y, ls='--', c='hotpink', lw=3)</span>✅ When using multiple lines, remember to distinguish them with different colors, harmonizing with similar color schemes is more pleasing!
With these techniques mastered, your charts in report PPTs will no longer be criticized by your boss for being monotonous! Go try it out, and I look forward to seeing your cool chart creations in the comments!