【发布时间】:2021-08-12 08:25:19
【问题描述】:
假设我有以下数据框:
Earthquakes:
latitude longitude place year
0 36.087000 -106.168000 New Mexico 1973
1 33.917000 -90.775000 Mississippi 1973
2 37.160000 -104.594000 Colorado 1973
3 37.148000 -104.571000 Colorado 1973
4 36.500000 -100.693000 Oklahoma 1974
… … … … …
13941 36.373500 -96.818700 Oklahoma 2016
13942 36.412200 -96.882400 Oklahoma 2016
13943 37.277167 -98.072667 Kansas 2016
13944 36.939300 -97.896000 Oklahoma 2016
13945 36.940500 -97.906300 Oklahoma 2016
和Wells:
LAT LONG BBLS Year
0 36.900324 -98.218260 300.0 1977
1 36.896636 -98.177720 1000.0 2002
2 36.806113 -98.325840 1000.0 1988
3 36.888589 -98.318530 1000.0 1985
4 36.892128 -98.194620 2400.0 2002
… … … … …
11117 36.263285 -99.557631 1000.0 2007
11118 36.263220 -99.548647 1000.0 2007
11119 36.520160 -99.334183 19999.0 2016
11120 36.276728 -99.298563 19999.0 2016
11121 36.436857 -99.137391 60000.0 2012
我如何制作一个显示每年 BBLS 数量(来自Wells)和一年中发生的地震数量(来自Earthquakes)的折线图,其中 x 轴显示自 1980 年以来的年份,y1 轴显示每年 BBLS 的总和,而 y2 轴显示地震次数。
我认为我需要创建一个 groupby、count(用于地震)和 sum(用于 BBLS)来制作情节,但我真的尝试了很多编码,但我就是不知道该怎么做。
唯一有用的是地震的折线图,如下所示:
Earthquakes.pivot_table(index=['year'],columns='type',aggfunc='size').plot(kind='line')
不过,对于 BBLS 的折线图,没有任何效果
Wells.pivot_table(index=['Year'],columns='BBLS',aggfunc='count').plot(kind='line')
这一个:
plt.plot(Wells['Year'].values, Wells['BBL'].values, label='Barrels Produced')
plt.legend() # Plot legends (the two labels)
plt.xlabel('Year') # Set x-axis text
plt.ylabel('Earthquakes') # Set y-axis text
plt.show() # Display plot
这个来自另一个thread或者:
fig, ax = plt.subplots(figsize=(10,8))
Earthquakes.plot(ax = ax, marker='v')
ax.title.set_text('Earthquakes and Injection Wells')
ax.set_ylabel('Earthquakes')
ax.set_xlabel('Year')
ax.set_xticks(Earthquakes['year'])
ax2=ax.twinx()
ax2.plot(Wells.Year, Wells.BBL, color='c',
linewidth=2.0, label='Number of Barrels', marker='o')
ax2.set_ylabel('Annual Number of Barrels')
lines_1, labels_1 = ax.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()
lines = lines_1 + lines_2
labels = labels_1 + labels_2
ax.legend(lines, labels, loc='upper center')
【问题讨论】:
-
@SırrıKırımlıoğlu,刚刚做到了。没有为我工作。 ://
标签: python pandas matplotlib counter spyder