常用折线图
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('/Users/fanchengwen/Downloads/xxx副本.csv')
data.head()
# 筛选
data = data[(data['a.product'] == 'NEBULA') & (data['is_aim_user'] == 1)]
# 将日期列转为 datetime 格式
data['a.p_date'] = pd.to_datetime(data['a.p_date'], format='%Y%m%d').astype(str)
data.head()
exps = [exp for exp in data['group_name'].unique() if (exp == 'base1' or exp == 'base2' or exp == 'base3' or exp == 'exp13' )]
exps
各组的线不同颜色
import matplotlib.pyplot as plt
# 你要对比的指标列表
metrics = ['show_cnt', 'show_user_cnt', 'valid_play_cnt',
'play_duration', 'follow_author_cnt', 'comment_cnt',
'share_success_cnt', 'like_cnt', 'send_gift_cnt']
#,'report_live_cnt', 'report_user_cnt', 'dislike_user_cnt']
# 获取唯一的实验组(exp)
exps = [exp for exp in data['group_name'].unique() if (exp == 'base1' or exp == 'base2' or exp == 'base3' or exp == 'exp13')]
# 设置画布大小
plt.figure(figsize=(32, 16))
# 为每个指标分别画图
for i, metric in enumerate(metrics, 1):
plt.subplot(3, 3, i) # 3行4列的子图
for exp in exps:
df_exp = data[data['group_name'] == exp]
df_exp = df_exp.sort_values('a.p_date')
# 绘制折线
plt.plot(df_exp['a.p_date'], df_exp[metric], label=exp, marker='o')
# 在每个点上添加文本标记
for x, y in zip(df_exp['a.p_date'], df_exp[metric]):
plt.text(x, y, str(y), fontsize=9, ha='right')
plt.title(f'{metric}')
plt.xlabel('Date')
plt.ylabel(metric)
plt.xticks(rotation=45)
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
突出某一条线的颜色 调字号
# 正反馈指标
metrics = ['show_cnt', 'show_user_cnt',
'inside_play_cnt','inside_more_60s_play_cnt', 'play_duration',
'follow_author_cnt', 'comment_cnt','share_success_cnt', 'like_cnt', 'send_gift_cnt']
# 获取唯一的实验组(exp)
exps = [exp for exp in data['group_name'].unique() if (exp == 'base1' or exp == 'base2' or exp == 'base3' or exp == 'exp13')]
exps = ['exp13', 'base1', 'base2', 'base3']
# 设置画布大小
plt.figure(figsize=(20, 16))
# 为每个指标分别画图
for i, metric in enumerate(metrics, 1):
plt.subplot(3, 4, i) # 3行3列的子图
for exp in exps:
df_exp = data[data['group_name'] == exp]
df_exp = df_exp.sort_values('a.p_date')
# 判断是否为 exp13,并设置不同的样式
if exp == 'exp13':
plt.plot(df_exp['a.p_date'], df_exp[metric], label=exp, marker='o', color='red', linewidth=2, markersize=8)
else:
plt.plot(df_exp['a.p_date'], df_exp[metric], label=exp, marker='o', color='blue')
# 在每个点上添加文本标记
for x, y in zip(df_exp['a.p_date'], df_exp[metric]):
plt.text(x, y, str(y), fontsize=12, ha='right')
plt.title(f'{metric}', fontsize=20)
plt.xlabel('Date')
plt.ylabel(metric)
plt.xticks(rotation=45)
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig('后缀极速染色正反馈变化0529之后.png', dpi=300, bbox_inches='tight')
plt.show()
1. 折线图 plt.plot(x, y)
import matplotlib.pyplot as plt
date = ['2023-09-01', '2023-09-02', '2023-09-03', '2023-09-04', '2023-09-05', '2023-09-06', '2023-09-07', '2023-09-08', '2023-09-09', '2023-09-10', '2023-09-11', '2023-09-12', '2023-09-13', '2023-09-14', '2023-09-15', '2023-09-16', '2023-09-17', '2023-09-18', '2023-09-19', '2023-09-20', '2023-09-21', '2023-09-22', '2023-09-23', '2023-09-24', '2023-09-25', '2023-09-26', '2023-09-27', '2023-09-28', '2023-09-29', '2023-09-30', '2023-10-01', '2023-10-02', '2023-10-03', '2023-10-04', '2023-10-05', '2023-10-06', '2023-10-07', '2023-10-08', '2023-10-09', '2023-10-10', '2023-10-11', '2023-10-12', '2023-10-13', '2023-10-14', '2023-10-15', '2023-10-16', '2023-10-17', '2023-10-18', '2023-10-19', '2023-10-20', '2023-10-21', '2023-10-22', '2023-10-23', '2023-10-24', '2023-10-25', '2023-10-26', '2023-10-27', '2023-10-28', '2023-10-29', '2023-10-30', '2023-10-31']
nums = [1, 1, 1, 0, 1156, 17435, 12722, 10679, 10094, 7885, 12203, 17173, 11239, 11233, 10890, 8628, 8189, 16502, 19689, 14063, 10757, 11643, 10193, 8538, 8328, 8768, 7567, 1201, 92, 84, 90, 74, 100, 77, 46, 60, 41, 37, 52, 38, 32, 39, 35, 32, 30, 50, 35, 70, 163, 163, 124, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
plt.xlabel('date')
plt.ylabel('nums')
plt.title('date-nums')
plt.plot(date, nums)
# 多个折线,多次plt.plot(date, y)
plt.legend() # 显示标签
plt.show()
2. 柱状图
import matplotlib.pyplot as plt
y = [5, 10, 20, 15, 25]
x = ['math', 'language', 'english', 'sports', 'politics']
plt.title("plt.title()")
plt.bar(x, y, color=['r', 'b', 'y'], width=0.5)
plt.show()