0

Basically I have a dataframe from a csv file that ive made some changes to using pandas. Now I have to plot specific columns that compare data within the dataframe.

The last row with daily information has a day field of 31 which gives you a way to programmatically find the date of that row. I'm trying to Plot the male births first, then the female births on the same plot. Using the date column as the x axis values. For some reason the data doesn't show up on the graph. I've tried everything I can think of but for some reason the data doesn't show up? what am I doing wrong?

    import pandas as pd
    import matplotlib
    import matplotlib.pyplot as plt
    matplotlib.rcParams['figure.figsize'] = (8.0, 3.0)

    births = pd.read_csv("births.csv")
    births = births.drop(births[births.day == 99].index)
    births = births.drop(births[births.births < 500].index)
    births['day'].fillna(1, inplace=True)
    births['day'] = births['day'].astype('int32')
    births.dtypes
    births['Date'] = pd.to_datetime(births[['year','month','day']])
    print(births)
    newbirth = births[births['day'] == 31]


    start = pd.to_datetime(births['Date'].iloc[0])
    end = pd.to_datetime(newbirth['Date'].iloc[-1])

    births1 = births[births['gender'] == 'M']
    births2 = births[births['gender'] == 'F']
    births1.reset_index(inplace = True)
    births2.reset_index(inplace = True)

    #start1 = births1[births1['Date'] <= last]
    #start2 = births2[births2['Date'] <= last]

    new_plot = plt.subplot()
    new_plot.plot(births1['births'], label ='Males')
    new_plot.plot(births2['births'], label ='Females')
    new_plot.set_xlim(start, end)
    new_plot.set_ylim(0,6500)
    new_plot.set_ylabel('Daily Births')
    new_plot.set_title("Daily Births By Gender")
    new_plot.legend(loc='lower right')
    new_plot.tick_params(axis='x',rotation=45)

ill post a little bit of the dataframe's output so you can have an idea of what it looks like:

           year  month  day gender  births       Date
    0      1969      1    1      F    4046 1969-01-01
    1      1969      1    1      M    4440 1969-01-01
    2      1969      1    2      F    4454 1969-01-02
    3      1969      1    2      M    4548 1969-01-02
    4      1969      1    3      F    4548 1969-01-03
    5      1969      1    3      M    4994 1969-01-03
    6      1969      1    4      F    4440 1969-01-04
    7      1969      1    4      M    4520 1969-01-04
    8      1969      1    5      F    4192 1969-01-05
    9      1969      1    5      M    4198 1969-01-05
    10     1969      1    6      F    4710 1969-01-06
    11     1969      1    6      M    4850 1969-01-06
    12     1969      1    7      F    4646 1969-01-07
    13     1969      1    7      M    5092 1969-01-07
    14     1969      1    8      F    4800 1969-01-08
    15     1969      1    8      M    4934 1969-01-08
    16     1969      1    9      F    4592 1969-01-09
    17     1969      1    9      M    4842 1969-01-09
    18     1969      1   10      F    4852 1969-01-10
    19     1969      1   10      M    5190 1969-01-10
    20     1969      1   11      F    4580 1969-01-11
    21     1969      1   11      M    4598 1969-01-11
    22     1969      1   12      F    4126 1969-01-12
    23     1969      1   12      M    4324 1969-01-12
    24     1969      1   13      F    4758 1969-01-13
    25     1969      1   13      M    5076 1969-01-13
    26     1969      1   14      F    5070 1969-01-14
    27     1969      1   14      M    5296 1969-01-14
    28     1969      1   15      F    4798 1969-01-15
    29     1969      1   15      M    5096 1969-01-15
     ...     ...    ...  ...    ...     ...        ...
    15517  2007     10    1      F  180912 2007-10-01
    15518  2007     10    1      M  189157 2007-10-01
    15519  2007     11    1      F  173513 2007-11-01
    15520  2007     11    1      M  180814 2007-11-01
    15521  2007     12    1      F  173787 2007-12-01
    15522  2007     12    1      M  181426 2007-12-01
    15523  2008      1    1      F  174255 2008-01-01
    15524  2008      1    1      M  182789 2008-01-01
    15525  2008      2    1      F  165669 2008-02-01
    15526  2008      2    1      M  173434 2008-02-01
    15527  2008      3    1      F  172053 2008-03-01
    15528  2008      3    1      M  179129 2008-03-01
    15529  2008      4    1      F  169585 2008-04-01
    15530  2008      4    1      M  177399 2008-04-01
    15531  2008      5    1      F  173141 2008-05-01
    15532  2008      5    1      M  182294 2008-05-01
    15533  2008      6    1      F  169958 2008-06-01
    15534  2008      6    1      M  179267 2008-06-01
    15535  2008      7    1      F  183391 2008-07-01
    15536  2008      7    1      M  192714 2008-07-01
    15537  2008      8    1      F  182713 2008-08-01
    15538  2008      8    1      M  191315 2008-08-01
    15539  2008      9    1      F  179696 2008-09-01
    15540  2008      9    1      M  188964 2008-09-01
    15541  2008     10    1      F  175314 2008-10-01
    15542  2008     10    1      M  183219 2008-10-01
    15543  2008     11    1      F  158939 2008-11-01
    15544  2008     11    1      M  165468 2008-11-01
    15545  2008     12    1      F  173215 2008-12-01
    15546  2008     12    1      M  181235 2008-12-01
  • Do you see a window with a graph at all? If not, try plt.show() – warped Apr 21 '19 at 17:48
  • yes it does show a window, it shows a graph but no data is in it, like no lines or points inside the graph show, is there a way to post a screenshot or something in here like a gyazo? @warped – programmernoob Apr 21 '19 at 17:55
  • change new_plot.plot(births1['births'], label ='Males') to new_plot.plot(births1['Date'], births1['births'], label ='Males'), same for the births2. Reason: if you don't explicitly state which data should be taken for the x-axis, the index will be taken. The index of your dataframes is a range of int, incompatible with the datetime that you specify via set_xlim() – warped Apr 21 '19 at 18:19
  • Also, you might need to specify arguments in `subplots()` with initialized figure. See: [How do I get multiple subplots in matplotlib?](https://stackoverflow.com/questions/31726643/how-do-i-get-multiple-subplots-in-matplotlib) – Parfait Apr 21 '19 at 18:26
  • awesome it's plotting data now, thanks for the help guys :) @warped – programmernoob Apr 21 '19 at 18:33
  • and also thanks @Parfait – programmernoob Apr 21 '19 at 18:33

0 Answers0