analyze_mendian.py
5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import datetime
import pymysql
import pandas as pd
# con = pymysql.connect(host="192.168.5.20",
# port=3306,
# user="biz_glsx_data",
# password="unEKczDc",
# db="glsx_biz_data",
# charset='utf8')
import pandas as pd
from sqlalchemy import create_engine
import matplotlib.pyplot as plt
from matplotlib import font_manager
import matplotlib
from sqlalchemy.orm import sessionmaker
font_path = "C:\\Windows\\Fonts\\simhei.ttf"
my_font = font_manager.FontProperties(fname=font_path, size=15)
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
# 用sqlalchemy构建数据库链接engine
db_info = {'user': 'biz_glsx_data',
'password': 'unEKczDc',
'host': '192.168.5.20',
'database': 'glsx_biz_data' # 这里我们事先指定了数据库,后续操作只需要表即可
}
engine = create_engine('mysql+pymysql://%(user)s:%(password)s@%(host)s/%(database)s?charset=utf8' % db_info,
encoding='utf-8') # 这里直接使用pymysql连接,echo=True,会显示在加载数据库所执行的SQL语句。
# sql 连接
DBsession = sessionmaker(bind=engine)
session = DBsession()
def mendianzhucequsi():
sql = '''
select DATE_FORMAT(created_date, '%%Y-%%m') as createdDate, count(1) as num
from dj_system_merchant
where DATE_FORMAT(created_date, '%%Y-%%m') >= '2020-01'
and DATE_FORMAT(created_date, '%%Y-%%m') <= '2022-12'
and deleted_flag = 'N'
group by DATE_FORMAT(created_date, '%%Y-%%m')
order by DATE_FORMAT(created_date, '%%Y-%%m') ;
'''
# 读取sql
data_df = pd.read_sql(sql, engine)
print(data_df.info())
print(data_df.head())
x = data_df['createdDate'].values
y = data_df['num'].values
# 设置图片大小
plt.figure(figsize=(15, 8), dpi=80)
# 设置标题
plt.title("门店注册趋势图", fontproperties=my_font)
# 设置X周与Y周的标题
plt.xticks(rotation=30)
plt.xlabel("创建时间(月份)", fontproperties=my_font)
plt.ylabel("注册数量(个)", fontproperties=my_font)
# 显示数据标签
for a, b in zip(x, y):
plt.text(a, b,
b,
ha='center',
va='bottom',
)
# 绘图
plt.bar(x, y, color="m", width=0.5)
plt.savefig('./data/门店注册趋势.png')
plt.show()
# 存储
# data_sql.to_csv("test.csv")
def mendianquyufenbu():
sql = '''
select province_name as provinceName, count(1) as num
from dj_system_merchant
where DATE_FORMAT(created_date, '%%Y-%%m') >= '2018-01' and province_name is not null and deleted_flag = 'N'
group by province_name
order by num desc;
'''
# 读取sql
data_df = pd.read_sql(sql, engine)
print(data_df.info())
print(data_df.head())
x = data_df['provinceName'].values
print(x)
y = data_df['num'].values
# 设置图片大小
plt.figure(figsize=(15, 8), dpi=80)
# 设置标题
plt.title("门店区域分布图", fontproperties=my_font)
# 设置X周与Y周的标题
# plt.xticks(rotation=30)
plt.xlabel("省份区域", fontproperties=my_font)
plt.ylabel("数量(个)", fontproperties=my_font)
# 显示数据标签
for a, b in zip(x, y):
plt.text(a, b,
b,
ha='center',
va='bottom',
)
# 绘图
plt.bar(x, y, color="m", width=0.5)
plt.savefig('./data/门店区域分布.png')
plt.show()
# 存储
# data_sql.to_csv("test.csv")
def mendianhuoyuequsi():
x = []
y = []
month_list = get_month_range(datetime.date(2020, 1, 1), datetime.date(2022, 12, 31))
for month in month_list:
print(month)
sql = f'''
SELECT count(DISTINCT active_merchant) as num from dj_2021_settle_details where active_merchant <> '' and
((DATE_FORMAT(active_time, '%Y-%m') = '{month}' and DATE_FORMAT(DATE_ADD(active_time,INTERVAL pk_period MONTH),'%Y-%m') > '{month}') or
DATE_FORMAT(active_time, '%Y-%m') = '{month}');
'''
print(sql)
result = session.execute(sql)
data = result.fetchone()[0]
print(data)
x.append(month)
y.append(data)
print(x)
print(y)
# 设置图片大小
plt.figure(figsize=(15, 8), dpi=80)
# 设置标题
plt.title("门店活跃趋势图", fontproperties=my_font)
# 设置X周与Y周的标题
plt.xticks(rotation=30)
plt.xlabel("月份", fontproperties=my_font)
plt.ylabel("数量(个)", fontproperties=my_font)
# 显示数据标签
for a, b in zip(x, y):
plt.text(a, b,
b,
ha='center',
va='bottom',
)
# 绘图
plt.bar(x, y, color="m", width=0.5)
plt.savefig('./data/门店活跃趋势.png')
plt.show()
# 存储
# data_sql.to_csv("test.csv")
def get_month_range(start_day, end_day):
months = (end_day.year - start_day.year) * 12 + end_day.month - start_day.month
month_range = ['%s-%s' % (start_day.year + mon // 12, format_month(mon % 12 + 1))
for mon in range(start_day.month - 1, start_day.month + months)]
return month_range
def format_month(num):
if num < 10:
return '0' + str(num)
else:
return str(num)
if __name__ == '__main__':
# 门店注册趋势
mendianzhucequsi()
# 门店区域分布
mendianquyufenbu()
# 门店活跃趋势
mendianhuoyuequsi()