Give Your Boss a Pile of Numbers, and They'll Just See a Blur
In the previous chapter, we successfully used web scraping to capture 1,000 gaming laptop prices from competitors and saved them as a lengthy Excel (CSV) file.
You excitedly printed the spreadsheet, tossed a thick stack of paper on your boss's desk, and declared: "Boss! Look! I've uncovered all our competitors' secrets!"
The boss flipped through a few pages, frowned, and said: "Who can make sense of these密密麻麻的数字? Just tell me: What’s their main price range? Which brand has the highest average price? What should we price our new product next month?"
At this point, you froze. This is the most common mistake made by rookie data scientists: "Thinking that having data means having answers."
The human brain is naturally bad at processing thousands of raw numbers but is highly sensitive to "images, colors, and lengths."
If you could transform those 1,000 messy numbers into a sleek line chart, bar chart, or pie chart, your boss could make a business decision with just a glance.
This technique of turning cold numbers into business stories is called Data Visualization.
Python’s Dynamic Duo of Visualization: Matplotlib and Seaborn
In Python’s data science ecosystem, two libraries reign supreme for visualization:
- Matplotlib: The oldest and most foundational tool. It can create any chart you imagine, but its syntax is cumbersome, and its default charts look like relics from 1990s reports—ugly.
- Seaborn: An advanced tool built on Matplotlib. Its specialty is "creating modern, aesthetically pleasing charts with minimal syntax." It comes with beautiful commercial-grade color schemes and layouts.
In Vibe Coding’s实战, we typically use both libraries together: Seaborn for the main polished visuals and Matplotlib for fine-tuning titles and fonts.
Vibe Prompt实战: Generate Stunning Business Charts in Seconds
Now, we’ll visualize the data from laptops_price.csv into a bar chart comparing the average prices of different brands.
But here’s a huge pitfall!
If you plot the chart in Python and include Chinese text (e.g., "华硕" or "微���"), you’ll notice all Chinese characters turn into "tofu blocks" (口口口)! This happens because Python doesn’t recognize Microsoft’s Chinese fonts by default.
To fix this, we need to add a special "Chinese font defense spell" to our Vibe Prompt.
【Python Visualization Prompt】
I have a file namedlaptops_price.csv.
Its columns include:Brand(e.g., ASUS, MSI, Acer) andPrice(numeric).Please write a Python script to:
- Read the file using
pandas.- Calculate the average price per brand.
- Use
seabornandmatplotlibto create a polished bar chart (X-axis: brand, Y-axis: average price). Sort bars from highest to lowest price.- 【Critical Fix】 Ensure matplotlib supports Chinese fonts (e.g.,
SimHeiorMicrosoft JhengHei) and fixes negative signs turning into blocks.- Save the chart as
price_compare.png.
AI-Generated Masterpiece Script:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# ==========================================
# Ultimate Defense: Fix Chinese font rendering!
# Windows: 'Microsoft JhengHei'
# Mac: 'Arial Unicode MS' or 'PingFang HK'
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False # Fix negative signs
# ==========================================
def draw_price_chart():
# 1. Load data
df = pd.read_csv('laptops_price.csv')
# 2. Group by brand, calculate avg price, sort descending
avg_price_df = df.groupby('品牌')['价格'].mean().reset_index()
avg_price_df = avg_price_df.sort_values(by='价格', ascending=False)
# 3. Set canvas size and sleek Seaborn style
plt.figure(figsize=(10, 6))
sns.set_theme(style="whitegrid", font="Microsoft JhengHei")
# 4. Plot the bar chart! Use 'viridis' gradient palette
sns.barplot(
x='品牌',
y='价格',
data=avg_price_df,
palette='viridis'
)
# 5. Add professional titles and labels
plt.title('2024 Gaming Laptop Average Price by Brand', fontsize=18, fontweight='bold')
plt.xlabel('Brand', fontsize=12)
plt.ylabel('Avg Price (NTD)', fontsize=12)
# 6. Save as high-res image
plt.tight_layout() # Prevent text cutoff
plt.savefig('price_compare.png', dpi=300)
print("✅ Chart saved as price_compare.png!")
# Execute
draw_price_chart()
When you run this, a high-definition price_compare.png appears in your folder—featuring smooth gradients, crisp Chinese text, and professional styling.
You drop it into a slide and hand it to your boss. At a glance, they see: "So MSI has the highest average price. If we target the mid-range market, we should price between Acer and ASUS."
This is the power of data science. You’ve transformed cold numbers into million-dollar business decisions.
In the next chapter, we’ll combine web scraping + data analysis + automated emails to build a superbot that sends daily market reports to the entire company at 8 AM!