Help needed,,, Pivot table VS line graphs

Screen Link:

My Code:

plt.figure(figsize=(11,3.5))
plt.subplot(1,2,1)
bussiness_days.pivot_table(['traffic_volume'],'hours',aggfunc=[np.mean]).plot.line()
plt.show()

plt.subplot(1,2,2)
weekends.pivot_table(['traffic_volume'],'hours',aggfunc=[np.mean]).plot.line()
plt.legend().remove()
plt.show()

What I expected to happen:
I expect two adjacent graphs

What actually happened:

first an empty plot is displayed below Line graph is plotted . And again empty plot then second line plot. Not getting the plots adjacently

Replace this line with the output/error

The same graphs works fine if I use group by clause instead of Pivot.

Hi @vanajak03 and welcome to the community!

As per this post, could you please provide the link to the mission screen for better context and the ability to test out your code?

I believe the reason you are getting these results is due to the differences between generating a plot using df.plot.line() vs. using plt.plot(). A lot of things are done “by default” for you when using pandas plotting compared to the native equivalent in matplotlib.

To get the results you’re after, I would start by creating fig and ax objects and passing ax=ax to df.plot.line() in order to force pandas to use the correct ax object rather than using its default settings which is to create new graphs for each call to df.plot.line().

As a bit of starter code, try this and see what you get:

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize(11, 3.5))
bussiness_days.pivot_table(['traffic_volume'], 'hours', aggfunc=[np.mean]).plot.line(ax=ax1)
weekends.pivot_table(['traffic_volume'], 'hours', aggfunc=[np.mean]).plot.line(ax=ax2)
plt.show()

I’m not 100% sure this will work as I have no way to test the code “in context” but I think it’s a good start. Let me know how you make out and if you need any more help. We could, alternatively, look into using plt.plot() and just pass the data as arguments rather than trying to get pandas to do what we want it to do.

Hi @mathmike314

Thank you for the reply.

Here is the screen which I’m trying to do

https://app.dataquest.io/m/524/guided-project%3A-finding-heavy-traffic-indicators-on-i-94/7/time-indicators-iii

By creating fig and ax got the expected result

Thank you !

Awesome, I’m glad I was able to help you out!