寫完利用python代替EXCEL常用操作那篇文章后格仲,一直想寫一篇python作圖的文章,之所以遲遲沒(méi)有完成侮东,一來(lái)是因?yàn)樽鲌D細(xì)節(jié)太多驱敲,不好總結(jié),二來(lái)自己最初學(xué)習(xí)python的主要目的是做爬蟲和文本處理,即使需要做圖览徒,也是用Python把數(shù)據(jù)處理好然后喂給EXCEL或者Tableau(老是覺(jué)得python做的圖很丑),后來(lái)開(kāi)始做機(jī)器學(xué)習(xí)的項(xiàng)目發(fā)現(xiàn)研究過(guò)程中還是用python作圖效率更高些
作圖方面細(xì)節(jié)非常多,不可能一次寫完,正好最近在讀《精益創(chuàng)業(yè)》涕俗,對(duì)書里介紹的MVP法則(最小可行產(chǎn)品)找御,所以決定先寫一個(gè)能看的版本,然后持續(xù)更新吧遇革。。。
注:本文每個(gè)圖都會(huì)用seaborn和matplotlib分別實(shí)現(xiàn)冰更,seaborn用于快速出圖叁怪,matplotlib可以設(shè)置更多參數(shù)實(shí)現(xiàn)個(gè)性化繪圖(matplotlib設(shè)置確實(shí)很麻煩痴荐,還不如直接用Tableau實(shí)現(xiàn)难捌,哈哈。。沃斤。)
一、準(zhǔn)備工作
1、環(huán)境搭建
正式工作開(kāi)始前,需要安裝matplotlib,seaborn,pandas和wordcloud(詞云)包,Anaconda已經(jīng)集成了matplotlib,seaborn和pandas包缝其,但是wordcloud包仍然需要自己下載安裝待锈,安裝教程請(qǐng)參考
python安裝第三方包
2、下載數(shù)據(jù)集
本文數(shù)據(jù)集下載地址
鏈接:https://pan.baidu.com/s/1804beamaiEKdT_WciSQqqg
密碼:qnqx
本例數(shù)據(jù)集是拉鉤網(wǎng)數(shù)據(jù)分析崗位明細(xì)數(shù)據(jù),共有11個(gè)特征,分別為:薪酬下限、薪酬上限、工作地點(diǎn)询件、經(jīng)驗(yàn)要求嘿辟、學(xué)歷要求、工作時(shí)間、公司、所處行業(yè)、公司融資情況、投資機(jī)構(gòu)、崗位要求等
二募狂、開(kāi)始作圖
1勺三、導(dǎo)入相關(guān)包祈远,導(dǎo)入數(shù)據(jù)
import matplotlib
import numpy as np
import pandas as pd
import seaborn as sns
import re
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
from wordcloud import WordCloud
df = pd.read_excel('la_gou.xlsx')
df_clean= df.drop(['Index', 'home_page', 'address', 'url', 'date_time'],axis=1)
df_clean = df_clean.drop_duplicates(['company', 'title', 'description'])
df_clean = df_clean.reset_index(drop=True)
2、散點(diǎn)圖
稍后補(bǔ)
3严就、頻數(shù)圖
1)數(shù)據(jù)預(yù)處理
city_series = df_clean['city'].value_counts()
y_cor = list(city_series.values) #統(tǒng)計(jì)值
x_cor = list(np.arange(len(y_cor)))
2)頻數(shù)圖_seaborn
f, ax = plt.subplots(figsize=(18, 10)) #設(shè)定圖片大小
sns.barplot(y=city_series.values, x=city_series.index,orient='v', alpha=0.8, color='red')
for x,y in zip(x_cor,y_cor): #顯示上方文字
plt.text(x, y+1, '%s' % y, ha='center', va= 'bottom',fontsize=14)
plt.title('各城市職位數(shù)量', size = 18)
plt.show()
3)頻數(shù)圖_matplotlib
city_series.plot(kind='bar', figsize=(18,10), fontsize=15, rot=40)
for x,y in zip(x_cor,y_cor):
plt.text(x, y+1, '%s' % y, ha='center', va= 'bottom',fontsize=14)
plt.title('各城市職位數(shù)量', size = 18)
plt.show()
4.1 頻數(shù)圖_重新排序
1)數(shù)據(jù)預(yù)處理
experience_series=df_city['experience_new'].value_counts()
experience_series=experience_series.reindex(['1年以下','1-3年','3-5年','5-10年','不限'])
2)頻數(shù)圖_重新排序_seaborn
sns.barplot(y=experience_series.values, x=experience_series.index,orient='v',
alpha=0.8, palette="Set1")
plt.ylabel(u'頻數(shù)', size=15)
plt.show()
3)頻數(shù)圖_重新排序_matplotlib
experience_series.plot(kind='bar',figsize=(8,5), fontsize=15, rot=0)
plt.grid(color='#95a5a6', linewidth=1,axis='y',alpha=0.2)
plt.xticks(range(5), experience_series.index, size=15)
plt.ylabel('頻數(shù)', size=15)
plt.show()
4.2、頻數(shù)圖_多變量
1)數(shù)據(jù)預(yù)處理
df_city=df_clean[df_clean['city'].isin(['北京','上海','深圳','廣州','杭州'])]
df_city['fuzhu'] = 1
#替換前對(duì)中文數(shù)據(jù)去空格
df_city['experience_new'] = df_city['experience'].map(lambda s: s.strip())
df_city = df_city.replace({'experience_new':'應(yīng)屆畢業(yè)生'},'1年以下')
df_city = df_city.replace({'experience_new':'10年以上'},'5-10年')
df_xin = df_city.loc[:,['city', 'experience','fuzhu']]#選擇多列
df_xin1 = df_city.loc[:,['city', 'experience']]#選擇多列
2)頻數(shù)圖_多變量_seaborn
sns.countplot(x="city",hue="experience",data=df_xin1)
plt.legend(loc=0,ncol=3)#loc:0為最優(yōu)器罐,1右上角洪囤,2 左上角 ncol為標(biāo)簽有幾列
plt.ylabel('頻數(shù)', size=15)
plt.show()
2)頻數(shù)圖_多變量_matplotlib
df_group = df_city.groupby(['city', 'experience_new'])#聚合
df_grouped = df_group['fuzhu'].agg('sum')
df_grouped.unstack().plot(kind='bar')#stacked=True 為堆積圖
plt.legend(loc=0,ncol=3)#loc:0為最優(yōu),1右上角差油,2 左上角 ncol為標(biāo)簽有幾列
plt.ylabel('頻數(shù)', size=15)
plt.show()
4估脆、直方圖
1)直方圖_seaborn
sns.distplot(df_clean['salary_median'], color='red',kde=False) #kde=True 顯示概率密度圖
plt.xlabel('薪資(千/月)', size=15)
plt.ylabel('頻數(shù)', size=15)
plt.title('薪資分布', size=18)
plt.show()
2)直方圖_matplotlib
df_clean['salary_median'].hist(figsize=(10,6), bins=30, edgecolor='k', grid=False)
plt.xlabel('薪資(千/月)', size=15)
plt.ylabel('頻數(shù)', size=15)
plt.title('薪資分布', size=18)
plt.xticks(range(0,90,5), size=15) #橫坐標(biāo)范圍(0-90,刻度值為5)
plt.yticks(size=15)
plt.grid(axis='y', alpha=0.2) #顯示網(wǎng)格
plt.show()
6衰倦、折線圖
稍后補(bǔ)
7袒炉、箱圖
1)數(shù)據(jù)預(yù)處理
salary_groupby_city = df_clean.groupby('city')['salary_median']
large_city = city_series[0:6].index
df_city_large = df_clean.loc[df_clean['city'].isin(large_city)]
salary_of_city = []
for city in large_city: #得到各城市對(duì)應(yīng)的薪水的數(shù)組
salary_value = salary_groupby_city.get_group(city).values
salary_of_city.append(salary_value)
2)箱圖_seaborn
plt.style.use('seaborn-darkgrid')
matplotlib.rcParams['font.sans-serif'] = ['SimHei']# 對(duì)于有些seaborn的style,必須同時(shí)運(yùn)行此命令樊零,否則還是不顯示中文
sns.boxplot(x = df_city_large['city'],y = df_city_large['salary_median'],palette="Set3")
plt.title('互聯(lián)網(wǎng)熱點(diǎn)城市薪資分布', size=18)
plt.show()
3)箱圖_matplotlib
plt.style.use('seaborn-darkgrid')
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
# 對(duì)于有些seaborn的style我磁,必須同時(shí)運(yùn)行此命令,否則還是不顯示中文
plt.figure(figsize=(10,5))
plt.boxplot(salary_of_city, boxprops = {'color':'blue'},
flierprops = {'markerfacecolor':'red','color':'black','markersize':4})
plt.title('互聯(lián)網(wǎng)熱點(diǎn)城市薪資分布', size=18)
plt.ylabel('薪資(千/月)',size=15)
plt.xticks(np.arange(6)+1,large_city, size=15) #橫坐標(biāo)刻度值設(shè)置
plt.yticks(size=15)
plt.grid(color='#95a5a6', linewidth=1,axis='x',alpha=0.2)
plt.show()
6驻襟、核密度估計(jì)圖
稍后補(bǔ)
7夺艰、詞云
def get_skill(text):
skill_list = re.findall('([a-zA-Z][0-9a-zA-Z]+|C\#|\.Net|R\d?|A\/B|算法)', text)
for skill in skill_list:
if skill.upper() == 'EXCEL' or skill.upper() == 'PPT':
skill_list[skill_list.index(skill)] = 'office'
return ','.join(skill_list).upper()
df_clean['skill'] = df_clean['description'].apply(get_skill)
print(df_clean['skill'][:10])
#生成技能字典
import nltk
skill_list = []
#df_clean表skill列每一行數(shù)據(jù)插入skill_list列表中
for i in df_clean.index:
if len(df_clean.loc[i, 'skill']) > 0:
skill_list.extend(df_clean.loc[i, 'skill'].split(','))
skill_freq = dict(nltk.FreqDist(skill_list))
print(skill_freq)
# 刪除主要的提取錯(cuò)誤的鍵值
del skill_freq['AND']
del skill_freq['TO']
del skill_freq['IN']
del skill_freq['DATA']
del skill_freq['THE']
del skill_freq['OF']
del skill_freq['KPI']
del skill_freq['APP']
del skill_freq['WITH']
del skill_freq['SERVER']
print(skill_freq)
wc = WordCloud(width=800, height=400, background_color='white').generate_from_frequencies(skill_freq)
plt.figure(figsize=(8,4))
plt.imshow(wc)
plt.axis('off')
plt.show()