Python 生成多分散子批次-返回意外结果

Python 生成多分散子批次-返回意外结果,python,matplotlib,subplot,scatter,Python,Matplotlib,Subplot,Scatter,我是python新手,我正在尝试创建多个月的子地块。发生的情况是,生成了3x3的块,但它是空的,然后每个图表都在另一个下面,这不便于查看 这是我的代码,我从一个类似的问题中提取出来 def scat_months2(df,prod): """Print scatter for all months as sub plots of any given product""" uniq=sorted(set(train2.YM))[0:9] fig, axes = plt.su

我是python新手,我正在尝试创建多个月的子地块。发生的情况是,生成了3x3的块,但它是空的,然后每个图表都在另一个下面,这不便于查看

这是我的代码,我从一个类似的问题中提取出来

def scat_months2(df,prod):
    """Print scatter for all months as sub plots of any given product"""
    uniq=sorted(set(train2.YM))[0:9]
    fig, axes = plt.subplots(3, 3, figsize=(6, 4), sharex=True, sharey=True)
    for period in uniq:
        df[(df["YM"]==period) & (df["item_id"]==prod)].plot(x='shop_id',
            y='item_price',
            kind='scatter',
            label=period,alpha=0.2)

    fig.tight_layout()
我已经尝试为生成一些随机数据,以便您可以帮助我,但这也没有像我希望的那样成功(同样是python新手)。。。它会产生不同的错误。我希望这仍然可以让您轻松修复我的示例,然后看到与我看到的相同的结果

如果你告诉我如何正确地生成随机数据,那将真正有助于我的学习。 我很抱歉不能提供一个功能示例

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

T=pd.Series([201301,201301,201301,201301,201301,201301,201301,201301,201301])
Shop=pd.Series([1,1,1,2,2,2,3,3,3])
Price=pd.Series(np.random.randint(10, size=(9)))
ds1=pd.DataFrame(dict(T = T, Shop=Shop,Price = Price))
T2=pd.Series([201302,201302,201302,201302,201302,201302,201302,201302,201302])
ds2=pd.DataFrame(dict(T = T2, Shop=Shop,Price = Price))
T3=pd.Series([201303,201303,201303,201303,201303,201303,201303,201303,201303])
ds3=pd.DataFrame(dict(T = T3, Shop=Shop,Price = Price))
ds=pd.concat([ds1,ds2,ds3], axis=0)
ds.index=range(27)

def scat_months2(df):
    """Print scatter for all months as sub plots of any given product"""
    uniq=sorted(set(df.T))
    fig, axes = plt.subplots(3, 1, figsize=(6, 4), sharex=True, sharey=True)
    for period in uniq:
        df[df["T"]==period].plot(x='Shop',
            y='Price',
            kind='scatter')

    fig.tight_layout()

您需要指定绘图函数的
ax
参数:

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

T=pd.Series([201301,201301,201301,201301,201301,201301,201301,201301,201301])
Shop=pd.Series([1,1,1,2,2,2,3,3,3])
Price=pd.Series(np.random.randint(10, size=(9)))
ds1=pd.DataFrame(dict(T = T, Shop=Shop,Price = Price))
T2=pd.Series([201302,201302,201302,201302,201302,201302,201302,201302,201302])
ds2=pd.DataFrame(dict(T = T2, Shop=Shop,Price = Price))
T3=pd.Series([201303,201303,201303,201303,201303,201303,201303,201303,201303])
ds3=pd.DataFrame(dict(T = T3, Shop=Shop,Price = Price))
ds=pd.concat([ds1,ds2,ds3], axis=0)
ds.index=range(27)

def scat_months2(df):
    """Print scatter for all months as sub plots of any given product"""
    uniq=sorted(set(df['T']))
    fig, axes = plt.subplots(len(uniq), 1, figsize=(6, 4), sharex=True, sharey=True)
    for i, period in enumerate(uniq):
        df[df["T"]==period].plot(x='Shop',
            y='Price',
            kind='scatter',
            ax=axes[i])

    fig.tight_layout()
    plt.show()

scat_months2(ds)

(在您的示例中有一个小错误,我对此进行了纠正:df.T返回转置的数据帧,如果列名为T,则需要显式地编写
df['T']
,而不是
df.T

PS:为了方便创建样本数据您可以使用numpy的和功能:

months = [201301, 201302, 201303]
shops = [1,2,3]
n = 3
df = pd.DataFrame({'Month': np.repeat(months, n*len(shops)), 'Shop': np.tile(shops, n*len(months)), 'Price': np.random.randint(10, size=n*len(shops)*len(months))})

非常感谢。我在我的实际数据集上运行了这个,当我按照第一段代码添加标签或设置alpha时,它失败了。错误为AttributeError:'numpy.ndarray'对象没有属性'get_figure'。然而,当我运行你的代码时,它是有效的。。。我将尝试找出差异所在,并复制您向我展示的如何更轻松地创建数据的示例。这是因为您的绘图网格为2D(3x3),在本例中使用
ax=axes.flatte()[I]
。这已经奏效。我真的不明白为什么,但好吧。。。实际上我想用这种方式打印36个月的数据。当我试图将尺寸更改为“fig,axes=plt.subplot(12,3,figsize=(6,4),sharex=True,sharey=True)”时,结果难以辨认。你能告诉我为什么吗?谢谢。在不展平2D axis数组的情况下,axis[i]将返回一个axis对象数组,该数组不具有属性
get\u figure
。您需要的是一个单轴对象,例如,
axes[i][j]
,但由于我们没有第二个索引
j
,我们首先将2D数组展平为1D数组。