subway_analysis.py 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
from wordcloud import WordCloud, ImageColorGenerator
from pyecharts import Line, Bar, Geo
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
import jieba
##使用版本:pyecharts 0.3版本
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文标签
plt.rcParams['axes.unicode_minus']=False # 设置正常显示符号

# 设置列名与数据对齐
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
# 显示10行
pd.set_option('display.max_rows', 10)
# 读取数据
df = pd.read_csv('../data/data-analyselesson/subway.csv', header=None, names=['city', 'line', 'station'], encoding='gbk')
print(type(df))
# 统计各个城市地铁线路情况
df_line = df.groupby(['city', 'line']).count().reset_index()
print(df_line)

def create_map(df):
    # 绘制地图
    value = [i for i in df['line']]
    attr = [i for i in df['city']]
    geo = Geo("已开通地铁城市分布情况", title_pos='center', title_top='0', width=800, height=400, title_color="#fff", background_color="#404a59", )
    geo.add("", attr, value, is_visualmap=True, visual_range=[0, 25], visual_text_color="#fff", symbol_size=15)
    geo.show_config()
    geo.render("已开通地铁城市分布情况.html")
def create_line(df):
    """
    生成城市地铁线路数量分布情况
    """
    title_len = df['line']
    bins = [0, 5, 10, 15, 20, 25]
    level = ['线路数0-5', '线路数5-10', '线路数10-15', '线路数15-20', '线路数20以上']
    len_stage = pd.cut(title_len, bins=bins, labels=level).value_counts().sort_index()
    # 生成柱状图
    attr = len_stage.index
    v1 = len_stage.values
    bar = Bar("各城市地铁线路数量分布", title_pos='center', title_top='18', width=800, height=400)
    bar.add("城市个数", attr, v1, is_stack=True, is_label_show=True)
    bar.render("各城市地铁线路数量分布.html")


# 各个城市地铁线路数
df_city = df_line.groupby(['city']).count().reset_index().sort_values(by='line', ascending=False)
print(df_city)
create_map(df_city)
create_line(df_city)

# 哪个城市哪条线路地铁站最多
print(df_line.sort_values(by='station', ascending=False))#这里只是把各个城市的所有线路的站点数排列了一下

# 去除重复换乘站的地铁数据
df_station = df.groupby(['city', 'station']).count().reset_index()
print(df_station)

# 统计每个城市包含地铁站数(已去除重复换乘站)
print(df_station.groupby(['city']).count().reset_index().sort_values(by='station', ascending=False))


def create_wordcloud(df):
    """
    #生成地铁名词云
    """
    # 分词
    text = ''
    for line in df['station']:
        text += ' '.join(jieba.cut(line, cut_all=False))
        text += ' '
    backgroud_Image = plt.imread('rocket.jpg')
    wc = WordCloud(
        background_color='white',
        mask=backgroud_Image,
        font_path='C:\Windows\Fonts\HGY3_CNKI.TTF',
        max_words=1000,
        max_font_size=150,
        min_font_size=15,
        prefer_horizontal=1,
        random_state=50,
    )
    wc.generate_from_text(text)
    img_colors = ImageColorGenerator(backgroud_Image)
    wc.recolor(color_func=img_colors)
    # 看看词频高的有哪些
    process_word = WordCloud.process_text(wc, text)
    sort = sorted(process_word.items(), key=lambda e: e[1], reverse=True)
    print(sort[:50])
    plt.imshow(wc)
    plt.axis('off')
    wc.to_file("地铁名词云.jpg")
    print('生成词云成功!')


# create_wordcloud(df_station)

words = []
for line in df['station']:
    for i in line:
        # 将字符串输出一个个中文
        words.append(i)


def all_np(arr):
    """
    #统计单字频率
    """
    arr = np.array(arr)
    key = np.unique(arr)
    result = {}
    for k in key:
        mask = (arr == k)
        arr_new = arr[mask]
        v = arr_new.size
        result[k] = v
    return result


def create_word(word_message):
    """
    #生成柱状图
    """
    attr = [j[0] for j in word_message]
    v1 = [j[1] for j in word_message]
    bar = Bar("中国地铁站最爱用的字", title_pos='center', title_top='18', width=800, height=400)
    bar.add("", attr, v1, is_stack=True, is_label_show=True)
    bar.render("中国地铁站最爱用的字.html")


word = all_np(words)
word_message = sorted(word.items(), key=lambda x: x[1], reverse=True)[:10]
create_word(word_message)

# 选取上海的地铁站
df1 = df_station[df_station['city'] == '上海']
print(df1)
# 选取上海地铁站名字包含路的数据
df2 = df1[df1['station'].str.contains('路')]
print(df2)

# 选取武汉的地铁站
df1 = df_station[df_station['city'] == '武汉']
print(df1)
# 选取武汉地铁站名字包含家的数据
df2 = df1[df1['station'].str.contains('家')]
print(df2)

# 选取重庆的地铁站
df1 = df_station[df_station['city'] == '重庆']
print(df1)
# 选取重庆地铁站名字包含家的数据
df2 = df1[df1['station'].str.contains('家')]
print(df2)


def create_door(door):
    """
    生成柱状图
    """
    attr = [j for j in door['city'][:8]]
    v1 = [j for j in door['line'][:8]]
    bar = Bar("地铁站最爱用“门”命名的城市", title_pos='center', title_top='18', width=800, height=400,)
    bar.add("", attr, v1, is_stack=True, is_label_show=True, yaxis_max=40)
    bar.render("地铁站最爱用门命名的城市.html")


# 选取地铁站名字包含门的数据
df1 = df_station[df_station['station'].str.contains('门')]
# 对数据进行分组计数
df2 = df1.groupby(['city']).count().reset_index().sort_values(by='line', ascending=False)
print(df2)
create_door(df2)


# 选取北京的地铁站
df1 = df_station[df_station['city'] == '北京']
print(df1)
# 选取北京地铁站名字包含门的数据
df2 = df1[df1['station'].str.contains('门')]
print(df2)

# 选取南京的地铁站
df1 = df_station[df_station['city'] == '南京']
# 选取南京地铁站名字包含门的数据
df2 = df1[df1['station'].str.contains('门')]
print(df2)

# 选取西安的地铁站
df1 = df_station[df_station['city'] == '西安']
# 选取西安地铁站名字包含门的数据
df2 = df1[df1['station'].str.contains('门')]
print(df2)



#问题:选取数量前5个名字中带有大学的地铁站的城市,并绘制柱状图
df1=df[df['station'].str.contains('大学')]
city_counts=df1['city'].value_counts()
plt.figure(figsize=(10,5))
labelline=list(city_counts[:5].index)#
print(labelline)#['上海', '沈阳', '北京', '天津', '重庆']
plt.xlabel('城市')
plt.ylabel('站点数量')
plt.title('名字中带有大学的地铁站的城市数量分布')
plt.bar([i for i in labelline],city_counts[:5])

#问题:绘制北京、武汉、天津、上海等各线路站点数量的折线图趋势分布
#北京:
df1=df[df['city']=='北京']
Bei_station=df1['line'].value_counts()
print(Bei_station)
plt.figure(figsize=(12,6))
labelline=list(Bei_station[:8].index)
plt.xlabel=('线路')
plt.ylabel=('各站点数量')
plt.title("北京各线路站点数量的分布趋势")
plt.plot([i for i in labelline],Bei_station[:8])
# plt.show()

#武汉
df1=df[df['city']=='武汉']
Wu_station=df1['line'].value_counts()
print(Wu_station)
plt.figure(figsize=(12,6))
labelline=list(Wu_station[:8].index)
plt.xlabel=('线路')
plt.ylabel=('各站点数量')
plt.title("武汉各线路站点数量的分布趋势")
plt.plot([i for i in labelline],Wu_station[:8])
# plt.show()

#天津
df1=df[df['city']=='天津']
Tian_station=df1['line'].value_counts()
print(Tian_station)
plt.figure(figsize=(12,6))
labelline=list(Tian_station[:8].index)
plt.xlabel=('线路')
plt.ylabel=('各站点数量')
plt.title("天津各线路站点数量的分布趋势")
plt.plot([i for i in labelline],Tian_station[:8])
# plt.show()

#上海
df1=df[df['city']=='上海']
Shang_station=df1['line'].value_counts()
print(Shang_station)
plt.figure(figsize=(12,6))
labelline=list(Shang_station[:8].index)
plt.xlabel=('线路')
plt.ylabel=('各站点数量')
plt.title("上海各线路站点数量的分布趋势")
plt.plot([i for i in labelline],Shang_station[:8])
# plt.show()

#问题:各个城市的线路数量的饼状图分布
line_count=df['city'].value_counts()
plt.figure(figsize=(10,7))
plt.pie(line_count,labels=line_count.index,autopct='%1.1f%%')
plt.title('各个城市的线路数量的饼状图分布')
# plt.show()

#问题:各个城市的站点数量的饼状图分布
#饼状图展示
df_station = df.groupby(['city', 'station']).count().reset_index()  #此处去除每个城市的重复换乘站点数,得到实际数量的站点数量
df1=df_station.groupby(['city']).count().reset_index().sort_values(by='station', ascending=False)
df1['city']=df1['city']+'(站点数'+df1['station'].map(str)+')'
line_count=df1['station']
plt.figure(figsize=(10,7))
plt.pie(line_count,labels=df1['city'],autopct='%1.1f%%')
plt.title('各个城市的站点数量的饼状图分布')
plt.show()

# #散点图展示
# df_station = df.groupby(['city', 'station']).count().reset_index()  #此处去除每个城市的重复换乘站点数,得到实际数量的站点数量
# df1=df_station.groupby(['city']).count().reset_index().sort_values(by='station', ascending=False)
# line_count=df1['station']
# plt.figure(figsize=(10,7))
# plt.xlabel=('城市')
# plt.ylabel=('站点数量')
# plt.scatter(x=df1['city'],y=line_count,marker='*')
# plt.title('各个城市的站点数量的散点图分布')
# plt.show()

#各城市的每条线路的站点数量的变化 折线图
df1=df_line.sort_values(by='station', ascending=False)#by中指定按照什么列排序,ascending中默认升序排列,值为True
station_count=df1['line']+df1['city']
plt.figure(figsize=(15,8))
labelline=list(station_count[:12])
plt.xlabel=('线路')
plt.ylabel=('各站点数量')
plt.title("各城市各线路的站点数量前10的变化")
plt.plot([i for i in labelline],df1['station'][:12])
plt.show()

#每个城市的哪条线路的地铁站点数量最多  柱形图
df_1=df_line.sort_values(by='station', ascending=False)
df_2=df_1.groupby('city')['station'].max().reset_index(drop=False)#保留索引
line_station_c=df_2.sort_values(by='station',ascending=False)
# line_station_c.to_csv("../1.csv",header=False,index=False)
plt.figure(figsize=(15,5))
labelline=list(line_station_c['city'])
# line_text=pd.merge(left=line_station_c,right=df_1,on=['city','station'],how='inner')
# line_text.to_csv("../2.csv",header=False,index=False)
labelline=labelline#+line_text['line'].map(str)
plt.xlabel=('城市')
plt.ylabel=('站点数量')
plt.bar([i for i in labelline],line_station_c['station'])
plt.title('每个城市哪条线路的站点数最多')
plt.show()

#问题:统计各个城市的大学数量,然后利用回归图进行拟合(分析各个城市的大学数量与站点数量的关系
df_uni= pd.read_csv('../university.csv', header=None, names=['city', 'uni_count'], encoding='gbk')
df_uni=pd.merge(left=line_station_c,right=df_uni,on='city',how='inner') #将两个表格中的数据基于city列进行内连接。
x=df_uni['uni_count']
y=df_uni['station']
sns.regplot(x=x,y=y,color='b')
plt.title('分析各个城市的大学数量与站点数量的关系')
plt.show()

#散点图
fig=plt.figure(figsize=(10,7))
plt.xlabel=('站点数量')
plt.ylabel=('大学数量')
plt.title('各个城市的大学数量与站点数量的关系')
plt.scatter(x=x,y=y,cmap='b',marker='*',alpha=0.8)
plt.grid()
plt.show()

#seaborn的双变量图:可以查看多变量之间的分布关系,也可以显示它本身的单变量情况
df_s=df_uni
sns.jointplot(x='uni_count',y='station',data=df_s)
plt.show()
plt.close()

#问题:选取郑州、武汉、广州、长沙同名的线路1-线路6,绘制折线图分析这些城市的目标线路的站点数量分布
df_1=df_line.sort_values(by='station', ascending=False)
zz_=df_1[df_1['city']=='郑州'].sort_values(by='line',ascending=False).reset_index()#ascending参数值为False时,则数据按指定列降序排序。
zz_=zz_.loc[zz_['line'].isin(['1号线','2号线','3号线','4号线','5号线','6号线'])]

wh_=df_1[df_1['city']=='武汉'].sort_values(by='line',ascending=False).reset_index()
wh_=wh_.loc[wh_['line'].isin(['1号线','2号线','3号线','4号线','5号线','6号线'])]

gz_=df_1[df_1['city']=='广州'].sort_values(by='line',ascending=False).reset_index()
gz_=gz_.loc[gz_['line'].isin(['1号线','2号线','3号线','4号线','5号线','6号线'])]

cs_=df_1[df_1['city']=='长沙'].sort_values(by='line',ascending=False).reset_index()
cs_=cs_.loc[cs_['line'].isin(['1号线','2号线','3号线','4号线','5号线','6号线'])]

print(zz_)
print(wh_)
print(gz_)
print(cs_)
plt.figure(figsize=(10,7))
L1=plt.plot(zz_['line'],zz_['station'],color='b',label='郑州线路1-6的站点数量变化')
L2=plt.plot(wh_['line'],wh_['station'],color='g',label='武汉线路1-6的站点数量变化')
L3=plt.plot(gz_['line'],gz_['station'],color='r',label='广州线路1-6的站点数量变化')
L4=plt.plot(cs_['line'],cs_['station'],color='k',label='长沙线路1-6的站点数量变化')
plt.legend()
plt.title('郑州、武汉、广州、长沙同名的线路1-线路6的站点数量分布')
plt.xlabel=('线路1-线路6')
plt.ylabel=('站点数量')
plt.show()

#全国各城市的总的换乘站点数量(2换乘、3换乘、4换乘等)分布统计
df_1=df.groupby(['city','station']).count().reset_index()
print(df_1)
df_1=df_1[df_1['line']>1]#筛选出来全国的换乘站点数
tran_sit=df_1.groupby('line').count().reset_index()  #保留原索引,但是值是count()函数计数之后的值
plt.figure(figsize=(10,5))
plt.xlabel=('站点可换乘等级')
plt.ylabel=('站点数量')
plt.bar(tran_sit['line'],tran_sit['station'],color='g')
plt.title('全国各城市总的换乘站点数量(2换乘、3换乘、4换乘等)分布统计')
plt.show()
print(tran_sit[tran_sit['line']==5]['station'])