0

How can I draw multiple plots for each group of data from the same dataframe?

My data is:

Fecha          IP        count
---------- --------------- -----
2017/06/03 115.159.241.160     1
2017/06/03 116.31.116.21    1268
2017/06/03 116.31.116.27     432
2017/06/03 122.4.172.199       1
2017/06/03 153.187.102.60      1
2017/06/03 171.8.75.134        1
2017/06/03 188.16.161.176      1
2017/06/03 190.179.128.206     1
2017/06/03 190.50.203.101      1
2017/06/03 193.201.224.215    31
2017/06/03 193.70.95.180       1
2017/06/03 201.179.182.64      1
2017/06/03 221.122.101.203     1
2017/06/03 221.229.60.6        1
2017/06/03 58.218.198.160    832
2017/06/03 58.218.198.161   1981
2017/06/03 60.190.98.50        1
2017/06/03 72.2.170.24         1
2017/06/03 77.53.124.27        1
2017/06/03 91.197.232.108     17
2017/06/03 91.197.232.11      31
2017/06/03 95.5.136.211        1
2017/06/04 114.113.234.66      1
2017/06/04 116.31.116.21     148
2017/06/04 116.31.116.27     975
2017/06/04 118.102.213.245     1
2017/06/04 122.232.8.113       1
2017/06/04 124.173.118.39      1
2017/06/04 174.90.226.4        1
2017/06/04 182.243.91.32       1
2017/06/04 185.70.28.137       3
2017/06/04 192.81.214.183      3
2017/06/04 193.70.86.63       42
2017/06/04 195.66.220.121      1
2017/06/04 202.4.110.75        3
2017/06/04 203.185.22.28       3
2017/06/04 218.104.207.53      1
2017/06/04 27.223.195.200      1
2017/06/04 31.44.116.54        1
2017/06/04 49.248.112.46       1
2017/06/04 58.218.198.160   2611
2017/06/04 58.218.198.161    262
2017/06/04 58.248.66.53        1
2017/06/04 58.68.23.70         1
2017/06/04 61.163.231.213      1
2017/06/04 83.219.73.102       2
2017/06/04 88.201.33.141       1
2017/06/04 89.166.222.253      1
2017/06/04 93.97.193.152       1

I am plotting all together data in a one graph:

fig 1

with this code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
from  matplotlib.dates import DateFormatter, DayLocator, AutoDateLocator, AutoDateFormatter

df = pd.read_csv("DaysCountIP.csv", delimiter=',', parse_dates = ['Fecha'])
grupos = df.groupby(['IP'])

print df

fig, ax = plt.subplots(figsize=(16,6.5))
plt.subplots_adjust(left=.06, right=None, top=.95, bottom=None)

color=iter(cm.rainbow(np.linspace(0,1,len(grupos))))

for nombre, grupo in grupos:
    ax.plot_date(grupo['Fecha'].values, 
                 grupo['count'].values, 
                 color=next(color), marker='o', ls='solid', label=nombre)
    for x, y in zip(grupo['Fecha'],grupo['count']):
        annotation_string = (nombre) 
        annotation_string += "\n"
        annotation_string += "%.0f" % (y)
        ax.annotate(annotation_string, xy=(x,y),
        xytext=(0,15),
        textcoords='offset points', size=6.5, ha='center', va='center',
        bbox=dict(boxstyle='round,pad=0.3', fc='white', alpha=None),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))

locator = DayLocator()
formatter = AutoDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.autoscale_view()
ax.grid(True)
fig.autofmt_xdate()
ax.margins(0.05)

box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(ncol=3, fontsize='x-small',loc='center left', title='IPs',  bbox_to_anchor=(1.04, 0.4))

plt.show()

I want to draw each group (nombre), each on different graph into the same canvas/figure.

Is this possible?

º----------------------------- UPDATE 13/06/2017 -----------------------------º

I am looking to graph a different graph into a loop for each group element in one figure (plot area), something like this: (the image has been generated in splunk):

figure 2

º----------------------------- UPDATE 14/06/2017 -----------------------------º

I tried this:

fig, ax = plt.subplots(nrows=18, ncols=2)

for row in ax:
    for col in row:
        for nombre, grupo in grupos:
            col.plot_date(grupo['Fecha'].values,
                         grupo['count'].values)

locator = DayLocator()
formatter = AutoDateFormatter(locator)
col.xaxis.set_major_locator(locator)
col.xaxis.set_major_formatter(formatter)
fig.autofmt_xdate()

But I can't isolate each group by graph, the code graph al data in every plot graph.

Error

Also rarely the date format is only taken for the second column of graphs.

My dataset is:

[In] type(df)
[Out] <class 'pandas.core.frame.DataFrame'>
Community
  • 1
  • 1

0 Answers0