Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在matplotlib(python)中组合组图_Python_Python 3.x_Pandas_Matplotlib - Fatal编程技术网

如何在matplotlib(python)中组合组图

如何在matplotlib(python)中组合组图,python,python-3.x,pandas,matplotlib,Python,Python 3.x,Pandas,Matplotlib,我有一个按国家和年份划分的数千行的大型数据集(纵向文本数据)。如下数据框所示,wordcount列表示单词“长期”的出现次数 df3 index country text wordcount year 0 Bolivia This is an example text.. 1 2010 1 Bolivia This is an example text2.

我有一个按国家和年份划分的数千行的大型数据集(纵向文本数据)。如下数据框所示,
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轴
wordcount
上。下面的代码分别为每个国家提供了所需的绘图,但我需要将它们合并。例如,每行将有10个国家。有没有简单的方法?任何帮助都将不胜感激。非常感谢。如果需要澄清,请告诉我

import matplotlib.pyplot as plt

for title, group in df3.groupby('country'):
    group.plot(x='year', y='wordcount', title=title)
更新: 我也尝试过使用下面的代码,但我想它不会在一年内多次为
wordcount
计算相同的值。换句话说,与以前的代码(单独的国家/地区图)相比,我得到的单词出现次数更少


您需要计算每个国家/地区每年的
wordcount
值的总数:

sum_df=pd.DataFrame(df3.groupby(['Country','year']).wordcount.sum()).reset_index()
然后:


您需要计算每个国家/地区每年的
wordcount
值的总数:

sum_df=pd.DataFrame(df3.groupby(['Country','year']).wordcount.sum()).reset_index()
然后:


我得到了
ValueError:索引包含重复的条目,即使将年份重置为索引,也无法重塑
错误。可能在某些情况下,对于相同的
年份
国家
您有多个
字数
值?你需要先解决这些问题!是的,我在同一年有多个
wordcount
值,因为每年都有多行/文本文件。您希望如何处理这些多个值?你想取平均值、最大值、最小值或其他值吗?我想取
计数
或它们的出现次数。我得到
值错误:索引包含重复项,即使在将年份重置为索引后,也无法重塑
错误。可能在某些情况下,对于相同的
年份
国家
您有多个
字数
值?你需要先解决这些问题!是的,我在同一年有多个
wordcount
值,因为每年都有多行/文本文件。您希望如何处理这些多个值?你想取平均值、最大值、最小值还是其他值?我想取
计数
或它们的出现次数。尝试使用matplotlib.pyplot.SubPlot构建循环我尝试了这个方法。我刚刚更新了我使用的代码和答案中的问题。谢谢。请尝试使用matplotlib.pyplot.SubPlot构建一个循环。我尝试过这样做。我刚刚更新了我使用的代码和答案中的问题。谢谢
df_pivot = sum_df.pivot(index='year', columns='Country', values='wordcount')
df_pivot.plot()