I have the following dataset :
{'PRODUCTLINE': {0: 'Classic Cars',
1: 'Classic Cars',
2: 'Classic Cars',
3: 'Classic Cars',
4: 'Classic Cars',
5: 'Classic Cars',
6: 'Classic Cars',
7: 'Classic Cars',
8: 'Classic Cars',
9: 'Classic Cars',
10: 'Classic Cars',
11: 'Classic Cars',
12: 'Classic Cars',
13: 'Classic Cars',
14: 'Classic Cars',
15: 'Classic Cars',
16: 'Classic Cars',
17: 'Classic Cars',
18: 'Classic Cars',
19: 'Motorcycles',
20: 'Motorcycles',
21: 'Motorcycles',
22: 'Motorcycles',
23: 'Motorcycles',
24: 'Motorcycles',
25: 'Motorcycles',
26: 'Motorcycles',
27: 'Motorcycles',
28: 'Motorcycles',
29: 'Motorcycles',
30: 'Motorcycles',
31: 'Motorcycles',
32: 'Motorcycles',
33: 'Motorcycles',
34: 'Motorcycles',
35: 'Planes',
36: 'Planes',
37: 'Planes',
38: 'Planes',
39: 'Planes',
40: 'Planes',
41: 'Planes',
42: 'Planes',
43: 'Planes',
44: 'Planes',
45: 'Planes',
46: 'Planes',
47: 'Planes',
48: 'Planes',
49: 'Planes',
50: 'Planes',
51: 'Planes',
52: 'Ships',
53: 'Ships',
54: 'Ships',
55: 'Ships',
56: 'Ships',
57: 'Ships',
58: 'Ships',
59: 'Ships',
60: 'Ships',
61: 'Ships',
62: 'Ships',
63: 'Ships',
64: 'Ships',
65: 'Ships',
66: 'Ships',
67: 'Trains',
68: 'Trains',
69: 'Trains',
70: 'Trains',
71: 'Trains',
72: 'Trains',
73: 'Trains',
74: 'Trains',
75: 'Trains',
76: 'Trains',
77: 'Trains',
78: 'Trains',
79: 'Trains',
80: 'Trains',
81: 'Trains',
82: 'Trucks and Buses',
83: 'Trucks and Buses',
84: 'Trucks and Buses',
85: 'Trucks and Buses',
86: 'Trucks and Buses',
87: 'Trucks and Buses',
88: 'Trucks and Buses',
89: 'Trucks and Buses',
90: 'Trucks and Buses',
91: 'Trucks and Buses',
92: 'Trucks and Buses',
93: 'Trucks and Buses',
94: 'Trucks and Buses',
95: 'Trucks and Buses',
96: 'Trucks and Buses',
97: 'Trucks and Buses',
98: 'Vintage Cars',
99: 'Vintage Cars',
100: 'Vintage Cars',
101: 'Vintage Cars',
102: 'Vintage Cars',
103: 'Vintage Cars',
104: 'Vintage Cars',
105: 'Vintage Cars',
106: 'Vintage Cars',
107: 'Vintage Cars',
108: 'Vintage Cars',
109: 'Vintage Cars',
110: 'Vintage Cars',
111: 'Vintage Cars',
112: 'Vintage Cars',
113: 'Vintage Cars',
114: 'Vintage Cars',
115: 'Vintage Cars'},
'COUNTRY': {0: 'Australia',
1: 'Austria',
2: 'Belgium',
3: 'Canada',
4: 'Denmark',
5: 'Finland',
6: 'France',
7: 'Germany',
8: 'Ireland',
9: 'Italy',
10: 'Japan',
11: 'Norway',
12: 'Philippines',
13: 'Singapore',
14: 'Spain',
15: 'Sweden',
16: 'Switzerland',
17: 'UK',
18: 'USA',
19: 'Australia',
20: 'Austria',
21: 'Canada',
22: 'Finland',
23: 'France',
24: 'Germany',
25: 'Ireland',
26: 'Italy',
27: 'Japan',
28: 'Norway',
29: 'Philippines',
30: 'Singapore',
31: 'Spain',
32: 'Sweden',
33: 'UK',
34: 'USA',
35: 'Australia',
36: 'Austria',
37: 'Belgium',
38: 'Canada',
39: 'Denmark',
40: 'Finland',
41: 'France',
42: 'Germany',
43: 'Ireland',
44: 'Italy',
45: 'Japan',
46: 'Norway',
47: 'Philippines',
48: 'Spain',
49: 'Sweden',
50: 'UK',
51: 'USA',
52: 'Australia',
53: 'Austria',
54: 'Belgium',
55: 'Canada',
56: 'Denmark',
57: 'Finland',
58: 'France',
59: 'Germany',
60: 'Italy',
61: 'Japan',
62: 'Singapore',
63: 'Spain',
64: 'Sweden',
65: 'UK',
66: 'USA',
67: 'Australia',
68: 'Belgium',
69: 'Denmark',
70: 'Finland',
71: 'France',
72: 'Germany',
73: 'Ireland',
74: 'Italy',
75: 'Japan',
76: 'Norway',
77: 'Singapore',
78: 'Spain',
79: 'Sweden',
80: 'UK',
81: 'USA',
82: 'Australia',
83: 'Austria',
84: 'Canada',
85: 'Denmark',
86: 'Finland',
87: 'France',
88: 'Germany',
89: 'Ireland',
90: 'Italy',
91: 'Japan',
92: 'Norway',
93: 'Singapore',
94: 'Spain',
95: 'Sweden',
96: 'UK',
97: 'USA',
98: 'Australia',
99: 'Austria',
100: 'Belgium',
101: 'Canada',
102: 'Denmark',
103: 'Finland',
104: 'France',
105: 'Germany',
106: 'Ireland',
107: 'Italy',
108: 'Japan',
109: 'Norway',
110: 'Philippines',
111: 'Singapore',
112: 'Spain',
113: 'Sweden',
114: 'UK',
115: 'USA'},
'QUANTITYORDERED': {0: 1818,
1: 937,
2: 147,
3: 456,
4: 1244,
5: 1284,
6: 3540,
7: 1281,
8: 202,
9: 948,
10: 314,
11: 1158,
12: 478,
13: 1043,
14: 4380,
15: 552,
16: 1078,
17: 1507,
18: 11625,
19: 876,
20: 197,
21: 41,
22: 447,
23: 2404,
24: 121,
25: 58,
26: 77,
27: 309,
28: 484,
29: 241,
30: 44,
31: 780,
32: 133,
33: 371,
34: 5080,
35: 813,
36: 200,
37: 41,
38: 317,
39: 70,
40: 421,
41: 1136,
42: 245,
43: 115,
44: 1122,
45: 547,
46: 325,
47: 215,
48: 1101,
49: 104,
50: 479,
51: 3476,
52: 56,
53: 113,
54: 343,
55: 486,
56: 436,
57: 315,
58: 766,
59: 55,
60: 194,
61: 208,
62: 174,
63: 1388,
64: 367,
65: 831,
66: 2395,
67: 33,
68: 97,
69: 134,
70: 89,
71: 222,
72: 89,
73: 50,
74: 82,
75: 49,
76: 72,
77: 174,
78: 509,
79: 32,
80: 168,
81: 912,
82: 705,
83: 203,
84: 517,
85: 73,
86: 384,
87: 1067,
88: 81,
89: 37,
90: 47,
91: 102,
92: 308,
93: 888,
94: 1709,
95: 433,
96: 291,
97: 3932,
98: 1945,
99: 324,
100: 446,
101: 476,
102: 240,
103: 252,
104: 1955,
105: 276,
106: 28,
107: 1303,
108: 313,
109: 495,
110: 27,
111: 437,
112: 2562,
113: 385,
114: 1366,
115: 8239},
'SALES': {0: 193085.5400000001,
1: 101459.47,
2: 20136.960000000003,
3: 61623.219999999994,
4: 157182.48000000004,
5: 153552.24000000002,
6: 388951.2000000002,
7: 148314.99999999997,
8: 31688.82,
9: 128576.65,
10: 47271.49,
11: 134787.36999999997,
12: 53112.090000000004,
13: 132890.44,
14: 476165.1499999998,
15: 69088.06000000001,
16: 117713.55999999998,
17: 159377.69999999998,
18: 1344638.2199999993,
19: 89968.76,
20: 26047.66,
21: 4177.49,
22: 47866.72,
23: 226390.30999999997,
24: 7497.500000000001,
25: 4953.200000000001,
26: 7567.8,
27: 26536.41,
28: 51768.63,
29: 18061.68,
30: 4175.6,
31: 74634.82000000002,
32: 15567.25,
33: 40802.810000000005,
34: 520371.70000000024,
35: 74853.87000000001,
36: 17860.44,
37: 5624.79,
38: 25510.07,
39: 7586.45,
40: 34375.130000000005,
41: 108155.51000000002,
42: 23001.26,
43: 11784.36,
44: 98185.65000000001,
45: 49176.96000000001,
46: 29500.7,
47: 20906.87,
48: 89985.51,
49: 8899.6,
50: 41163.51,
51: 328432.88999999996,
52: 4159.76,
53: 9024.73,
54: 31708.010000000002,
55: 40309.01,
56: 38697.259999999995,
57: 29808.440000000002,
58: 66486.67,
59: 5501.0,
60: 17703.54,
61: 18860.02,
62: 14155.519999999999,
63: 124459.96999999997,
64: 30915.89,
65: 72959.17000000001,
66: 209688.13999999998,
67: 1681.35,
68: 9017.26,
69: 11476.330000000002,
70: 5117.05,
71: 27340.8,
72: 5043.42,
73: 3112.6,
74: 6274.959999999999,
75: 3523.67,
76: 11310.36,
77: 13278.71,
78: 43370.17999999999,
79: 3807.68,
80: 12635.539999999999,
81: 69253.56,
82: 77318.49999999999,
83: 20472.75,
84: 51945.98,
85: 9588.82,
86: 40479.329999999994,
87: 116982.22000000003,
88: 10178.0,
89: 3983.05,
90: 5914.969999999999,
91: 13349.44,
92: 37075.64,
93: 89027.68000000002,
94: 177556.78000000003,
95: 47931.270000000004,
96: 28142.989999999998,
97: 397842.4200000002,
98: 189555.32000000004,
99: 27197.480000000003,
100: 41925.6,
101: 40512.79,
102: 21105.81,
103: 18383.0,
104: 176609.81,
105: 20935.91,
106: 2234.4,
107: 110450.74000000003,
108: 29449.82,
109: 43021.0,
110: 1935.09,
111: 34960.46,
112: 229514.51,
113: 33804.45999999999,
114: 123798.73999999999,
115: 757755.9}}
I want to visualize the frequency of each category ordered in a country.
For example, in USA we ordered "X" amount of Products and "y" of them were Classic Cars and so on. So far, I only reached that it shows the overall frequency of each productline. How can I integrate the country?
sales_by_categorie = df_clean.groupby(['PRODUCTLINE', "COUNTRY"])[['QUANTITYORDERED', 'SALES']].sum().reset_index()
bar_data1 = go.Bar(
x = sales_by_categorie['PRODUCTLINE'],
y = sales_by_categorie['QUANTITYORDERED'],
name = 'Quantity Ordered',
text = sales_by_categorie['QUANTITYORDERED'],
texttemplate = '%{text:.2s}',
textposition = 'inside',
yaxis = 'y1',
offsetgroup=1,
)
figure2 = go.Figure(bar_data1 )
figure2.show()
CodePudding user response:
IIUC, try:
import matplotlib.pylot as plt
output = df_clean.pivot_table("QUANTITYORDERED","PRODUCTLINE","COUNTRY","sum")
f = plt.figure()
output.plot.bar(stacked=True, ax=f.gca())
plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
If you instead want country on the X-axis and the breakdown of products on the Y-axis, you can do:
f = plt.figure()
output.T.plot.bar(stacked=True, ax=f.gca())
plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))