【问题标题】:Matplotlib Heat-Map Y AxisMatplotlib 热图 Y 轴
【发布时间】:2021-01-14 16:29:49
【问题描述】:

提前感谢您! (下图)

我试图让我的热图的 Y 轴反映与其提取的数据相关的年份。发生的情况是 Y 轴仅计算应该显示为 1990、1995、2000 等的年数(0、1、2、...30)。

如何更新我的代码(如下所示),以便 Y 轴显示实际年份而不是年份计数?

# links to Minot data if you want to pull from the web
##url2 = 'https://raw.githubusercontent.com/the- 
datadudes/deepSoilTemperature/master/allStationsDailyAirTemp1.csv'
raw_data = pd.read_csv('https://raw.githubusercontent.com/the- 
datadudes/deepSoilTemperature/master/allStationsDailyAirTemp1.csv', index_col=1, parse_dates=True)
df_all_stations = raw_data.copy()

selected_station = 'Minot'

# load the data into a DataFrame, not a Series
# parse the dates, and set them as the index
df1 = df_all_stations[df_all_stations['Station'] == selected_station]



# groupby year and aggregate Temp into a list
dfg1 = df1.groupby(df1.index.year).agg({'Temp': list})

# create a wide format dataframe with all the temp data expanded
df1_wide = pd.DataFrame(dfg1.Temp.tolist(), index=dfg1.index)


# adding the data between 1990/01/01 -/04/23 and delete the 29th of Feb
rng = pd.date_range(start='1990-01-01', end='1990-04-23', freq='D')
df = pd.DataFrame(index= rng)
df.index = pd.to_datetime(df.index)
df['Temp'] = np.NaN
frames = [df, df1]
result = pd.concat(frames)
result = result[~((result.index.month == 2) & (result.index.day == 29))]

dfg1 = result.groupby(result.index.year).agg({'Temp': list})
df1_wide = pd.DataFrame(dfg1['Temp'].tolist(), index=dfg1.index)

# Setting all leftover empty fields to the average of that time in order to fill in the gaps
df1_wide = df1_wide.apply(lambda x: x.fillna(x.mean()),axis=0)

# ploting the data
fig, (ax1) = plt.subplots(ncols=1, figsize=(20, 5))

##ax1.set_title('Average Daily Air Temperature - Minot Station')
ax1.set_xlabel('Day of the year')
ax1.set_ylabel('Years since start of data collection')

# Setting the title so that it changes based off of the selected station
ax1.set_title('Average Air Temp for ' + str(selected_station))

# Creating Colorbar  
cbm = ax1.matshow(df1_wide, interpolation=None, aspect='auto');

# Plotting the colorbar
cb = plt.colorbar(cbm, ax=ax1)
cb.set_label('Temp in Celsius')

【问题讨论】:

    标签: python pandas numpy matplotlib time-series


    【解决方案1】:

    在代码末尾添加这一行:

    ax1.set_yticklabels(['']+df1_wide.index.tolist()[::5])
    

    【讨论】:

    • 您所做的假设所有数据都从 1990 年开始。如果您将工作站更改为 Bottineau,即使它实际上开始于 1993 年,它也会从 1990 年开始该图。例如:Bottineau,1993- 01-01,-28.75 - 这是 Bottineau 站的第一行数据
    • 我应该如何更改它,以便它基于所选电台数据的开始时间?
    • @XavierConzet:它基于您构建df1_wide 的方式。您将此行rng = pd.date_range(start='1990-01-01', end='1990-04-23', freq='D') 硬编码为Minot。所以,如果你换站,你需要相应地改变它。
    • @XavierConzet:需要对动态处理站点的逻辑进行全面审查。我建议您应该发布一个新问题以引起更多关注。
    • 听起来不错,我想我会这样做。感谢您的所有帮助。
    猜你喜欢
    • 2016-03-17
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多