Skip to content

Instantly share code, notes, and snippets.

@TooTouch
Last active December 9, 2020 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TooTouch/cdeedf1b302882c397668edc43f77d1a to your computer and use it in GitHub Desktop.
Save TooTouch/cdeedf1b302882c397668edc43f77d1a to your computer and use it in GitHub Desktop.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from math import pi
import matplotlib as mpl
plt.rcParams["font.family"] = 'NanumGothicCoding'
mpl.rcParams['axes.unicode_minus'] = False
def cat_card_by_cluster_plot(cluster: int, data, save=False):
plt.figure(figsize=(10,10))
ax = plt.subplot(polar=True)
# set categories
categories = data.category.unique().tolist()
N = len(categories)
# set color
color=['red','blue','orange','green','purple']
# calculate values
for i in range(5):
df2019 = data[data['t'] <= '2019-06-30']
df2020 = data[('2020-01-01' <= data['t']) & (data['t'] <= '2020-06-30')]
df2019_card_mean = df2019[df2019.k == i].groupby('category')['card'].mean()
df2020_card_mean = df2020[df2020.k == i].groupby('category')['card'].mean()
values = (df2020_card_mean / df2019_card_mean).tolist()
values += values[:1]
# angles
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
if i == cluster:
plt.polar(angles, values, marker='.', linewidth=2, color=color[i])
plt.fill(angles, values, alpha=0.5, color=color[i])
else:
plt.polar(angles, values, marker='.', linewidth=1, linestyle='--', color=color[i], alpha=0.5)
plt.xticks(angles[:-1], categories)
ax.spines["polar"].set_visible(False)
yticks_values = [0.2, 0.4, 0.6, 0.8, 1]
plt.yticks(yticks_values, labels=['{0:.0%}'.format(i) for i in yticks_values],
color='grey', size=17)
plt.xticks(size=20)
plt.title('군집별 업종별 구매 건수 변화율', size=25)
plt.legend([f'군집{i}' for i in range(5)], fontsize=20, bbox_to_anchor=(1.45, 0.8))
plt.tight_layout()
if save:
plt.savefig('../images/sample/card_example.jpg', dpi=300)
plt.show()
categories = ['업종1','업종2','업종3','업종4','업종5','업종6']
sex_age = [f'{sex}_{age}' for sex in ['F','M'] for age in range(10,80,10)]
date = pd.date_range('2019-01-01','2020-06-30',freq='d')
sample_df = pd.DataFrame()
for sex_age_i in sex_age:
for cat in categories:
sample_i_df = pd.DataFrame({'t':date})
sample_i_df['sex_age'] = sex_age_i
sample_i_df['category'] = cat
sample_df = pd.concat([sample_df, sample_i_df], axis=0)
sample_df = sample_df.sample(frac=1, random_state=42)
np.random.seed(42)
sample_df['card'] = random_walks(n_ts=1, sz=len(sample_df), d=1)[0,:,0]
sample_df['card'] = sample_df['card'] - np.random.randint(low=1, high=100, size=len(sample_df))
sample_df['k'] = np.random.randint(low=0, high=5, size=len(sample_df))
cat_card_by_cluster_plot(cluster=3, data=sample_df, save=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment