从.csv文件的日期框架进行绘图,使用matplotlib和pandas不会在图形上显示数据

时间:2019-04-21 17:32:28

标签: python pandas matplotlib

基本上,我有一个来自csv文件的数据帧,该文件对使用熊猫做了一些更改。现在,我必须绘制特定列以比较数据框内的数据。

包含每日信息的最后一行的日字段为31,这使您可以通过编程方式找到该行的日期。我正在尝试先绘制男性出生,然后再绘制女性出生。使用日期列作为x轴值。由于某种原因,数据没有显示在图表上。我已经尝试了所有可以想到的方法,但是由于某种原因数据没有显示出来?我在做什么错了?

    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)

请发布数据框输出的一些内容,以便您可以 看起来像什么:

           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

0 个答案:

没有答案