可视化高峰数据

matplotlib 数据可视化
图像中文字体

数据集存在的问题


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by xuehz on 2017/3/16

import pandas as pd
import matplotlib.pyplot as plt

from matplotlib import style

style.use('ggplot')

## 解决matplotlib显示中文问题

plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题


dataset_path = './dataset/Mountains.csv'


def preview_data(data):
    """
    预览数据
    :param data:
    :return:
    """
    # 数据预览
    print(data.head())

    # 数据信息
    print(data.info())

def proc_success(val):
    """
    处理 'Ascents bef. 2004' 列中的数据
    :param val:
    :return:
    """
    if '>' in str(val):
        return 200
    elif 'Many' in str(val):
        return 160
    else:
        return val

def run_main():
    data = pd.read_csv(dataset_path)

    #预览数据信息
    #preview_data(data)

    # 数据重构
    # 重命名列名
    data.rename(columns={'Height (m)': 'Height', 'Ascents bef. 2004': 'Success',
                         'Failed attempts bef. 2004': 'Failed'}, inplace=True)

    # 数据清洗
    data['Failed'] = data['Failed'].fillna(0).astype(int)
    data['Success'] = data['Success'].apply(proc_success)
    data['Success'] = data['Success'].fillna(0).astype(int)
    data = data[data['First ascent'] != 'unclimbed']
    data['First ascent'] = data['First ascent'].astype(int) #年份存在空值

    # 可视化数据
    # 1. 登顶次数 vs 年份
    plt.hist(data['First ascent'].astype(int), bins=20) #直方图
    plt.xlabel(u'年份')
    plt.ylabel(u'高峰数量')
    plt.title(u'登顶次数')
    plt.savefig('./first_ascent_vs_year.png')
    plt.show()

    # 2. 高峰vs海拔
    data['Height'].plot.hist(color='steelblue', bins=20) # plot pandas
    plt.bar(data['Height'],
            (data['Height'] - data['Height'].min()) / (data['Height'].max() - data['Height'].min()) * 23,   # 按比例缩放
            color='red',
            width=30, alpha=0.2)
    plt.ylabel(u'高峰数量')
    plt.xlabel(u'海拔')

    plt.text(8750, 20, u"海拔", color='red') #图形上添加文字

    plt.title(u'高峰vs海拔')
    plt.savefig('./mountain_vs_height.png')

    plt.show()

    # 3. 首次登顶
    data['Attempts'] = data['Failed'] + data['Success']  # 攀登尝试次数
    fig = plt.figure(figsize=(13, 7))
    fig.add_subplot(211)
    plt.scatter(data['First ascent'], data['Height'], c=data['Attempts'], alpha=0.8, s=50)
    plt.ylabel(u'海拔')
    plt.xlabel(u'登顶')

    fig.add_subplot(212)
    plt.scatter(data['First ascent'], data['Rank'].max() - data['Rank'], c=data['Attempts'], alpha=0.8, s=50)
    plt.ylabel(u'排名')
    plt.xlabel(u'登顶')
    plt.savefig('./mountain_vs_attempts.png')
    plt.show()


if __name__ == '__main__':
    run_main()
    



posted @ 2017-03-16 23:45  keven0526  阅读(233)  评论(0编辑  收藏  举报