【发布时间】:2020-07-15 08:55:46
【问题描述】:
我有一个大型数据集,其中包含按国家和年份分列的数千行(纵向文本数据)。如以下数据框所示,wordcount 列表示“世俗”一词的出现次数。
df3
index country text wordcount year
0 Bolivia This is an example text.. 1 2010
1 Bolivia This is an example text2.. 5 2015
2 Bolivia This is an example text3 .. 7 2017
现在我想分别为所有国家/地区创建子图(散点图),其中year 将在x-axis 和wordcount 上为每个国家/地区提供y-axis。以下代码分别为我提供了每个国家/地区所需的图,但我需要将它们组合起来。例如,每行将包含 10 个国家/地区。有没有简单的方法来做到这一点?任何帮助,将不胜感激。谢谢你。如果需要任何澄清,请告诉我。
import matplotlib.pyplot as plt
for title, group in df3.groupby('country'):
group.plot(x='year', y='wordcount', title=title)
更新:
我也尝试过使用以下代码,但我想它不会在一年内多次总结 wordcount 的相同值。换句话说,与之前的代码相比,我得到的单词出现次数更少(单独的国家图)。
fig, axes = plt.subplots(nrows=11, ncols=8, sharex=True, sharey=True, figsize=(18,10))
axes_list = [item for sublist in axes for item in sublist]
for countryname, selection in df3.head(1200).groupby("country"):
ax = axes_list.pop(0)
selection.plot(x='year', y='wordcount', label=countryname, ax=ax, legend=False)
ax.set_title(countryname)
ax.tick_params(
which='both',
bottom='off',
left='off',
right='off',
top='off'
)
ax.grid(linewidth=0.5)
ax.set_xlim((1980, 2020))
ax.set_xlabel("")
ax.set_xticks(range(1980, 2020, 10))
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_ylim((0, 10))
for ax in axes_list:
ax.remove()
plt.subplots_adjust(hspace=1)
plt.tight_layout()
【问题讨论】:
-
尝试使用matplotlib.pyplot.subplots构造一个循环
-
我试过这个。我刚刚更新了我使用的代码以及答案中的问题。谢谢。
标签: python python-3.x pandas matplotlib