python - 为什么 matplotlib 开始绘制条形图而不是折线图

标签 python python-3.x matplotlib bar-chart linechart

这段简单的代码按预期绘制折线图:

james_f=names[(names.name=='James') & (names.sex=='F')]
plt.plot(james_f['year'],james_f['births'])    
plt.show()

enter image description here

但后来我改变条件,只删除其中一个,然后它开始绘制条形图。为什么以及如何强制绘制折线图?

james_f=names[(names.name=='James')]
plt.plot(james_f['year'],james_f['births'])    
plt.show()

enter image description here

添加而不是它 1==1 规则,没有任何变化(

james_f=names[(names.name=='James') & ( 1 == 1)]
plt.plot(james_f['year'],james_f['births'])    
plt.show()

即使是这段代码也会绘制条形图:

james_f=names[(names.name=='James') | (names.name=='John') | (names.name=='Robert') ]
plt.plot(james_f['year'],james_f['births'])

james_f['births'] 输出 (pandas.core.series.Series):

228           46
343           22
538           11
942         9655
944         5927
2312          26
2329          24
2617           9
2938        8769
....
Name: births, dtype: int64

james_f['births'].min() 返回 7 没有零值或 NaN 值

>>> print(james_f[james_f['births'].isnull()])
Empty DataFrame
Columns: [name, sex, births, year]
Index: []


>>> james_f.head(10)
        name    sex births  year
343     James   F   22      1880
944     James   M   5927    1880
2329    James   F   24      1881
2940    James   M   5441    1881
4372    James   F   18      1882
4965    James   M   5892    1882
6428    James   F   25      1883
7118    James   M   5223    1883
8488    James   F   33      1884
9320    James   M   5693    1884

最佳答案

如果不过滤性别,每年会产生两种观察结果:一种针对女性,一种针对男性。名字为“詹姆斯”的男女人数差异很大,使情节显得非常嘈杂。您(至少)有两个选择:

(1) 像这样总结男女人数。

james = names[names.name == 'james']
years = []
births = []
for year, subset in james.groupby('year'):
    years.append(year)
    births.append(subset.births.sum())

plt.plot(years, births)

对 pandas 有更多了解的人可能会把它写成一行。

(2) 像这样为男性和女性绘制两条单独的线。

james = names[names.name == 'james']
for sex, subset in james.groupby('sex'):
    plt.plot(subset.year, subset.births, label=sex)
plt.legend()

关于python - 为什么 matplotlib 开始绘制条形图而不是折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755784/

相关文章:

python - "' 时间戳 ' object does not support indexing", 'occurred at index 0' )

python - 使用 matplotlib 绘制 2D 像素图

python - 从不同的工作目录导入 Python 模块

python - 如何在不使用模型的情况下将 Django 集成到现有数据库中

python - 为什么我的图像按钮没有出现?

python-3.x - 升级到 Ubuntu 20.04 后,python 3.7 venv 损坏

python - 使用 matplotlib 绘制带有纪元时间 x 轴的图表

python - 导出为 PDF 时如何避免使用 matplotlib 调整图形大小?

python - 在 pandas read_csv() 中引用参数

Python:urwid:试图处理不同的 View