使用子图和循环绘制 Pandas groupby 组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30379645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:22:47  来源:igfitidea点击:

Plotting Pandas groupby groups using subplots and loop

pythonpandasplotsubplotpandas-groupby

提问by fireitup

I am trying to generate a grid of subplots based off of a Pandas groupby object. I would like each plot to be based off of two columns of data for one group of the groupby object. Fake data set:

我正在尝试根据 Pandas groupby 对象生成子图网格。我希望每个图都基于 groupby 对象的一组的两列数据。假数据集:

C1,C2,C3,C4
1,12,125,25
2,13,25,25
3,15,98,25
4,12,77,25
5,15,889,25
6,13,56,25
7,12,256,25
8,12,158,25
9,13,158,25
10,15,1366,25

I have tried the following code:

我尝试了以下代码:

import pandas as pd
import csv   
import matplotlib as mpl
import matplotlib.pyplot as plt
import math

#Path to CSV File
path = "..\fake_data.csv"

#Read CSV into pandas DataFrame
df = pd.read_csv(path)

#GroupBy C2
grouped = df.groupby('C2')

#Figure out number of rows needed for 2 column grid plot
#Also accounts for odd number of plots
nrows = int(math.ceil(len(grouped)/2.))

#Setup Subplots
fig, axs = plt.subplots(nrows,2)

for ax in axs.flatten():
    for i,j in grouped:
        j.plot(x='C1',y='C3', ax=ax)

plt.savefig("plot.png")

But it generates 4 identical subplots with all of the data plotted on each (see example output below):

但它会生成 4 个相同的子图,每个子图上都绘制了所有数据(请参见下面的示例输出):

enter image description here

在此处输入图片说明

I would like to do something like the following to fix this:

我想执行以下操作来解决此问题:

for i,j in grouped:
    j.plot(x='C1',y='C3',ax=axs)
    next(axs)

but I get this error

但我收到这个错误

AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'

AttributeError: 'numpy.ndarray' 对象没有属性 'get_figure'

I will have a dynamic number of groups in the groupby object I want to plot, and many more elements than the fake data I have provided. This is why I need an elegant, dynamic solution and each group data set plotted on a separate subplot.

我将在要绘制的 groupby 对象中拥有动态数量的组,以及比我提供的假数据更多的元素。这就是为什么我需要一个优雅的动态解决方案,并将每个组数据集绘制在一个单独的子图上。

回答by mcwitt

Sounds like you want to iterate over the groups and the axes in parallel, so rather than having nested forloops (which iterates over all groups for eachaxis), you want something like this:

听起来你想并行迭代组和轴,所以与其使用嵌套for循环(它为每个轴迭代所有组),你想要这样的东西:

for (name, df), ax in zip(grouped, axs.flat):
    df.plot(x='C1',y='C3', ax=ax)

enter image description here

在此处输入图片说明

You have the right idea in your second code snippet, but you're getting an error because axsis an array of axes, but plotexpects just a single axis. So it should also work to replace next(axs)in your example with ax = axs.next()and change the argument of plotto ax=ax.

您在第二个代码片段中有正确的想法,但您收到错误,因为它axs是一个轴数组,但plot只需要一个轴。因此,它也应该可以next(axs)在您的示例中替换为ax = axs.next()并更改plotto的参数ax=ax