绘制熊猫数据框时,y轴值显示不正确

时间:2020-07-06 21:59:22

标签: pandas dataframe plot pivot-table

我有一个数据框(请参阅图像链接),并且已在数据框上列出了信息。我使用pivot_table函数对每年的总出生人数求和。问题是,当我尝试绘制数据框时,y轴值的范围从02.0,而不是MF的最小值和最大值列。

为验证这不是我的环境,我创建了一个简单的数据框,其中仅包含几个值,并绘制了该数据框的折线图,其工作正常。有人知道为什么会这样吗?尝试使用ylimyticks设置值不起作用。最终,我将不得不尝试其他绘图工具,例如matplotlib,但我很好奇为什么它不适用于如此简单的数据框和数据集。

访问我的github页面以获取工作示例

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

0 个答案:

没有答案