0

I've written a python script that parses a trace file and retrieves a list of objects (vehicle objects) containing the vehicle id, timestep and the number of other vehicles in radio range of a particular vehicle for that timestep:

for d_obj in global_list_of_nbrs:
    print "\t", d_obj.id, "\t", d_obj.time, "\t", d_obj.num_nbrs

The sample output from the test file I am using is:

0   0   1
0   1   2
0   2   0
1   0   1
1   1   2
2   0   0
2   1   2

This can be interpreted as vehicle with id 0 at timestep 0 has 1 neighbouring vehicle, vehicle with id 0 at timestep 1 has 2 neighbouring vehicles (i.e. in radio range) etc.

I would like to plot a histogram using matplotlib to represent this data but am confused what I should do with bins etc and how I should represent the list (currently a list of objects).

Can anyone please advise on this?

Many thanks in advance.

2
  • do you want a histogram or just any appropiate visualization of the data? Commented Aug 18, 2013 at 0:05
  • Can you clarify what your question is? Do you want to look at a histogram of neighbor counts, across ID? As far as data representation goes, I would recommend using pandas, as it excels at this kind of data analysis. Commented Aug 19, 2013 at 16:06

1 Answer 1

3

Here's an example of something you might be able to do with this data set:

Note: You'll need to install pandas for this example to work for you.

n = 10000
id_col = randint(3, size=n)
lam = 10
num_nbrs = poisson(lam, size=n)

d = DataFrame({'id': id_col, 'num_nbrs': num_nbrs})

fig, axs = subplots(2, 3, figsize=(12, 4))

def plotter(ax, grp, grp_name, method, **kwargs):
    getattr(ax, method)(grp.num_nbrs.values, **kwargs)
    ax.set_title('ID: %i' % grp_name)

gb = d.groupby('id')

for row, method in zip((0, 1), ('plot', 'hist')):
    for ax, (grp_name, grp) in zip(axs[row].flat, gb):
        plotter(ax, grp, grp_name, method)

enter image description here

What I've done is created 2 plots for each of 3 IDs. The top row shows the number of neighbors as a function of time for each ID. The bottom row shows the distribution of the number of neighbors across time.

You'll probably want to play around with sharing axes, axes labelling and all the other fun things that matplotlib offers.

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

Comments

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.