这段简单的代码按预期绘制折线图:
james_f=names[(names.name=='James') & (names.sex=='F')]
plt.plot(james_f['year'],james_f['births'])
plt.show()
但后来我改变条件,只删除其中一个,然后它开始绘制条形图。为什么以及如何强制绘制折线图?
james_f=names[(names.name=='James')]
plt.plot(james_f['year'],james_f['births'])
plt.show()
添加而不是它 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/