从本地文件(气候数据可以从data world下载)读入,然后可以按城市、省、国家以及时间长短(年或月)来分别可视化,从多方位多角度感受全球气候变暖的事实,还可以了解全球平均气温最高和最低的国家和城市。感兴趣的可以copy后自行下载数据到你本地运算。
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
def 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 os
import pandas as pd
file_path = r'C:UsersPublicDocumentsDatadata-society-global-climate-change-dataGlobalLandTemperatures-2023GlobalLandTemperaturesByMajorCity.csv'
# Read the data into a pandas DataFrame
df = 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:UsersPublicDocumentsData'
name1 = 'City'
#tick_name = 'Tick_Month'
tick_name = 'Tick_Year'
# Create a directory to save the figures
os.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)
页面更新:2024-03-05
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号