1

I have the following data in a csv file

   SourceID    BSs   hour   Type
    7208       87     11    MAIN
    11060      67     11    MAIN
    3737       88     11    MAIN
    9683       69     11    MAIN

I have the following python code.I want to plot a graph with the following specifications.

For each SourceID and Type I want to plot a graph of BSs over time. I would prefer if each SourceID and Type is a subplot on single plot.I have tried a lot of options using groupby, but can't seem to get it work.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

COLLECTION = 'NEW'
DATA = r'C:\Analysis\Test\{}'.format(COLLECTION)
INPUT_FILE = DATA + r'\in.csv'
OUTPUT_FILE = DATA + r'\out.csv'


with open(INPUT_FILE) as fin:
    df = pd.read_csv(INPUT_FILE,
                  usecols=["SourceID", 'hour','BSs','Type'],
                  header=0)

    df.drop_duplicates(inplace=True)

    df.reset_index(inplace=True)
5
  • 2
    Could you show what you've tried so far? I don't see any plotting code here. Commented Nov 20, 2015 at 17:56
  • @ali_m - I have updated the plotting code.But it is giving some strange plots. Commented Nov 20, 2015 at 20:57
  • 1
    The IndexError was my fault - my code was setting nrows to be too small to accommodate all of the plots in cases where ngrps is not evenly divisible by ncols. This is because I was doing floor division, ngrps // ncols, rather than ceiling division, -(-ngrps // ncols). This is now fixed in my answer. Commented Nov 21, 2015 at 1:38
  • 1
    The warnings are nothing to worry about - they occur whenever there is only a single point to plot, and therefore when the axes are automatically scaled the lower and upper limits are set to the same value. This doesn't matter, since I'm manually setting the axis limits at the end. You could make the warnings go away by either setting the x-axis limits at the time of plotting (e.g. by passing the xlim= keyword argument to rows.plot(..)), or by preventing the axes from being autoscaled by passing scalex=False, scaley=False to rows.plot(). I've made the latter change to my answer. Commented Nov 21, 2015 at 1:41
  • @ali_m - even after setting scalex and scaley to false in rows.plot() still the warning persists.I am not too worried about it at the moment.Please see my comment below about the new error. thanks. Commented Nov 21, 2015 at 3:22

1 Answer 1

1

It's still not 100% clear to me what sort of plot you actually want, but my guess is that you're looking for something like this:

from matplotlib import pyplot as plt

# group by SourceID and Type, find out how many unique combinations there are
grps = df.groupby(['SourceID', 'Type'])
ngrps = len(grps)

# make a grid of axes
ncols = int(np.sqrt(ngrps))
nrows = -(-ngrps // ncols)

fig, ax = plt.subplots(nrows, ncols, sharex=True, sharey=True)

# iterate over the groups, plot into each axis
for ii, (idx, rows) in enumerate(grps):
    rows.plot(x='hour', y='BSs', style='-s', ax=ax.flat[ii], legend=False,
              scalex=False, scaley=False)

# hide any unused axes
for aa in ax.flat[ngrps:]:
    aa.set_axis_off()

# set the axis limits
ax.flat[0].set_xlim(df['hour'].min() - 1, df['hour'].max() + 1)
ax.flat[0].set_ylim(df['BSs'].min() - 5, df['BSs'].max() + 5)

fig.tight_layout()

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Yes this is exactly what I want. However If I use your code on my data set I am getting some warnings and errors.Please see details above.
I am not getting the hour values on x axis in bottom row.` I am getting only a single value of 13 for some reason.
I have a small hitch issue with this approach. The value ranges for MAIN BSs and SUB BSs and not in the same range. I see that you have set sharey=True. Can we output these two sets of graphs to to different plots?
I'm not quite sure what you mean by "output these two sets of graphs to to different plots". One simple option would be to set sharey=False and omit the set_ylim line at the end to allow each set of axes to scale its y-axes independently.
In that case, group by Type first, create a figure and subplots within this outer loop, then iterate over SourceIDs and fill the subplots.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.