首页 > 新闻

用pandas做全球变暖可视化

发布时间:2023-06-14 02:20:18

从本地文件(气候数据可以从data world下载)读入,然后可以按城市、省、国家以及时间长短(年或月)来分别可视化,从多方位多角度感受全球气候变暖的事实,还可以了解全球平均气温最高和最低的国家和城市。感兴趣的可以copy后自行下载数据到你本地运算。

import matplotlib.pyplot as pltimport matplotlib.dates as mdatesimport numpy as npdef draw_scatter_2d(width,height,xtick,ytick,xtickmax,ytickmax,xlabel,ylabel,title,xtick_rotation_angle,ytick_rotation_angle,df,path_for_saving):
    # Create a scatter plot
    fig, ax = plt.subplots(figsize=(width,height)) 
    ax.scatter(df[xtick],df[ytick])   
    # Set the x-tick locator 
    ax.xaxis.set_major_locator(plt.MaxNLocator(xtickmax))    
    # Set the y-tick locator
    ax.yaxis.set_major_locator(plt.MaxNLocator(ytickmax))    
    if df[xtick].dtype == np.dtype('datetime64[ns]'):        # Convert the date_str column to a numerical format
        df.loc[:,xtick] = mdates.date2num(pd.to_datetime(df[xtick]))        
        # Set the x-axis scale to a standard date format
        ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))        
    if df[ytick].dtype ==  np.dtype('datetime64[ns]'):        # Convert the date_str column to a numerical format
        df.loc[:,ytick] = mdates.date2num(pd.to_datetime(df[ytick]))        
        # Set the y-axis scale to a standard date format
        ax.yaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))    
    # Set the labels for each axis
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    ax.tick_params(axis='x', rotation=xtick_rotation_angle, labelsize=9)  
    ax.tick_params(axis='y', rotation=ytick_rotation_angle, labelsize=9) 
    
    plt.rcParams['font.sans-serif'] = ['Times New Roman']    # Show the plot
    #plt.show()
    plt.savefig(path_for_saving)
    plt.close()def draw_based_on_date(width,height,name1,name2,xtick,ytick,xtickmax,ytickmax,xscaleunit,yscaleunit,xlabel,ylabel,title,xtick_rotation_angle,ytick_rotation_angle,df,path_for_saving):
            
    # Filter the data to only include records from the specified name
    filtered_data = df.loc[df[name1] == name2]

    action = True
    
    if xscaleunit == 'month':        # Group the data by year and calculate the mean temperature for each year
        #filtered_data = filtered_data.groupby(filtered_data[xtick].dt.month)[ytick].mean()
        action = False
    elif yscaleunit == 'month':        # Group the data by year and calculate the mean temperature for each year
        #filtered_data = filtered_data.groupby(filtered_data[ytick].dt.month)[xtick].mean()
        action = False
    elif xscaleunit == 'year':        # Group the data by year and calculate the mean temperature for each year
        filtered_data = filtered_data.groupby(filtered_data[xtick].dt.year)[ytick].mean()    elif yscaleunit == 'year':        # Group the data by year and calculate the mean temperature for each year
        filtered_data = filtered_data.groupby(filtered_data[ytick].dt.year)[xtick].mean()    else:
        action = False

    if action:        # Convert the series back to a DataFrame, reset the index and move the 'dt' column to a regular column
        filtered_data = filtered_data.to_frame().reset_index()

    draw_scatter_2d(width,height,xtick,ytick,xtickmax,ytickmax,xlabel,ylabel,title,xtick_rotation_angle,ytick_rotation_angle,filtered_data,path_for_saving)import osimport pandas as pd

file_path = r'C:\Users\Public\Documents\Data\data-society-global-climate-change-data\GlobalLandTemperatures-2023\GlobalLandTemperaturesByMajorCity.csv'# Read the data into a pandas DataFramedf = pd.read_csv(file_path, delimiter=',')

df['dt'] = pd.to_datetime(df['dt'], errors='coerce').dropna()
df['AverageTemperature'].fillna(df['AverageTemperature'].mean(), inplace=True)

df2 = df.groupby(['City'], as_index = False)['AverageTemperature'].mean().dropna()
path = r'C:\Users\Public\Documents\Data'name1 = 'City'#tick_name = 'Tick_Month'tick_name = 'Tick_Year'# Create a directory to save the figuresos.makedirs(os.path.join(path, name1,tick_name), exist_ok=True)for i in range(df2.shape[0]):
    name2 = df2.iloc[i,0].strip()    # Save the figure as a PNG file in the directory
    path_for_saving = os.path.join(path, name1, tick_name, f'{name2}.png')    #draw_based_on_date(24,12,name1,name2,df.columns[0],df.columns[1],60,30,'month','','Month','Monthly Average Temperature','Monthly Average Temperature Change' '(' name2 ')',60,0,df,path_for_saving)
    draw_based_on_date(12,6,name1,name2,df.columns[0],df.columns[1],30,30,'year','','Year','Yearly Average Temperature','Yearly Average Temperature Change' '(' name2 ')',60,0,df,path_for_saving)

1843年以来全球气温明显升高

全球国家平均气温排行

全球城市平均气温排行

成都历年气温变化

四川历年气温变化

中国历年气温变化


(责编: 网络)

免责声明:本文为转载,非本网原创内容,不代表本网观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。

如有疑问请发送邮件至:goldenhorseconnect@gmail.com