Python之Pandas繪圖,設置顯示中文問題


 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM