【问题标题】:Matplotlib Time-Series Heatmap Visualization Row ModificationMatplotlib 时间序列热图可视化行修改
【发布时间】:2020-09-23 06:32:44
【问题描述】:

提前感谢您的帮助!

我正在尝试从时间序列数据创建热图,数据从年中开始,这导致我的热图顶部向左移动,与图的其余部分不匹配(如图所示以下)。我将如何移动仅顶线,以便数据的可视化与绘图的其余部分同步?

(下面提供的代码)

import pandas as pd
import matplotlib.pyplot as plt

# links to datadata
url1 = 'https://raw.githubusercontent.com/the-datadudes/deepSoilTemperature/master/minotDailyAirTemp.csv'

# load the data into a DataFrame, not a Series
# parse the dates, and set them as the index
df1 = pd.read_csv(url1, parse_dates=['Date'], index_col=['Date'])

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

# ploting the data

fig, (ax1) = plt.subplots(ncols=1, figsize=(20, 5))


ax1.matshow(df1_wide, interpolation=None, aspect='auto');

【问题讨论】:

    标签: python pandas numpy dataframe matplotlib


    【解决方案1】:

    现在,问题是什么,数据集上的日期,如果你看到数据集这个开始

    `1990-4-24,15.533`
    

    为了解决这个问题,需要添加 1990/01/01 -/04/23 之间的数据并删除 29Feb。

    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)
    
    # ploting the data
    
    fig, (ax1) = plt.subplots(ncols=1, figsize=(20, 5))
    
    
    ax1.matshow(df1_wide, interpolation=None, aspect='auto');
    

    未填充部分的问题是数据集上的 NaN 值的结果,在这种情况下,您可以选择将 NaN 值替换为列均值或行均值。 还有其他方法可以替换 NaN 值

    df1_wide = df1_wide.apply(lambda x: x.fillna(x.mean()),axis=0)
    

    【讨论】:

    • 我了解您正在对绘图上的其余数据进行平均,但最上面的行对于 250 标记之外的所有数据仍然不同步。我如何将它移到右侧并填写左侧出现的空白区域?我还更新了原始问题中的图像,以使我的要求更加明显
    猜你喜欢
    • 2016-04-10
    • 2018-03-17
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多