0

I have an issue when plotting a boxplot in a subplot. My data is a time series with datetime index which contains NaNs. This is the code

fig, ax = plt.subplots(1,1, figsize=(5, 3))
ax.boxplot([df['col1'], df['col2']])
plt.show()

It shows and empty graph (Figue (a) below) and 'RuntimeWarning: Invalid value encountered in percentile interpolation=interpolation.' When NaNs are removed, the plot is showing, but I don't want to remove NaNs before boxplotting because the result is different.
enter image description here

When using the following code, it returns the correct plot (Figure (b)), but not in a subplot.

df.boxplot(column=['col1', 'col2'], figsize=(5, 3))

I want something like Figure (b) in a subplot.

k.ko3n
  • 954
  • 8
  • 26
  • Can you put the whole code? – Petronella May 23 '19 at 09:58
  • @Petronella Those are the whole code, except that the resulted graphs are individual instead of in one figure. – k.ko3n May 23 '19 at 10:04
  • 1
    maybe try `df.plot(kind='box', subplots=True)` ? – Chris Adams May 23 '19 at 10:16
  • @Chris A. Thanks, i can implement it to get a plot where one graph represents a column. Don't know yet how to make the plot more customizable, such as: two boxplots in one graph, a different color for each graph, etc. Using ```fig, ax = plt.subplots(........)``` then ```ax.boxplot(......)``` seems to have more freedom to me. – k.ko3n May 23 '19 at 10:41
  • this might help you: https://stackoverflow.com/questions/22483588/how-can-i-plot-separate-pandas-dataframes-as-subplots – Petronella May 23 '19 at 11:34

1 Answers1

0

As you rightly said, the problem could be due to NANs in the columns. If you don't want to remove NANs, replace them with a '0' or an appropriate value you think.

The below shall work.

df.fillna(0, inplace=True)
Srinivas
  • 568
  • 1
  • 4
  • 21
  • 1
    I don't think nan should be replaced with 0 since this will impact box plot. Nan values either be dropped or ignored from data frame. – lostin Apr 23 '20 at 14:48