1

I'm trying to plot columns of a dataframe. There are 9 columns in the DF (the first being the days of the current week, and four pairs of columns after that - each pair representing total_sessions and average session duration.

The first pair of session-data columns is for the current week, the next pair for the previous week, next for the week ending two weeks ago, and the final pair of columns for week ending three weeks ago. I am trying to create two plots, the first for the four weeks of total_sessions data and the second for the four weeks of average session_duration data. Lets call these Plot 1 and Plot2

The python code for plotting Plot 1 and Plot2 using pandas DataFrame.plot is as follows:

#PLOT1: CURR WEEK DAILY TOTAL SESSIONS AND ALSO LAST 3 WEEKS
# gca stands for 'get current axis'
ax = plt.gca()

df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='IOS_TOTAL_SESSIONS',ax=ax)
df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='WM1_IOS_TOTSESSN', color='red', ax=ax)
df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='WM2_IOS_TOTSESSN', color='green', ax=ax)
df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='WM3_IOS_TOTSESSN', color='cyan', ax=ax)

plt.show()
plt.savefig("iOSWOWAvgDailySessionCount.png")



#PLOT2: CURR WEEK DAILY AVERAGE SESSION  DURATION AND ALSO LAST 3 WEEKS
ax2 = plt.gca()
df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='IOS_AVG_SESSION_DURATION',ax=ax2)
df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='WM1_IOS_AVSESDUR', color='red', ax=ax2)
df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='WM2_IOS_AVSESDUR', color='green', ax=ax2)
df_CurrWeekiOSDailySessionCountDuration.plot(kind='line',x='SESSION_DATE',y='WM3_IOS_AVSESDUR', color='cyan', ax=ax2)

plt.show()
plt.savefig("iOSWOWAvgDailySessionDuration.png")

The dataframe has the correct data (as printed below)

enter image description here

However the code just creates the first plot successfully. For the second plot it gives me a "no numeric data to plot" error (see below)

enter image description here

Appreciate any help or pointers on what I am doing wrong/how to fix this.

trsvchn
  • 8,033
  • 3
  • 23
  • 30
CoolDocMan
  • 637
  • 7
  • 29
  • 3
    Does this answer your question? [Saving a figure after invoking pyplot.show() results in an empty file](https://stackoverflow.com/questions/21875356/saving-a-figure-after-invoking-pyplot-show-results-in-an-empty-file) – trsvchn Feb 07 '20 at 21:08
  • thanks for the tip. unfortunately not. I deferred using the show as suggested on that post, but I still get a blank second plot. When I try plotting the second plot first, it still does not show - and I see this error (perhaps related?) "from_bounds() argument after * must be an iterable, not float" (I also did a round(0) on the entire dataframe to ensure that all values are int but still no luck. – CoolDocMan Feb 11 '20 at 20:26
  • The second plot appears to be picking up incorrect values. The y axis should represent average session duration which ranges from 400 to 750 whereas the y axis in the blank plot ranges from 0 to 1. Likewise the X axis (supposed to be date) again is 0 to 1. – CoolDocMan Feb 12 '20 at 02:47
  • Even when I comment out the first plot I see the same problem. – CoolDocMan Feb 12 '20 at 02:49
  • Did you check your df columns type, there should be no `object` or `str` type: `df_CurrWeekiOSDailySessionCountDuration.dtypes`. – trsvchn Feb 12 '20 at 06:16
  • [pandas DataFrame "no numeric data to plot" error](https://stackoverflow.com/questions/31494870/pandas-dataframe-no-numeric-data-to-plot-error) – trsvchn Feb 12 '20 at 06:46
  • Got it to work! However the solution was not the one in the referenced post from adreasdr and not a perfect solution wither. I worked around the problem by rounding up the data (in the SQL query prior to this code) to integer values. – CoolDocMan Feb 12 '20 at 11:55
  • Oh I missed your note above regarding the "no numeric data to plot" post from harijay. Yes that appears to be the right solution i.e. without compromising precision. Will try that as well. – CoolDocMan Feb 12 '20 at 11:59

0 Answers0