Home > front end >  How to compare 3 days data grouped by hour in a bar chart?
How to compare 3 days data grouped by hour in a bar chart?

Time:05-04

Given the following data:

    total_traffic  day  hour
0             116    2     0
1             131    2     1
2              89    2     2
3             840    2     3
4             289    2     4
5             419    2     5
6            1003    2     6
7            1274    2     7
8             446    2     8
9             934    2     9
10           1476    2    10
11            808    2    11
12           1096    2    12
13           1257    2    13
14            635    2    14
15            650    2    15
16           2227    2    16
17           2574    2    17
18            636    2    18
19           3005    2    19
20           1587    2    20
21            230    3     0
22            476    3     1
23            376    3     2
24            291    3     3
25            505    3     4
26           1688    3     5
27           1638    3     6
28           1818    3     7
29            259    3     8
30            610    3     9
31           1328    3    10
32            954    3    11
33            708    3    12
34            396    3    13
35            527    3    14
36            759    3    15
37            492    3    16
38            704    3    17
39            371    3    18
40            386    3    19
41            270    3    20
42            208    4     0
43            159    4     1
44             94    4     2
45           1222    4     3
46            480    4     4
47            261    4     5
48            399    4     6
49            434    4     7
50            360    4     8
51            208    4     9
52            326    4    10
53           1565    4    11
54           2575    4    12
55           1193    4    13
56           1206    4    14
57            296    4    15
58            598    4    16
59            352    4    17
60           1358    4    18
61           2057    4    19
62           2269    4    20

I need to compare hourly total_traffic in 3 days (2, 3, 4) using a bar chart. I'm picturing 24 hours on x-axis, having 3 bars each (for each day) and total_traffic on y-axis or the other way around, doesn't matter. Any suggestions for better ways of comparing the 3 days through a plot is welcome. If this is too much to fit in one figure perhaps n hours could be compared (15, 10, 5, ...)

Here's the data above:

df = pd.DataFrame({'total_traffic': {0: 116,
  1: 131,
  2: 89,
  3: 840,
  4: 289,
  5: 419,
  6: 1003,
  7: 1274,
  8: 446,
  9: 934,
  10: 1476,
  11: 808,
  12: 1096,
  13: 1257,
  14: 635,
  15: 650,
  16: 2227,
  17: 2574,
  18: 636,
  19: 3005,
  20: 1587,
  21: 230,
  22: 476,
  23: 376,
  24: 291,
  25: 505,
  26: 1688,
  27: 1638,
  28: 1818,
  29: 259,
  30: 610,
  31: 1328,
  32: 954,
  33: 708,
  34: 396,
  35: 527,
  36: 759,
  37: 492,
  38: 704,
  39: 371,
  40: 386,
  41: 270,
  42: 208,
  43: 159,
  44: 94,
  45: 1222,
  46: 480,
  47: 261,
  48: 399,
  49: 434,
  50: 360,
  51: 208,
  52: 326,
  53: 1565,
  54: 2575,
  55: 1193,
  56: 1206,
  57: 296,
  58: 598,
  59: 352,
  60: 1358,
  61: 2057,
  62: 2269},
 'day': {0: 2,
  1: 2,
  2: 2,
  3: 2,
  4: 2,
  5: 2,
  6: 2,
  7: 2,
  8: 2,
  9: 2,
  10: 2,
  11: 2,
  12: 2,
  13: 2,
  14: 2,
  15: 2,
  16: 2,
  17: 2,
  18: 2,
  19: 2,
  20: 2,
  21: 3,
  22: 3,
  23: 3,
  24: 3,
  25: 3,
  26: 3,
  27: 3,
  28: 3,
  29: 3,
  30: 3,
  31: 3,
  32: 3,
  33: 3,
  34: 3,
  35: 3,
  36: 3,
  37: 3,
  38: 3,
  39: 3,
  40: 3,
  41: 3,
  42: 4,
  43: 4,
  44: 4,
  45: 4,
  46: 4,
  47: 4,
  48: 4,
  49: 4,
  50: 4,
  51: 4,
  52: 4,
  53: 4,
  54: 4,
  55: 4,
  56: 4,
  57: 4,
  58: 4,
  59: 4,
  60: 4,
  61: 4,
  62: 4},
 'hour': {0: 0,
  1: 1,
  2: 2,
  3: 3,
  4: 4,
  5: 5,
  6: 6,
  7: 7,
  8: 8,
  9: 9,
  10: 10,
  11: 11,
  12: 12,
  13: 13,
  14: 14,
  15: 15,
  16: 16,
  17: 17,
  18: 18,
  19: 19,
  20: 20,
  21: 0,
  22: 1,
  23: 2,
  24: 3,
  25: 4,
  26: 5,
  27: 6,
  28: 7,
  29: 8,
  30: 9,
  31: 10,
  32: 11,
  33: 12,
  34: 13,
  35: 14,
  36: 15,
  37: 16,
  38: 17,
  39: 18,
  40: 19,
  41: 20,
  42: 0,
  43: 1,
  44: 2,
  45: 3,
  46: 4,
  47: 5,
  48: 6,
  49: 7,
  50: 8,
  51: 9,
  52: 10,
  53: 11,
  54: 12,
  55: 13,
  56: 14,
  57: 15,
  58: 16,
  59: 17,
  60: 18,
  61: 19,
  62: 20}})

CodePudding user response:

Okay so as per our conversation I have two ways to do this, so pick your poison.

  1. Using seaborn -
import seaborn as sns

plt.figure(figsize = (20,5))
sns.barplot(data = df, x = "hour", y = "total_traffic", hue = "day")
plt.show()

enter image description here

  1. Using plotly -
import plotly.express as px

barplot = px.bar(data_frame = df, x = "hour", y = "total_traffic", color = "day")
barplot.show()

enter image description here

  • Related