Created
February 19, 2020 05:49
-
-
Save jjsantanna/af7682a583dc531b10669638aedda3ea to your computer and use it in GitHub Desktop.
IP timeseries matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user_name = '[email protected]' | |
################################################################################ | |
# Data Processing | |
import pandas as pd | |
dataset = df[df['UserIds']==user_name][['CreationDate','ClientIP']] | |
dataset['ClientIP'] = dataset['ClientIP'].replace('','0.0.0.0').replace('<null>','0.0.0.0') | |
dataset['ip'] = dataset['ClientIP'].astype(str).apply(lambda x: x.split(':')[0] if len(x.split('.'))==4 and len(x.split(':'))==2 else x) | |
dataset = dataset.set_index('CreationDate') | |
dataset['ip_id'] = dataset['ip'].astype('category').cat.codes | |
categories = dataset.groupby(['ip','ip_id']).size().reset_index() | |
categories.sort_values(by=0,ascending=False).head() | |
################################################################################ | |
# Plotting | |
import matplotlib.pyplot as plt | |
%matplotlib inline | |
plt.style.use('ggplot') | |
import matplotlib.dates as mdates | |
fig = plt.figure(figsize=(15, 5)) | |
ax = plt.subplot2grid((1,1), (0,0)) | |
dataset['ip_id'].plot(ax=ax, y='ip_id',style=".") | |
ax.set_yticklabels(categories['ip'], fontsize=8) | |
ax.set_yticks(np.arange(min(dataset['ip_id']), max(dataset['ip_id']+1), 1.0)) | |
ax.set_ylim(min(dataset['ip_id']), max(dataset['ip_id'])) | |
ax.xaxis.set_major_locator(mdates.DayLocator(interval = 1)) | |
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m')) | |
ax.tick_params(axis='x', rotation=90, labelsize=8) | |
plt.axvline(dt.datetime(2020, 2, 7),linestyle='--',color='r') | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment