Plot pandas dataframe containing NaNs

后端 未结 4 483
星月不相逢
星月不相逢 2020-12-09 16:23

I have GPS data of ice speed from three different GPS receivers. The data are in a pandas dataframe with an index of julian day (incremental from the start of 2009).

相关标签:
4条回答
  • 2020-12-09 16:59

    Given that you want to draw a straight line between the points where you do have data, you can get Pandas to fill in the gaps via interpolation, and then plot:

    .interpolate(method='linear').plot()
    
    0 讨论(0)
  • 2020-12-09 17:12

    The reason your not seeing anything is because the default plot style is only a line. But the line gets interupted at NaN's so only multiple consequtive values will be plotted. And the latter doesnt happen in your case. You need to change the style of plotting, which depends on what you want to see.

    For starters, try adding:

    .plot(marker='o')
    

    That should make all data points appear as circles. It easily gets cluttered so adjusting markersize, edgecolor etc might be usefull. Im not fully adjusted to how Pandas is using matplotlib so i often switch to matplotlib myself if plots get more complicated, eg:

    plt.plot(df.R2.index.to_pydatetime(), df.R2, 'o-')
    
    0 讨论(0)
  • 2020-12-09 17:15

    Here is another way:

    nan_columns = []
    nan_values = []
    
    for column in dataset.columns:
        nan_columns.append(column)
        nan_values.append(dataset[column].isnull().sum())
    
    fig, ax = plt.subplots(figsize=(30,10))
    plt.bar(nan_columns, nan_values)
    
    0 讨论(0)
  • 2020-12-09 17:25

    I found even if the df was indexed as DateTime the same issues occurred. One solution to ensure all data points are respected, with no gaps in between lines, is to plot each df column separately and dropping the NaNs.

        for col in df.columns:
            plot_data = df[col].dropna()
            ax.plot(plot_data.index.values, plot_data.values, label=col)
    
    0 讨论(0)
提交回复
热议问题