# -*- coding: utf-8 -*-
# author:baoshan import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def main():
aqi_data = pd.read_csv('china_city_aqi_teacher.csv')
print('基本信息:')
print(aqi_data.info())
print('数据预览')
print(aqi_data.head()) # 数据清洗
# 只保留AQI>0的数据
filter_contation = aqi_data['AQI'] > 0
clean_data = aqi_data[filter_contation] # 基本统计
print('AQI最大值', clean_data['AQI'].max())
print('AQI最小值', clean_data['AQI'].min())
print('AQI均值', clean_data['AQI'].mean()) # top50
top50_cities = clean_data.sort_values(by=['AQI'], ascending=True).head(10)
print('空气质量最好的十个城市')
top50_cities.plot(kind='line', x='City', y='AQI', title='空气质量最好的50个城市', figsize=(20,10))
plt.savefig('top50_aqi_line.png')
top50_cities.plot(kind='bar', x='City', y='AQI', title='空气质量最好的50个城市', figsize=(20, 10))
plt.savefig('top50_aqi_bar.png')
plt.show() if __name__ == '__main__':
main()

pandas内置matplotlib绘图更简单。

05-11 17:13