Search code examples
pythonpandasmatplotlibnullline

Matplotlib creates blank chart for rows that have values in pandas dataframe


I have dataframe that is similar to this:

index     5        45       60    85    6        133  ....... 137   141   number_points
0         0.35    0.63     NaN   0.78  0.84     0.67         0.84  NaN       10
1         NaN     NaN      NaN   0.11   NaN     0.21         0.09  NaN        4
2         NaN     NaN      NaN   0.88   NaN     0.25         NaN   NaN        7
3         NaN     0.53     0.34  NaN    NaN     NaN          NaN   0.1        5
4         NaN     NaN      NaN   NaN    NaN     NaN          NaN   NaN        0
5         NaN     NaN      NaN   NaN    NaN     NaN          NaN   NaN        5
3095 rows × 146 columns

The dataframe columns are int, which are basically count of days (1 is day 1, 5 is day 5 ect. ,the oclumns are not organized by the order but the columns names are int type).

the "number_points" column is the number of columns that have value that is not null in each row.

I wanted to create line plot that will show the number of days in the x axis and the measurment on the y axis. For some plots it worked, like this:

#the numebrs are different, from the original dataframe
plt.figure(figsize=(10,8))
df.iloc[1,:-1].T.plot()
plt.show()

enter image description here

But some plots don't show any value:

#check if there are actually any values to plot
test.iloc[3028,:-1].unique().tolist()

#yes, there are
>>>[nan,
 0.611478498393178,
 0.8732359604823238,
 0.567957634306329,
 0.5389633004976686]

#create line plot
plt.figure(figsize=(10,8))
test.iloc[3028,:-1].T.plot()
plt.show()

enter image description here

I couldn't figure out by now why the plots are blank if there are values. What could be the reason for this?


Solution

  • try .dropna().plot() to remove missing rows and only plot entries with data