# -*- 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繪圖更簡單。spa