function f = plot_results(trace, P)

ts = [];
car_vs = [];
car_xs = [];
car_yaws = [];
car_eys = [];
car_accs = [];
car_steering_angles = [];

at_stop = [];
after_stop = [];
time_stopped_at_stop = [];

action_ids = [];

modes = [];

N = length(P.cars);

for i=1:length(trace)
  car_states = trace(i).car_states;
  car_controls = trace(i).car_controls;

  ts = [ts, car_states(1).t];
  
  car_vs = [car_vs, [car_states.v]'];
  car_yaws = [car_yaws, [car_states.yaw]'];
  car_eys = [car_eys, [car_states.ey]'];

  if isempty(car_controls)
    car_accs = [car_accs, nan(N,1)];
    car_steering_angles = [car_steering_angles, nan(N,1)];
    modes = [modes, nan(N,1)];
    actions = [nan(N,1)];
    action_ids = [action_ids, nan(N,1)];
  else
    car_accs = [car_accs, [car_controls.acc]'];
    car_steering_angles = [car_steering_angles, [car_controls.steering_angle]'];
    modes = [modes, [car_controls.mode]'];
    actions = [car_controls.action];
    action_ids = [action_ids, [actions.id]'];
  end
    
  time_stopped_at_stop = [time_stopped_at_stop, [car_states.time_stopped_at_stop]'];
  at_stop = [at_stop, [car_states.at_stop]'];
  after_stop = [after_stop, [car_states.after_stop]'];

  
  intersections = P.roads([car_states.road_id]).intersection;
  car_xs = [car_xs, [car_states.x]'];
    
end

metrics = 9;

for i=1:length(P.cars),
  subplot(length(P.cars), metrics, (i - 1)*metrics + 1)

  plot(ts, car_accs(i,:))
  xlabel('sec.')
  ylabel('m/s^2')
  legend('acc')
  title(P.cars(i).name)

  subplot(length(P.cars), metrics, (i - 1)*metrics + 2)

  plot(ts, car_vs(i,:), ...
       ts, P.cars(i).v_nominal*ones(size(ts)), ...
       ts, P.cars(i).goal_state.v*ones(size(ts)));
  xlabel('sec.')
  ylabel('m/s')
  legend('v', 'v nominal', 'v final')
  title(P.cars(i).name)
  
  subplot(length(P.cars), metrics, (i - 1)*metrics + 3)

  plot(ts, car_xs(i,:));
  xlabel('sec.')
  ylabel('m')
  legend('s')
  title(P.cars(i).name)
  
  subplot(length(P.cars), metrics, (i - 1)*metrics + 4)

  plot(ts, car_eys(i,:));
  xlabel('sec.')
  ylabel('m')
  legend('ey')
  title(P.cars(i).name)

  subplot(length(P.cars), metrics, (i - 1)*metrics + 5)

  plot(ts, car_yaws(i,:));
  xlabel('sec.')
  ylabel('rad')
  legend('yaw')
  title(P.cars(i).name)

  subplot(length(P.cars), metrics, (i - 1)*metrics + 6)
  
  plot(ts, car_steering_angles(i,:))
  xlabel('sec.')
  ylabel('rad')
  legend('steering_angle')
  title(P.cars(i).name)

  subplot(length(P.cars), metrics, (i - 1)*metrics + 7)
  
  plot(ts, at_stop(i,:), 'o-', ...
       ts, after_stop(i,:), 'v-',...
       ts, ~isnan(time_stopped_at_stop(i,:)), '^-');
  legend('at stop', 'after stop', 'time stopped at stop')
  title(P.cars(i).name)
  set(gca, 'YTick', [0, 1]);
  set(gca, 'YTickLabel', {'False', 'True'});
  set(gca,'TickLabelInterpreter','none')

  subplot(length(P.cars), metrics, (i - 1)*metrics + 8)
  plot(ts, action_ids(i,:), 'o-');
  legend('actions')
  title(P.cars(i).name)
  set(gca, 'YTick', P.action_ids);
  set(gca, 'YTickLabel', P.action_labels);
  set(gca,'TickLabelInterpreter','none')

  
  subplot(length(P.cars), metrics, (i - 1)*metrics + 9)
  plot(ts, modes(i,:), 'o-');
  legend('mode')
  title(P.cars(i).name)
  set(gca, 'YTick', P.mode_ids);
  set(gca, 'YTickLabel', P.mode_labels);
  set(gca,'TickLabelInterpreter','none')
   
end

% set(gcf,'Position',[0 0 1600 800]);
