Matplotlib 히트 맵 레이블

Sep 29 2020

안녕하세요, 도와 주셔서 감사합니다! (아래 제공된 코드 및 데이터) (아래 플롯 이미지)

맵의 색상 차이를 설명하는이 히트 맵에 범례를 추가하려고합니다 (따뜻한 색상은 더 높은 온도를 의미 함). 나는 추가하고있다 :

ax1.legend([ax1], ['Temp'])

그리고 문제는이 코드 줄이 내 플롯에 범례를 포함하지 않는다는 것입니다. 온도와 색상의 관계를 설명하는 범례를 추가하려면 어떻게해야합니까?

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()

# 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'] == 'Williston']



# 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('Year of collected data')
ax1.legend([ax1], ['Temp'])
ax1.matshow(df1_wide, interpolation=None, aspect='auto');

답변

4 QuangHoang Sep 29 2020 at 09:55

당신이 요청하는 것은 다음과 colorbar같습니다.

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('Year of collected data')

# register a colorbar mappable 
cbm = ax1.matshow(df1_wide, interpolation=None, aspect='auto');

# plot the colorbar
cb = plt.colorbar(cbm, ax=ax1)
cb.set_label('My Color Map')

산출: