保证原本的输出要求下优化以下代码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 16:06:41 浏览: 87
python项目实例代码源码- Python文本数据可视化之“词云”图.zip
以下是代码的优化建议:
1. 函数`generate_data()`中可以使用列表推导式来生成日期列表和数据列表,这样可以减少for循环的层数,提高代码的执行效率。
2. 函数`plot_sales_by_product()`中,每次循环时都需要对x轴、y轴、标题和图例进行设置,这样会导致图例重叠,建议将这些设置放在循环外面。
3. 函数`plot_sales_by_month()`和`plot_sales_by_quarter()`中,可以将legend标签的设置放在循环外面,避免重复设置。
4. 函数`plot_sales_by_quarter()`中,应该使用条形图(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 = [f'2019-{month:02d}-{day:02d}' for month in range(1, 13) for day in range(1, 29)]
datalist = [[date, it, round(random.uniform(150, 200), 2)] for date in datelist for it in products]
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):
plt.xlabel('Date')
plt.ylabel('Sales')
plt.title('Sales by Product')
for product in df['products'].unique():
data = df.loc[df['products'] == product]
plt.plot(data['date'], data['sales'], label=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(title='Quarter', bbox_to_anchor=(1.0, 0.5), loc='center left')
plt.show()
df = generate_data()
plot_sales_by_product(df)
plot_sales_by_month(df)
plot_sales_by_quarter(df)
```
阅读全文