Python使用Matplotlib將爬取的CSV數(shù)據(jù)變成直觀圖表
引言:為什么需要數(shù)據(jù)可視化?
當你在電商網(wǎng)站爬取了10萬條商品價格數(shù)據(jù),或是從氣象站抓取了3年的溫度記錄,面對密密麻麻的CSV表格時,是否感到無從下手?這時候,數(shù)據(jù)可視化就像給數(shù)據(jù)裝上了一副"透 視鏡",能讓你一眼看穿數(shù)據(jù)背后的規(guī)律。
本文將帶你完成一個完整的數(shù)據(jù)可視化實戰(zhàn):從爬取某招聘網(wǎng)站的職位信息,到用Matplotlib生成專業(yè)圖表。不需要數(shù)學博士背景,只需跟著步驟操作,你就能做出讓同事驚嘆的可視化報告。
一、準備階段:環(huán)境搭建與數(shù)據(jù)獲取
1.1 基礎環(huán)境配置
首先確保你的Python環(huán)境已安裝必要的庫:
pip install matplotlib pandas requests beautifulsoup4
這四個庫的分工很明確:
requests:負責發(fā)送HTTP請求獲取網(wǎng)頁BeautifulSoup4:解析HTML提取數(shù)據(jù)pandas:處理CSV數(shù)據(jù)matplotlib:繪制可視化圖表
1.2 爬取示例數(shù)據(jù)
以某招聘網(wǎng)站為例,我們爬取Python開發(fā)崗位的薪資和城市分布數(shù)據(jù):
import requests
from bs4 import BeautifulSoup
import pandas as pd
def crawl_job_data():
headers = {'User-Agent': 'Mozilla/5.0'}
jobs = []
for page in range(1, 6): # 爬取前5頁
url = f"https://example.com/jobs?page={page}&keyword=Python"
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 假設每個職位在class為'job-item'的div中
for item in soup.find_all('div', class_='job-item'):
title = item.find('h2').text.strip()
salary = item.find('span', class_='salary').text.strip()
city = item.find('span', class_='location').text.strip()
jobs.append({'title': title, 'salary': salary, 'city': city})
df = pd.DataFrame(jobs)
df.to_csv('job_data.csv', index=False)
return df
data = crawl_job_data()提示:實際爬取時需遵守網(wǎng)站的robots協(xié)議,建議添加延遲(time.sleep(2))避免被封。
二、數(shù)據(jù)清洗:讓數(shù)據(jù)"干凈"起來
爬取的原始數(shù)據(jù)往往包含臟數(shù)據(jù),比如薪資字段可能包含"15-20K/月"這樣的文本。我們需要將其轉換為數(shù)值:
def clean_salary(salary_str):
try:
# 提取數(shù)字部分,例如"15-20K/月"→[15,20]
parts = salary_str.replace('K', '').replace('/月', '').split('-')
if len(parts) == 2:
return (float(parts[0]) + float(parts[1])) / 2 # 取平均值
return float(parts[0])
except:
return None
data['salary'] = data['salary'].apply(clean_salary)
data = data.dropna(subset=['salary']) # 刪除薪資為空的記錄對于城市字段,我們可能需要統(tǒng)計每個城市的職位數(shù)量:
city_counts = data['city'].value_counts().head(10) # 取前10個城市
三、基礎圖表制作:柱狀圖與折線圖
3.1 城市職位分布柱狀圖
柱狀圖最適合展示分類數(shù)據(jù)的對比:
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6))
city_counts.plot(kind='bar', color='#4C72B0')
plt.title('Python崗位城市分布', fontsize=16)
plt.xlabel('城市', fontsize=12)
plt.ylabel('職位數(shù)量', fontsize=12)
plt.xticks(rotation=45) # 旋轉x軸標簽
plt.grid(axis='y', linestyle='--', alpha=0.7)
# 在柱子上方顯示數(shù)值
for i, v in enumerate(city_counts):
plt.text(i, v + 50, str(v), ha='center')
plt.tight_layout() # 自動調整布局
plt.savefig('city_distribution.png', dpi=300)
plt.show()效果說明:
- 藍色柱子高度代表職位數(shù)量
- 每個柱子上方顯示具體數(shù)值
- 網(wǎng)格線輔助讀數(shù)
- 圖片保存為300dpi高清圖
3.2 薪資趨勢折線圖
假設我們按經驗要求分組統(tǒng)計平均薪資:
# 假設數(shù)據(jù)中有'experience'字段
exp_salary = data.groupby('experience')['salary'].mean()
plt.figure(figsize=(10, 5))
exp_salary.plot(kind='line', marker='o', color='#DD8452')
plt.title('不同經驗要求的平均薪資', fontsize=14)
plt.xlabel('經驗要求', fontsize=11)
plt.ylabel('平均薪資(K)', fontsize=11)
plt.grid(True, linestyle=':', alpha=0.6)
# 標記數(shù)據(jù)點
for x, y in zip(exp_salary.index, exp_salary):
plt.text(x, y, f'{y:.1f}K', ha='center', va='bottom')
plt.show()關鍵點:
- 橙色折線連接各數(shù)據(jù)點
- 每個數(shù)據(jù)點上方顯示具體數(shù)值
- 虛線網(wǎng)格增強可讀性
四、進階圖表:散點圖與餅圖
4.1 薪資與經驗相關性散點圖
探索薪資與工作經驗是否相關:
# 假設我們隨機生成一些經驗年限數(shù)據(jù)
import numpy as np
np.random.seed(42)
data['experience_years'] = np.random.randint(1, 11, size=len(data))
plt.figure(figsize=(10, 8))
plt.scatter(data['experience_years'], data['salary'],
alpha=0.6, color='#55A868', s=50) # s控制點大小
plt.title('工作經驗與薪資關系', fontsize=15)
plt.xlabel('工作經驗(年)', fontsize=12)
plt.ylabel('薪資(K)', fontsize=12)
# 添加趨勢線
z = np.polyfit(data['experience_years'], data['salary'], 1)
p = np.poly1d(z)
plt.plot(data['experience_years'], p(data['experience_years']),
"r--", linewidth=1)
plt.grid(True)
plt.show()解讀:
- 綠色散點表示各個數(shù)據(jù)點
- 紅色虛線為線性趨勢線
- 透明度(alpha)設置避免點重疊
4.2 薪資范圍餅圖
展示不同薪資區(qū)間的職位占比:
# 創(chuàng)建薪資區(qū)間
bins = [0, 10, 15, 20, 30]
labels = ['10K以下', '10-15K', '15-20K', '20K以上']
data['salary_range'] = pd.cut(data['salary'], bins=bins, labels=labels)
range_counts = data['salary_range'].value_counts()
plt.figure(figsize=(8, 8))
plt.pie(range_counts, labels=range_counts.index,
autopct='%1.1f%%', startangle=90,
colors=['#C44E52', '#CCB974', '#64776D', '#798E87'])
plt.title('Python崗位薪資分布', fontsize=15)
plt.tight_layout()
plt.show()技巧:
autopct顯示百分比startangle控制起始角度- 自定義顏色讓圖表更美觀
五、多圖表組合:子圖與儀表盤
5.1 創(chuàng)建包含多個圖表的儀表盤
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
fig.suptitle('Python崗位數(shù)據(jù)分析儀表盤', fontsize=18)
# 子圖1:城市分布
axes[0,0].bar(city_counts.index, city_counts.values, color='#4C72B0')
axes[0,0].set_title('城市分布')
axes[0,0].tick_params(axis='x', rotation=45)
# 子圖2:經驗-薪資散點
axes[0,1].scatter(data['experience_years'], data['salary'],
alpha=0.5, color='#55A868')
axes[0,1].set_title('經驗與薪資關系')
# 子圖3:薪資范圍餅圖
axes[1,0].pie(range_counts, labels=None, autopct='%1.1f%%',
colors=['#C44E52', '#CCB974', '#64776D', '#798E87'])
axes[1,0].set_title('薪資分布')
# 子圖4:經驗要求折線圖
exp_salary.plot(kind='line', marker='o', ax=axes[1,1], color='#DD8452')
axes[1,1].set_title('經驗要求與平均薪資')
plt.tight_layout()
plt.subplots_adjust(top=0.92) # 調整標題位置
plt.savefig('dashboard.png', dpi=300)
plt.show()優(yōu)勢:
- 單張圖片展示多個維度
- 適合放入分析報告
- 保持統(tǒng)一的風格
六、樣式優(yōu)化:讓圖表更專業(yè)
6.1 全局樣式設置
plt.style.use('seaborn') # 使用seaborn樣式
# 或自定義樣式
plt.rcParams.update({
'font.family': 'Microsoft YaHei', # 中文支持
'axes.titlesize': 14,
'axes.labelsize': 12,
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'figure.figsize': (10, 6),
'savefig.dpi': 300
})6.2 顏色搭配技巧
推薦使用以下配色方案:
- 分類數(shù)據(jù):
['#4C72B0', '#55A868', '#C44E52', '#CCB974'] - 連續(xù)數(shù)據(jù):
['#313695', '#4575b4', '#74add1', '#abd9e9']
可以使用在線工具Coolors生成配色方案。
七、實際應用案例:電商價格監(jiān)控
假設我們爬取了某電商平臺的手機價格數(shù)據(jù):
# 模擬數(shù)據(jù)
phone_data = {
'brand': ['Apple']*5 + ['Samsung']*5 + ['Huawei']*5,
'model': ['iPhone13']*3 + ['iPhone13 Pro']*2 +
['S22']*3 + ['S22 Ultra']*2 +
['P50']*3 + ['Mate50']*2,
'price': [5999, 6799, 5499, 8999, 9799,
4999, 5499, 6299, 7999, 8999,
4499, 4999, 5299, 6799, 7499],
'rating': [4.8, 4.9, 4.7, 4.9, 4.8,
4.6, 4.7, 4.8, 4.9, 4.8,
4.5, 4.6, 4.7, 4.8, 4.7]
}
df = pd.DataFrame(phone_data)7.1 品牌價格對比箱線圖
plt.figure(figsize=(10, 6))
df.boxplot(column='price', by='brand',
patch_artist=True,
boxprops=dict(facecolor='#4C72B0', color='black'),
medianprops=dict(color='white'))
plt.title('各品牌手機價格分布')
plt.suptitle('') # 去除自動生成的標題
plt.xlabel('品牌')
plt.ylabel('價格(元)')
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()7.2 價格與評分散點圖
plt.figure(figsize=(10, 6))
brands = df['brand'].unique()
colors = {'Apple': '#4C72B0', 'Samsung': '#55A868', 'Huawei': '#C44E52'}
for brand in brands:
subset = df[df['brand'] == brand]
plt.scatter(subset['price'], subset['rating'],
s=100, label=brand, color=colors[brand])
plt.title('手機價格與評分關系')
plt.xlabel('價格(元)')
plt.ylabel('評分')
plt.legend()
plt.grid(True)
plt.show()常見問題Q&A
Q1:被網(wǎng)站封IP怎么辦?
A:立即啟用備用代理池,建議使用住宅代理(如站大爺IP代理),配合每請求更換IP策略??梢栽趓equests請求中添加代理參數(shù):
proxies = {
'http': 'http://your-proxy-ip:port',
'https': 'https://your-proxy-ip:port'
}
response = requests.get(url, headers=headers, proxies=proxies)Q2:中文顯示亂碼如何解決?
A:在代碼開頭添加以下配置:
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系統(tǒng) plt.rcParams['axes.unicode_minus'] = False # 解決負號顯示問題 # 或使用Mac/Linux的字體 # plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
Q3:如何保存透明背景的圖表?
A:使用savefig的transparent參數(shù):
plt.savefig('transparent.png', dpi=300, transparent=True)Q4:圖表中的文字重疊怎么辦?
A:可以嘗試以下方法:
- 旋轉x軸標簽:
plt.xticks(rotation=45) - 調整圖表大?。?code>plt.figure(figsize=(12, 6))
- 使用
tight_layout()自動調整 - 手動調整子圖間距:
plt.subplots_adjust(wspace=0.4, hspace=0.6)
Q5:如何實現(xiàn)動態(tài)圖表?
A:可以使用matplotlib.animation模塊創(chuàng)建動態(tài)圖表,或導出為HTML使用Plotly等庫實現(xiàn)交互式可視化。
結語:數(shù)據(jù)可視化的價值
通過本文的實戰(zhàn),你應該已經掌握了從數(shù)據(jù)爬取到專業(yè)可視化的完整流程。記住,好的可視化不是追求花哨的效果,而是準確、清晰地傳達數(shù)據(jù)背后的信息。下次當你面對一堆CSV數(shù)據(jù)時,不妨先用Matplotlib畫幾張圖,往往能發(fā)現(xiàn)意想不到的洞察。
數(shù)據(jù)可視化就像給數(shù)據(jù)講故事,而Matplotlib就是你手中的畫筆。現(xiàn)在,是時候用你爬取的數(shù)據(jù),創(chuàng)作屬于自己的數(shù)據(jù)故事了!
以上就是Python使用Matplotlib將爬取的CSV數(shù)據(jù)變成直觀圖表的詳細內容,更多關于Python Matplotlib將CSV數(shù)據(jù)變成圖表的資料請關注腳本之家其它相關文章!
相關文章
Python實現(xiàn)將一個帶鍵值特征的JSON數(shù)組轉換為JSON對象
這篇文章主要為大家詳細介紹了Python實現(xiàn)將一個帶鍵值特征的JSON數(shù)組轉換為JSON對象,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-11-11
numpy中的隨機打亂數(shù)據(jù)方法np.random.shuffle解讀
這篇文章主要介紹了numpy中的隨機打亂數(shù)據(jù)方法np.random.shuffle解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
python3 selenium自動化測試 強大的CSS定位方法
今天小編就為大家分享一篇python3 selenium自動化測試 強大的CSS定位方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
使用TensorFlow-Slim進行圖像分類的實現(xiàn)
這篇文章主要介紹了使用TensorFlow-Slim進行圖像分類的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
Python圖像銳化與邊緣檢測之Sobel與Laplacian算子詳解
圖像銳化和邊緣檢測主要包括一階微分銳化和二階微分銳化,本文主要講解常見的圖像銳化和邊緣檢測方法,即Sobel算子和Laplacian算子,感興趣的可以了解一下2022-12-12
pytorch實現(xiàn)seq2seq時對loss進行mask的方式
今天小編就為大家分享一篇pytorch實現(xiàn)seq2seq時對loss進行mask的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

