0

I have a dataframe (see link for image) and I've listed the info on the data frame. I use the pivot_table function to sum the total number of births for each year. The issue is that when I try to plot the dataframe, the y-axis values range from 0 to 2.0 instead of the minimum and maximum values from the M and F columns.

To verify that it's not my environment, I created a simple dataframe, with just a few values and plot the line graph for that dataframe and it works as expected. Does anyone know why this is happening? Attempting to set the values using ylim or yticks is not working. Ultimately, I will have to try other graphing utilities like matplotlib, but I'm curious as to why it's not working for such a simple dataframe and dataset.

Visit my github page for a working example <git@github.com:stevencorrea-chicago/stackoverflow_question.git>

Dataframe

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1690784 entries, 0 to 1690783
Data columns (total 4 columns):
 #   Column  Non-Null Count    Dtype 
---  ------  --------------    ----- 
 0   name    1690784 non-null  object
 1   sex     1690784 non-null  object
 2   births  1690784 non-null  int64 
 3   year    1690784 non-null  Int64 
dtypes: Int64(1), int64(1), object(2)
memory usage: 53.2+ MB
new_df = df.pivot_table(values='births', index='year', columns='sex', aggfunc=sum)
new_df.info()

<class 'pandas.core.frame.DataFrame'>
Index: 131 entries, 1880 to 2010
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   F       131 non-null    int64
 1   M       131 non-null    int64
dtypes: int64(2)
memory usage: 3.1+ KB

plot graphic for dataframe of pivot_table

Test dataframe and plot to show it works for much smaller dataframe

ChicagoSteve
  • 11
  • 1
  • 4
  • Do not include data as an image. Please [create a reproducible copy of the DataFrame with `df.head(10).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246/how-to-provide-a-copy-of-your-dataframe-with-to-clipboard), [edit] the question, and paste the clipboard into a code block or include synthetic data: [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Trenton McKinney Jul 06 '20 at 22:03
  • Just fyi, yaxis doesn't go from 0 to 2, it's from 0 to 2.000.000 (there's a 1e6 next to yaxis, which means the values have to be multiplied by 10^6) – dm2 Jul 06 '20 at 22:04
  • _Ultimately, I will have to try other graphing utilities like matplotlib_ this is using matplotlib. Include the code used to generate the first graph. – Trenton McKinney Jul 06 '20 at 22:06
  • 1
    I'm voting to close this question because the y-axis is properly displayed. The sum for each year is in the millions. – Trenton McKinney Jul 06 '20 at 22:14
  • 1
    [prevent scientific notation in matplotlib.pyplot](https://stackoverflow.com/questions/28371674/prevent-scientific-notation-in-matplotlib-pyplot) – Trenton McKinney Jul 06 '20 at 22:16
  • The comments have all been helpful. I'm new to stackoverflow and I'm learning the proper protocol for posting. – ChicagoSteve Jul 06 '20 at 22:47
  • No worries, welcome to stackoverflow. If you have yet to do so, you could check out the site tour https://stackoverflow.com/tour and 'How to Ask A Good Question': https://stackoverflow.com/help/how-to-ask to familiarize with the site :) – dm2 Jul 06 '20 at 22:54

0 Answers0