修改下列代码的错误import random import pandas as pd import matplotlib.pyplot as plt def generate_data() : products = ['商品1','商品2','商品3','商品4','商品5','商品6','商品7','商品8','商品9','商品10'] datelist = [] for month in range(1,13) : for day in range(1,29) : date = f'2019-{month:20d}-{day:02d}' datelist.append(date) datalist = [] for date in datelist : for it in products : sales = round(random.uniform(150,200),2) datalist.append([date,it,sales]) df = pd.DataFrame(datalist,columns=['date','products','sales']) df.to_csv('data.csv', index=False) return pd.read_csv('data.csv') def plot_sales_by_product(df) : for product in df['products'].unique() : data = df.loc[df['products'] == product] plt.plot(data['date'],data['sales'],label=product) plt.xlabel('Date') plt.ylabel('Sales') plt.title('Sales by Product') plt.legend() plt.show() def plot_sales_by_month(df) : df['month'] = pd.DatetimeIndex(df['date']).month groupeddata = df.groupby(['products','month'])['sales'].sum().unstack() groupeddata.plot(kind='bar') plt.xlabel('Products') plt.ylabel('sales') plt.title('Sales by Month') plt.legend(title='Morth',labels=['JAN','FEB','MAR','APR','NAV','JoW','JUL','AUG','SEP','OCT','NOV','DEV']) plt.show() def plot_sales_by_quarter(df) : df['quarter'] = pd.PeriodIndex(df['date'],freq='Q') groupeddata = df.groupby(['products','quarter'])['sales'].sum().unstack() groupeddata.plot(kind='pie',subplots=True) plt.title('Sales by Quarter') plt.legend(loc='center Left',bbox_to_anchor=(1.0,0.5)) plt.show() df = generate_data() plot_sales_by_product(df) plot_sales_by_month(df) plot_sales_by_quarter(df)
时间: 2023-08-22 09:06:40 浏览: 250
代码中的错误在于函数 `plot_sales_by_quarter` 中的 `groupeddata.plot(kind='pie',subplots=True)`,应该将 `kind` 参数的值改为 `'bar'`,才能按季度绘制柱状图。
修改后的代码如下:
```
import random
import pandas as pd
import matplotlib.pyplot as plt
def generate_data():
products = ['商品1', '商品2', '商品3', '商品4', '商品5', '商品6', '商品7', '商品8', '商品9', '商品10']
datelist = []
for month in range(1, 13):
for day in range(1, 29):
date = f'2019-{month:02d}-{day:02d}'
datelist.append(date)
datalist = []
for date in datelist:
for it in products:
sales = round(random.uniform(150, 200), 2)
datalist.append([date, it, sales])
df = pd.DataFrame(datalist, columns=['date', 'products', 'sales'])
df.to_csv('data.csv', index=False)
return pd.read_csv('data.csv')
def plot_sales_by_product(df):
for product in df['products'].unique():
data = df.loc[df['products'] == product]
plt.plot(data['date'], data['sales'], label=product)
plt.xlabel('Date')
plt.ylabel('Sales')
plt.title('Sales by Product')
plt.legend()
plt.show()
def plot_sales_by_month(df):
df['month'] = pd.DatetimeIndex(df['date']).month
groupeddata = df.groupby(['products', 'month'])['sales'].sum().unstack()
groupeddata.plot(kind='bar')
plt.xlabel('Products')
plt.ylabel('Sales')
plt.title('Sales by Month')
plt.legend(title='Month', labels=['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'])
plt.show()
def plot_sales_by_quarter(df):
df['quarter'] = pd.PeriodIndex(df['date'], freq='Q')
groupeddata = df.groupby(['products', 'quarter'])['sales'].sum().unstack()
groupeddata.plot(kind='bar')
plt.title('Sales by Quarter')
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.show()
df = generate_data()
plot_sales_by_product(df)
plot_sales_by_month(df)
plot_sales_by_quarter(df)
```
阅读全文