生成多散点子图 - 返回意外结果
Generate a multi scatter subplot - unexpected results returned
我是 python 的新手,我正在尝试创建多个月的子图。发生的情况是生成了 3 x3 的块但是是空的,然后每个图表都在另一个图表下面,这不允许轻松查看。
这是我从类似问题中提取的代码。
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()
你需要给plot函数的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 returns 转置数据框,如果您的列名是 T 那么您需要明确编写 df['T']
而不是 df.T
)
PS: 为了方便创建样本数据 你可以使用numpy的repeat and tile函数:
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))})
我是 python 的新手,我正在尝试创建多个月的子图。发生的情况是生成了 3 x3 的块但是是空的,然后每个图表都在另一个图表下面,这不允许轻松查看。
这是我从类似问题中提取的代码。
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()
你需要给plot函数的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 returns 转置数据框,如果您的列名是 T 那么您需要明确编写 df['T']
而不是 df.T
)
PS: 为了方便创建样本数据 你可以使用numpy的repeat and tile函数:
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))})