简体   繁体   English

Pandas 和 geopandas 仅一年的每月 Plot 数据

[英]Plot data of every month for only one year with Pandas and geopandas

I have these data from the years 1991-2020 and for five countries.我有 1991-2020 年和五个国家的这些数据。

Tempcountries临时国家

    Date  Temperature    Units  Year Month Statistics Country CODE
                                                             
Jan 1991        -26.2  Celsius  1991   Jan    Average  Canada  CAN
Feb 1991        -21.0  Celsius  1991   Feb    Average  Canada  CAN
Mar 1991        -18.2  Celsius  1991   Mar    Average  Canada  CAN
Apr 1991         -8.6  Celsius  1991   Apr    Average  Canada  CAN
May 1991          0.8  Celsius  1991   May    Average  Canada  CAN

Merge合并

 pop_est      continent    name CODE  ...  Country ISO2   latitude   longitude
0  35623680  North America  Canada  CAN  ...   Canada   CA  56.130366 -106.346771
1  35623680  North America  Canada  CAN  ...   Canada   CA  56.130366 -106.346771
2  35623680  North America  Canada  CAN  ...   Canada   CA  56.130366 -106.346771
3  35623680  North America  Canada  CAN  ...   Canada   CA  56.130366 -106.346771
4  35623680  North America  Canada  CAN  ...   Canada   CA  56.130366 -106.346771

I want to plot the temperature values on the world map for every month just for the year 1991. So at the end, I will get 12 plots, showing the temperature of each country.我想 plot 世界上的温度值 map 仅适用于 1991 年。所以最后,我将得到 12 个图,显示每个国家的温度。

How do I select only the year 1991 and how do I put a title indicating the Year and month of each plot?我如何仅在 1991 年 select 以及如何放置一个标题来指示每个 plot 的年份和月份?

I did this:我这样做了:

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import geopandas

location=pd.read_excel('countries.xlsx') #Longitude latitude data
tempcountries = pd.read_excel('Temperature Countries.xlsx')
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world.columns=['pop_est', 'continent', 'name', 'CODE', 'gdp_md_est', 'geometry']

for y in tempcountries['Year']:
    for i in tempcountries['Month']:
        fig, ax = plt.subplots(figsize=(8,6))
        world.plot(ax=ax, color='lightgrey')
        merge.plot(x="longitude", y="latitude", kind="scatter",  
                   c="Temperature", colormap="coolwarm", 
                   ax=ax)
        plt.show()

I get multiple plots but I do not see that the colors (indicating the temperature) on the map change.我得到了多个图,但我没有看到 map 上的 colors(指示温度)发生变化。

Assuming your merge dataframe is the result of merging tempcountries and world , you could do the following:假设您的merge dataframe 是合并tempcountriesworld的结果,您可以执行以下操作:

month_map = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# only select year 1991
df = merge.loc[merge["Year"] == '1991']

fig, axs = plt.subplots(3, 4, figsize=(16,9), sharey=True)
for i, ax in enumerate(axs.flat):
    # plot basemap
    world.plot(ax=ax, color='lightgrey')
    # plot values of current month
    df.loc[df["Month"] == month_map[i]].plot(ax=ax, x="longitude", y="latitude", kind="scatter", c="Temperature", colormap="coolwarm")
    ax.set_title(month_map[i])

As I'm not allowed to post images yet, here is the output of the above code.由于我还不允许发布图像,这里是上述代码的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM