Search code examples
pythonspyderpython-ggplot

Spyder newbie can't see generated chart


I am very new to Spyder. I've found it useful but one thing that continues to confuse me is working with graphs and charts. Below is work-in-progress code building toward showing a bar chart showing subway useage on rainy and dry days. At this point I'm simply trying to see rainy days (I am not much of a programmer so I proceed by baby steps). The IPython console returns

runfile('C:/Users/Brian/Dropbox/Data_Science_Course/Final Intro Data Science Project/rainy vs dry bar chart.py', wdir='C:/Users/Brian/Dropbox/Data_Science_Course/Final Intro Data Science Project')

So it looks like it has at least run. However, I CANNOT find how to actually view the graph.

Here is the code I'm running:

import numpy as np
import pandas as pd
from pandas import DataFrame, Series

pd.set_option('display.notebook_repr_html', False)
pd.set_option('display.max_columns', 10)
pd.set_option('display.max_rows', 10)

from ggplot import *

def show_rainy_days():

    subway_data_df = pd.read_csv('/home/brian/Dropbox/Data_Science_Course/Final Intro Data Science Project/turnstile_weather_v3_test.csv')

    rainy_days_df = subway_data_df[subway_data_df.rain == 1]

    dry_days_df = subway_data_df[subway_data_df.rain == 0]

    print rainy_days_df.mean()['ENTRIESn']

    print dry_days_df.mean()['ENTRIESn']

    print ggplot(rainy_days_df, aes('UNIT', 'ENTRIESn_hourly')) + geom_bar(stat='bar') + ggtitle('Rainy Day Subway Useage') + xlab('Stations on Rainy Days') + ylab('Average Riders')

What Spyder settings am I missing or what else am I doing wrong??


Solution

  • You need to call your show_rainy_days function at the end of your file, like this:

    def show_rainy_days():
        ...
        ggplot(rainy_days_df, aes('UNIT', 'ENTRIESn_hourly')) + geom_bar(stat='bar') + ggtitle('Rainy Day Subway Useage') + xlab('Stations on Rainy Days') + ylab('Average Riders')
    
    
    show_rainy_days()
    

    That's why your code is not doing anything :-)

    Note: The three dots I wrote above (before the last ggplot command) mean that everything is the same as in your code above. The only thing that I changed is removing print before ggplot because that's not needed.