Gantt1.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. """Main module."""
  3. from collections import OrderedDict
  4. import matplotlib.dates as m_dates
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. import pandas as pd
  8. def gantt(task=None, start=None, finish=None, **kwargs):
  9. """ Plot a gantt chart.
  10. """
  11. if 'task_type' in kwargs:
  12. task_type = kwargs['task_type']
  13. if 'color' in kwargs:
  14. color = kwargs['color']
  15. USES_DATES = False
  16. if np.issubdtype(start.dtype, np.datetime64):
  17. start = m_dates.date2num(start)
  18. USES_DATES = True
  19. if np.issubdtype(finish.dtype, np.datetime64):
  20. finish = m_dates.date2num(finish)
  21. delta = finish - start
  22. ax = plt.gca()
  23. labels = []
  24. # TODO: refactor?
  25. encoded_tasks = OrderedDict()
  26. k = 0
  27. for n in task:
  28. if not n in encoded_tasks:
  29. encoded_tasks[n] = k
  30. k += 1
  31. labels = list(encoded_tasks)
  32. for i, task in enumerate(task):
  33. j = encoded_tasks[task]
  34. if color:
  35. c = color[task_type[i]]
  36. else:
  37. c = None
  38. ax.broken_barh([(start[i], delta[i])], (j - 0.4, 0.8), color=c)
  39. # Set yticks
  40. ax.set_yticks(range(len(labels)))
  41. ax.set_yticklabels(labels)
  42. # Set xticks formatting
  43. # TODO: use matplotlib.dates.AutoDateFormatter
  44. if USES_DATES:
  45. ax.xaxis.set_major_formatter(m_dates.DateFormatter('%Y-%m-%d %H:%M'))
  46. fig = plt.gcf()
  47. fig.autofmt_xdate()
  48. ax.invert_yaxis()
  49. def my_gantt(show_pic=False):
  50. _data = pd.read_csv('Bin\\Data\\Result.csv')[['FLIGHTNO', 'DES_REAL_LANDING', 'DEP_REAL_TAKEOFF', 'Seat']]
  51. _data['DES_REAL_LANDING'] = pd.to_datetime(_data['DES_REAL_LANDING'])
  52. _data['DEP_REAL_TAKEOFF'] = pd.to_datetime(_data['DEP_REAL_TAKEOFF'])
  53. _data['Type'] = "main"
  54. _data.iloc[1::2, -1] = "sub"
  55. _data = _data.sort_values('Seat')
  56. fig = plt.figure(figsize=(30, 20))
  57. gantt(task=_data['Seat'], start=_data['DES_REAL_LANDING'],
  58. finish=_data['DEP_REAL_TAKEOFF'], task_type=_data['Type'],
  59. color={"main": "steelblue", "sub": "lightgreen"})
  60. plt.title('Gantt', {'fontsize': 14, 'fontweight': 'heavy'})
  61. if show_pic:
  62. plt.show()
  63. fig.savefig('Bin\\Data\\Gantt.png')