This is a follow up question from this question.
The data:
df1 = pd.DataFrame.from_dict({('group', ''): {0: 'A',
1: 'A',
2: 'A',
3: 'A',
4: 'A',
5: 'A',
6: 'A',
7: 'A',
8: 'B',
9: 'B',
10: 'B',
11: 'B',
12: 'B',
13: 'B',
14: 'B',
15: 'B',
16: 'C',
17: 'C',
18: 'C',
19: 'C',
20: 'C',
21: 'C',
22: 'C',
23: 'C',
24: 'D',
25: 'D',
26: 'D',
27: 'D',
28: 'D',
29: 'D',
30: 'D'},
('category', ''): {0: 'Apple',
1: 'Amazon',
2: 'Google',
3: 'Netflix',
4: 'Facebook',
5: 'Uber',
6: 'Tesla',
7: 'total',
8: 'Apple',
9: 'Amazon',
10: 'Google',
11: 'Netflix',
12: 'Facebook',
13: 'Uber',
14: 'Tesla',
15: 'total',
16: 'Apple',
17: 'Amazon',
18: 'Google',
19: 'Netflix',
20: 'Facebook',
21: 'Uber',
22: 'Tesla',
23: 'total',
24: 'Apple',
25: 'Amazon',
26: 'Google',
27: 'Netflix',
28: 'Uber',
29: 'Tesla',
30: 'total'},
(pd.Timestamp('2021-06-28 00:00:00'), 'total_orders'): {0: 88.0,
1: 66.0,
2: 191.0,
3: 558.0,
4: 12.0,
5: 4.0,
6: 56.0,
7: 975.0,
8: 90.0,
9: 26.0,
10: 49.0,
11: 250.0,
12: 7.0,
13: 2.0,
14: 44.0,
15: 468.0,
16: 36.0,
17: 52.0,
18: 94.0,
19: 750.0,
20: 10.0,
21: 0.0,
22: 52.0,
23: 994.0,
24: 16.0,
25: 22.0,
26: 5.0,
27: 57.0,
28: 3.0,
29: 33.0,
30: 136.0},
(pd.Timestamp('2021-06-28 00:00:00'), 'total_sales'): {0: 4603.209999999999,
1: 2485.059999999998,
2: 4919.39999999998,
3: 6097.77,
4: 31.22,
5: 155.71,
6: 3484.99,
7: 17237.35999999996,
8: 561.54,
9: 698.75,
10: 1290.13,
11: 4292.68000000001,
12: 947.65,
13: 329.0,
14: 2889.65,
15: 9989.4,
16: 330.8899999999994,
17: 2076.26,
18: 2982.270000000004,
19: 11978.62000000002,
20: 683.0,
21: 0.0,
22: 3812.16999999999,
23: 20963.21000000002,
24: 234.4900000000002,
25: 896.1,
26: 231.0,
27: 893.810000000001,
28: 129.0,
29: 1712.329999999998,
30: 4106.729999999996},
(pd.Timestamp('2021-07-05 00:00:00'), 'total_orders'): {0: 109.0,
1: 48.0,
2: 174.0,
3: 592.0,
4: 13.0,
5: 5.0,
6: 43.0,
7: 984.0,
8: 62.0,
9: 13.0,
10: 37.0,
11: 196.0,
12: 8.0,
13: 1.0,
14: 3.0,
15: 30.0,
16: 76.0,
17: 5.0,
18: 147.0,
19: 88.0,
20: 8.0,
21: 1.0,
22: 78.0,
23: 1248.0,
24: 1.0,
25: 18.0,
26: 23.0,
27: 83.0,
28: 0.0,
29: 29.0,
30: 154.0},
(pd.Timestamp('2021-07-05 00:00:00'), 'total_sales'): {0: 3453.02,
1: 17868.730000000003,
2: 44707.82999999999,
3: 61425.99,
4: 1261.0,
5: 1914.6000000000001,
6: 24146.09,
7: 154777.25999999998,
8: 6201.489999999999,
9: 5513.960000000001,
10: 9645.87,
11: 25086.785,
12: 663.0,
13: 448.61,
14: 26332.7,
15: 73892.415,
16: 556.749999999999,
17: 1746.859999999997,
18: 4103.219999999994,
19: 15571.52000000008,
20: 86.0,
21: 69.0,
22: 5882.759999999995,
23: 26476.11000000004,
24: 53.0,
25: 801.220000000001,
26: 684.56,
27: 1232.600000000002,
28: 0.0,
29: 15902.1,
30: 43943.48},
(pd.Timestamp('2021-07-12 00:00:00'), 'total_orders'): {0: 32.0,
1: 15.0,
2: 89.0,
3: 239.0,
4: 2.0,
5: 3.0,
6: 20.0,
7: 400.0,
8: 0.0,
9: 0.0,
10: 0.0,
11: 0.0,
12: 0.0,
13: 0.0,
14: 0.0,
15: 0.0,
16: 21.0,
17: 14.0,
18: 58.0,
19: 281.0,
20: 3.0,
21: 3.0,
22: 33.0,
23: 413.0,
24: 7.0,
25: 6.0,
26: 4.0,
27: 13.0,
28: 0.0,
29: 18.0,
30: 48.0},
(pd.Timestamp('2021-07-12 00:00:00'), 'total_sales'): {0: 2147.7000000000003,
1: 4767.3,
2: 2399.300000000003,
3: 3137.440000000002,
4: 178.0,
5: 866.61,
6: 10639.03,
7: 73235.38,
8: 0.0,
9: 0.0,
10: 0.0,
11: 0.0,
12: 0.0,
13: 0.0,
14: 0.0,
15: 0.0,
16: 220.94,
17: 727.5199999999995,
18: 2500.96999999999,
19: 4414.00999999998,
20: 15.0,
21: 196.71,
22: 2170.1,
23: 9745.24999999997,
24: 126.55,
25: 290.2,
26: 146.01,
27: 233.0,
28: 0.0,
29: 973.18,
30: 1658.940000000002}}).set_index(['group','category'])
df2 = pd.DataFrame.from_dict({'group': {0: 'total_full',
1: 'total_full',
2: 'A',
3: 'A',
4: 'B',
5: 'B',
6: 'C',
7: 'C',
8: 'D',
9: 'D',
10: 'Apple_total',
11: 'Apple_total',
12: 'A',
13: 'A',
14: 'B',
15: 'B',
16: 'C',
17: 'C',
18: 'D',
19: 'D',
20: 'Amazon_total',
21: 'Amazon_total',
22: 'A',
23: 'A',
24: 'B',
25: 'B',
26: 'C',
27: 'C',
28: 'D',
29: 'D',
30: 'Google_total',
31: 'Google_total',
32: 'A',
33: 'A',
34: 'B',
35: 'B',
36: 'C',
37: 'C',
38: 'D',
39: 'D',
40: 'Facebook_total',
41: 'Facebook_total',
42: 'A',
43: 'A',
44: 'B',
45: 'B',
46: 'C',
47: 'C',
48: 'D',
49: 'D',
50: 'Netflix_total',
51: 'Netflix_total',
52: 'A',
53: 'A',
54: 'B',
55: 'B',
56: 'C',
57: 'C',
58: 'D',
59: 'D',
60: 'Tesla_total',
61: 'Tesla_total',
62: 'A',
63: 'A',
64: 'B',
65: 'B',
66: 'C',
67: 'C',
68: 'D',
69: 'D',
70: 'Uber_total',
71: 'Uber_total',
72: 'A',
73: 'A',
74: 'B',
75: 'B',
76: 'C',
77: 'C',
78: 'D',
79: 'D'},
'category': {0: 'total_full',
1: 'total_full',
2: 'group_total',
3: 'group_total',
4: 'group_total',
5: 'group_total',
6: 'group_total',
7: 'group_total',
8: 'group_total',
9: 'group_total',
10: 'Apple_total',
11: 'Apple_total',
12: 'Apple',
13: 'Apple',
14: 'Apple',
15: 'Apple',
16: 'Apple',
17: 'Apple',
18: 'Apple',
19: 'Apple',
20: 'Amazon_total',
21: 'Amazon_total',
22: 'Amazon',
23: 'Amazon',
24: 'Amazon',
25: 'Amazon',
26: 'Amazon',
27: 'Amazon',
28: 'Amazon',
29: 'Amazon',
30: 'Google_total',
31: 'Google_total',
32: 'Google',
33: 'Google',
34: 'Google',
35: 'Google',
36: 'Google',
37: 'Google',
38: 'Google',
39: 'Google',
40: 'Facebook_total',
41: 'Facebook_total',
42: 'Facebook',
43: 'Facebook',
44: 'Facebook',
45: 'Facebook',
46: 'Facebook',
47: 'Facebook',
48: 'Facebook',
49: 'Facebook',
50: 'Netflix_total',
51: 'Netflix_total',
52: 'Netflix',
53: 'Netflix',
54: 'Netflix',
55: 'Netflix',
56: 'Netflix',
57: 'Netflix',
58: 'Netflix',
59: 'Netflix',
60: 'Tesla_total',
61: 'Tesla_total',
62: 'Tesla',
63: 'Tesla',
64: 'Tesla',
65: 'Tesla',
66: 'Tesla',
67: 'Tesla',
68: 'Tesla',
69: 'Tesla',
70: 'Uber_total',
71: 'Uber_total',
72: 'Uber',
73: 'Uber',
74: 'Uber',
75: 'Uber',
76: 'Uber',
77: 'Uber',
78: 'Uber',
79: 'Uber'},
'type': {0: 'Sales_1',
1: 'Sales_2',
2: 'Sales_1',
3: 'Sales_2',
4: 'Sales_1',
5: 'Sales_2',
6: 'Sales_1',
7: 'Sales_2',
8: 'Sales_1',
9: 'Sales_2',
10: 'Sales_1',
11: 'Sales_2',
12: 'Sales_1',
13: 'Sales_2',
14: 'Sales_1',
15: 'Sales_2',
16: 'Sales_1',
17: 'Sales_2',
18: 'Sales_1',
19: 'Sales_2',
20: 'Sales_1',
21: 'Sales_2',
22: 'Sales_1',
23: 'Sales_2',
24: 'Sales_1',
25: 'Sales_2',
26: 'Sales_1',
27: 'Sales_2',
28: 'Sales_1',
29: 'Sales_2',
30: 'Sales_1',
31: 'Sales_2',
32: 'Sales_1',
33: 'Sales_2',
34: 'Sales_1',
35: 'Sales_2',
36: 'Sales_1',
37: 'Sales_2',
38: 'Sales_1',
39: 'Sales_2',
40: 'Sales_1',
41: 'Sales_2',
42: 'Sales_1',
43: 'Sales_2',
44: 'Sales_1',
45: 'Sales_2',
46: 'Sales_1',
47: 'Sales_2',
48: 'Sales_1',
49: 'Sales_2',
50: 'Sales_1',
51: 'Sales_2',
52: 'Sales_1',
53: 'Sales_2',
54: 'Sales_1',
55: 'Sales_2',
56: 'Sales_1',
57: 'Sales_2',
58: 'Sales_1',
59: 'Sales_2',
60: 'Sales_1',
61: 'Sales_2',
62: 'Sales_1',
63: 'Sales_2',
64: 'Sales_1',
65: 'Sales_2',
66: 'Sales_1',
67: 'Sales_2',
68: 'Sales_1',
69: 'Sales_2',
70: 'Sales_1',
71: 'Sales_2',
72: 'Sales_1',
73: 'Sales_2',
74: 'Sales_1',
75: 'Sales_2',
76: 'Sales_1',
77: 'Sales_2',
78: 'Sales_1',
79: 'Sales_2'},
'2021-06-28': {0: 67.5277641202152,
1: 82.7854700135998,
2: 21.50082266792856,
3: 22.03644997199996,
4: 64.460440147,
5: 10.1060499896,
6: 65.1530371974946,
7: 50.6429700519999,
8: 56.413464107792045,
9: 0,
10: 17.48074540313092,
11: 26.8376199976,
12: 52.172,
13: 61.16600000040001,
14: 20.9447844,
15: 40.69122000000001,
16: 83.55718929717925,
17: 14.98039999719995,
18: 20.806771705951697,
19: np.nan,
20: 18.3766353690825,
21: 12.82565001479992,
22: 52.425508769690694,
23: 25.661999978399994,
24: 17.88071596,
25: 24.384659998799997,
26: 91.10086982794643,
27: 12.77899003759993,
28: 16.969540811445366,
29: np.nan,
30: 18.8795397517309,
31: 26.73017999840005,
32: 53.52039700062155,
33: 58.81199999639999,
34: 12.1243325,
35: 24.0544100028,
36: 55.94068246571674,
37: 133.86376999920006,
38: 7.294127785392621,
39: np.nan,
40: 6.07807089184563,
41: 7.27483001599998,
42: 2.300470581874837,
43: 30.71300000639998,
44: 5.810764652,
45: 12.333119997600003,
46: 25.475930745418292,
47: 64.228710012,
48: 9.490904912552498,
49: np.nan,
50: 8.184780211399392,
51: 24.59321999400001,
52: 6.807138946302334,
53: 12.0879999972,
54: 0.869207661,
55: 0.324,
56: 0.5084336040970575,
57: 12.181219996800007,
58: 0,
59: np.nan,
60: 9.293956915067886,
61: 11.171379993599999,
62: 6.384936971649232,
63: 3.657999996,
64: 0.913782413,
65: 1.9992000012000002,
66: 1.5322078073061867,
67: 5.514179996399999,
68: 0.4630297231124678,
69: np.nan,
70: 36.23403557795798,
71: 53.35258999919999,
72: 21.890370397789923,
73: 9.937449997200002,
74: 5.916852561,
75: 6.319439989199998,
76: 7.03772344983066,
77: 37.095700012799995,
78: 1.3890891693374032,
79: np.nan},
'2021-07-05': {0: 65.4690491915759,
1: 98.5235100112003,
2: 21.4573181155924,
3: 241.06741999679997,
4: 67.481716829,
5: 11.60325000040002,
6: 27.5807099999998,
7: 65.8528400140003,
8: 58.949304246983736,
9: 0.0,
10: 185.65887577993723,
11: 318.9965699964001,
12: 54.517,
13: 66.55265999039996,
14: 21.92632044,
15: 43.67116000320002,
16: 87.47349898707688,
17: 208.7727500028001,
18: 21.742056352860352,
19: np.nan,
20: 16.6038963173654,
21: 25.28952001920013,
22: 54.7820864335212,
23: 36.75802000560001,
24: 18.71872129,
25: 30.1634600016,
26: 95.37075040035738,
27: 138.3680400120001,
28: 17.73233819348684,
29: np.nan,
30: 14.80302342121337,
31: 251.83851001200003,
32: 55.926190956481534,
33: 72.4443400032,
34: 12.69221484,
35: 26.032340003999998,
36: 58.56261169338368,
37: 153.36183000480003,
38: 7.622005931348156,
39: np.nan,
40: 72.24367956241771,
41: 14.83083001279999,
42: 29.5726042895728,
43: 38.723000005199985,
44: 6.083562133,
45: 12.845630001599998,
46: 26.66998281055652,
47: 63.26220000600001,
48: 9.917530329288393,
49: np.nan,
50: 8.555606693927,
51: 23.802009994800002,
52: 7.113126469779095,
53: 7.206999998399999,
54: 0.910216433,
55: 1.4089999991999997,
56: 0.5322637911479053,
57: 15.186009997200001,
58: 0.0,
59: np.nan,
60: 9.716385738295367,
61: 14.7327399948,
62: 6.671946105284065,
63: 5.691999996,
64: 0.956574175,
65: 1.0203399996,
66: 1.6040220980113027,
67: 8.020399999199999,
68: 0.4838433599999999,
69: np.nan,
70: 37.88758167841983,
71: 59.03332998119994,
72: 22.874363860953647,
73: 13.690399997999998,
74: 6.194107518,
75: 6.4613199911999954,
76: 7.367580219466185,
77: 38.881609991999944,
78: 1.4515300799999995,
79: np.nan},
'2021-07-12': {0: 607.2971827405001,
1: 88.9671100664001,
2: 21.26749278974862,
3: 17.1524199804,
4: 64.471138092,
5: 89.84481002279999,
6: 26.2044999999998,
7: 51.9698800632001,
8: 5.354051858751745,
9: 0.0,
10: 177.42361595891452,
11: 287.5395700032,
12: 52.117,
13: 47.388199995600004,
14: 20.94835038,
15: 41.4250800048,
16: 83.57340667555117,
17: 198.72629000280003,
18: 20.784858903363354,
19: np.nan,
20: 178.323907459086,
21: 185.83897002839998,
22: 52.37029646474982,
23: 27.87144997800001,
24: 17.88339044,
25: 23.645340010799984,
26: 91.11855133792106,
27: 134.3221800396,
28: 16.95166921641509,
29: np.nan,
30: 128.82813286243115,
31: 192.6867300156,
32: 53.46403160619618,
33: 41.412320006399995,
34: 12.1261155,
35: 11.840830002000002,
36: 55.95153983444301,
37: 139.43358000720002,
38: 7.286445921791947,
39: np.nan,
40: 69.04410667683521,
41: 93.877410018,
42: 28.270665735943805,
43: 27.512680004399986,
44: 5.811656147,
45: 5.2319800032,
46: 25.480875296710053,
47: 61.132750010400024,
48: 9.480909497181356,
49: np.nan,
50: 8.178601399067174,
51: 17.6743199976,
52: 6.7999699585309585,
53: 6.131999998799999,
54: 0.870099156,
55: 0.6185600004,
56: 0.5085322845362154,
57: 10.923759998400003,
58: 0.0,
59: np.nan,
60: 9.287042311133577,
61: 19.966500000000007,
62: 6.378212628950804,
63: 6.524999997600001,
64: 0.913782413,
65: 1.9303400016,
66: 1.5325051891827732,
67: 11.511160000800006,
68: 0.4625420799999998,
69: np.nan,
70: 36.21177607303267,
71: 51.3836100036,
72: 21.86731639537707,
73: 10.310769999600003,
74: 5.917744056,
75: 5.152679999999999,
76: 7.039089381655591,
77: 35.920160003999996,
78: 1.3876262399999995,
79: np.nan}}).set_index(['group','category','type'])
f = lambda x: pd.to_datetime(x)
df = (df1.merge(df2.rename(columns=f).unstack(), left_index=True, right_index=True)
.sort_index(axis=1))
I am trying to add a column last_week_sales to each multiindex column.
So the total_sales from the previous week becomes last_week_sales in the following week in every multiindex column except for the first week as there are no data of the previous week.
What I've tried:
pd.merge(df.shift(-4, axis = 1).xs('total_sales', level=1,drop_level=False))
I am not sure how I can select only the second index, few answers suggested using .xs() but I am not sure how I would merge it on the date column, I tried shifting everything by -4 as then I get the desired week shift, but maybe there are better ways of achieving this.
Right now df.columns return
MultiIndex([('2021-06-28', 'Sales_1'),
('2021-06-28', 'Sales_2'),
('2021-06-28', 'total_orders'),
('2021-06-28', 'total_sales'),
('2021-07-05', 'Sales_1'),
('2021-07-05', 'Sales_2'),
('2021-07-05', 'total_orders'),
('2021-07-05', 'total_sales'),
('2021-07-12', 'Sales_1'),
('2021-07-12', 'Sales_2'),
('2021-07-12', 'total_orders'),
('2021-07-12', 'total_sales')])
And I would like to have:
MultiIndex([('2021-06-28', 'Sales_1'),
('2021-06-28', 'Sales_2'),
('2021-06-28', 'total_orders'),
('2021-06-28', 'total_sales'),
('2021-06-28', 'last_week_sales')
('2021-07-05', 'Sales_1'),
('2021-07-05', 'Sales_2'),
('2021-07-05', 'total_orders'),
('2021-07-05', 'total_sales'),
('2021-07-05', 'last_week_sales')
('2021-07-12', 'Sales_1'),
('2021-07-12', 'Sales_2'),
('2021-07-12', 'total_orders'),
('2021-07-12', 'total_sales'),
('2021-07-12', 'last_week_sales')])
Let us do in steps
Select the cross section of dataframe using loc
Shift the cross section along the columns axis
Rename total_sales to last_week_sales and combine the cross section with df
s = df.loc[:, (slice(None), 'total_sales')].shift(axis=1)
s = s.rename({'total_sales': 'last_week_sales'}, axis=1, level=1)
df.combine_first(s)
I want to plot a double y-axis plot where the user can select which variable to look at. I want the dropdown option to change the original y-axis, but also want the second y-axis to change with the variable change. The data comes from two dictionaries, where each dictionary has 2 keys which are dataframes. Here are the dicts:
df_vals.keys()
['corn','soybeans']
{'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-21 00:00:00'), 2: Timestamp('2020-09-22 00:00:00'), 3: Timestamp('2020-09-23 00:00:00'), 4: Timestamp('2020-09-24 00:00:00'), 5: Timestamp('2020-09-25 00:00:00'), 6: Timestamp('2020-09-26 00:00:00'), 7: Timestamp('2020-09-27 00:00:00'), 8: Timestamp('2020-09-28 00:00:00'), 9: Timestamp('2020-09-29 00:00:00'), 10: Timestamp('2020-09-30 00:00:00'), 11: Timestamp('2020-10-01 00:00:00'), 12: Timestamp('2020-10-02 00:00:00'), 13: Timestamp('2020-10-03 00:00:00'), 14: Timestamp('2020-10-04 00:00:00'), 15: Timestamp('2020-10-05 00:00:00')}, '2m_temp_24hdelta_prod': {0: -0.33056551535638423, 1: -0.06352654836543402, 2: -0.276225357129493, 3: -0.7788107233764435, 4: -0.12182345812042986, 5: 0.4844870528180379, 6: -0.7795401737865336, 7: -1.3736563255451468, 8: -0.7925949910485673, 9: -0.6084065411465024, 10: -0.6532739602739552, 11: -0.5334182358612463, 12: -0.3214601096392593, 13: 0.005672046711691303, 14: 0.294874775721931, 15: 0.0}, '2m_temp_24hdelta_area': {0: -0.2942234811255382, 1: -0.06695257736951654, 2: -0.2382484006633631, 3: -0.747444491858053, 4: -0.008792915536150941, 5: 0.25843822574146474, 6: -0.8653840134962728, 7: -1.3306357244341183, 8: -0.77807373911064, 9: -0.6101878063800394, 10: -0.6602593986352374, 11: -0.5377113682438022, 12: -0.3489377042704134, 13: -0.12997682663355925, 14: 0.08031697715852158, 15: 0.0}, 'total_precip_24hdelta_prod': {0: -1.4417379991236094, 1: -3.24276961794111, 2: -1.9549294932106442, 3: 1.0805492310759035, 4: -2.8572102912370547, 5: -0.6036102993252254, 6: 0.068152851219848, 7: -0.37760433118519804, 8: -0.2182212103223477, 9: 0.14385291749053375, 10: -0.09611101762074367, 11: -0.44170367466228744, 12: -0.9926136091953488, 13: -0.26638156415402015, 14: -0.0435076228174961, 15: 0.0}, 'total_precip_24hdelta_area': {0: -0.8969484573159082, 1: -2.002193332285897, 2: -1.3265684841453602, 3: 0.5232970017014504, 4: -1.9744318826572878, 5: -0.4756557183821801, 6: 0.09401319045911638, 7: -0.2480262049680989, 8: -0.16164720991353132, 9: 0.12918646107950632, 10: -0.025385234499880355, 11: -0.37094005589542123, 12: -0.7621485373750772, 13: -0.21974376949919702, 14: -0.06387744429696394, 15: 0.0}}
{'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-21 00:00:00'), 2: Timestamp('2020-09-22 00:00:00'), 3: Timestamp('2020-09-23 00:00:00'), 4: Timestamp('2020-09-24 00:00:00'), 5: Timestamp('2020-09-25 00:00:00'), 6: Timestamp('2020-09-26 00:00:00'), 7: Timestamp('2020-09-27 00:00:00'), 8: Timestamp('2020-09-28 00:00:00'), 9: Timestamp('2020-09-29 00:00:00'), 10: Timestamp('2020-09-30 00:00:00'), 11: Timestamp('2020-10-01 00:00:00'), 12: Timestamp('2020-10-02 00:00:00'), 13: Timestamp('2020-10-03 00:00:00'), 14: Timestamp('2020-10-04 00:00:00'), 15: Timestamp('2020-10-05 00:00:00')}, '2m_temp_24hdelta_prod': {0: -0.41072100441995785, 1: -0.11297222900273206, 2: -0.3134558546203517, 3: -0.7055797268403201, 4: -0.14038170579478787, 5: 0.2625117005223226, 6: -0.7101408224478941, 7: -1.2573241419225902, 8: -0.7906424533010389, 9: -0.661536032998284, 10: -0.6822789549363446, 11: -0.5744259308508103, 12: -0.4206210457410846, 13: -0.08431581171659296, 14: 0.2654961632050054, 15: 0.0}, '2m_temp_24hdelta_area': {0: -0.36772899690348704, 1: -0.05348366544356093, 2: -0.24183851104639115, 3: -0.6953305273210354, 4: -0.045424744173602496, 5: 0.19001500780368247, 6: -0.821935103347599, 7: -1.265563237441084, 8: -0.7328581189116925, 9: -0.6143048783236709, 10: -0.6633462127739507, 11: -0.5190203572096471, 12: -0.39498226425933547, 13: -0.10526773964830015, 14: 0.19258302497068705, 15: 0.0}, 'total_precip_24hdelta_prod': {0: -0.6776698356563055, 1: -1.7516299539550721, 2: -1.270815712012518, 3: 0.5996647217068156, 4: -2.2466045890287396, 5: -0.6706621597096377, 6: 0.06719919310410405, 7: -0.3572052717181586, 8: -0.2512178851633781, 9: 0.1313636195701381, 10: 0.08179245850142033, 11: -0.3036658888458674, 12: -0.8200237460316371, 13: -0.32927495598480583, 14: -0.08189579932441349, 15: 0.0}, 'total_precip_24hdelta_area': {0: -0.7530386453128719, 1: -1.7027556154743797, 2: -1.0907657669920294, 3: 0.429094034006764, 4: -1.9424324271143407, 5: -0.5370968484676638, 6: 0.044404490985521246, 7: -0.30794153270377544, 8: -0.2471378005907392, 9: 0.1212115468745879, 10: 0.07486462848670472, 11: -0.3507116185903665, 12: -0.7247205225779287, 13: -0.25431240691233936, 14: -0.06960793926681186, 15: 0.0}}
df_deltas.keys()
['corn','soybeans']
{'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-20 06:00:00'), 2: Timestamp('2020-09-20 12:00:00'), 3: Timestamp('2020-09-20 18:00:00'), 4: Timestamp('2020-09-21 00:00:00'), 5: Timestamp('2020-09-21 06:00:00'), 6: Timestamp('2020-09-21 12:00:00'), 7: Timestamp('2020-09-21 18:00:00'), 8: Timestamp('2020-09-22 00:00:00'), 9: Timestamp('2020-09-22 06:00:00'), 10: Timestamp('2020-09-22 12:00:00'), 11: Timestamp('2020-09-22 18:00:00'), 12: Timestamp('2020-09-23 00:00:00'), 13: Timestamp('2020-09-23 06:00:00'), 14: Timestamp('2020-09-23 12:00:00'), 15: Timestamp('2020-09-23 18:00:00'), 16: Timestamp('2020-09-24 00:00:00'), 17: Timestamp('2020-09-24 06:00:00'), 18: Timestamp('2020-09-24 12:00:00'), 19: Timestamp('2020-09-24 18:00:00'), 20: Timestamp('2020-09-25 00:00:00'), 21: Timestamp('2020-09-25 06:00:00'), 22: Timestamp('2020-09-25 12:00:00'), 23: Timestamp('2020-09-25 18:00:00'), 24: Timestamp('2020-09-26 00:00:00'), 25: Timestamp('2020-09-26 06:00:00'), 26: Timestamp('2020-09-26 12:00:00'), 27: Timestamp('2020-09-26 18:00:00'), 28: Timestamp('2020-09-27 00:00:00'), 29: Timestamp('2020-09-27 06:00:00'), 30: Timestamp('2020-09-27 12:00:00'), 31: Timestamp('2020-09-27 18:00:00'), 32: Timestamp('2020-09-28 00:00:00'), 33: Timestamp('2020-09-28 06:00:00'), 34: Timestamp('2020-09-28 12:00:00'), 35: Timestamp('2020-09-28 18:00:00'), 36: Timestamp('2020-09-29 00:00:00'), 37: Timestamp('2020-09-29 06:00:00'), 38: Timestamp('2020-09-29 12:00:00'), 39: Timestamp('2020-09-29 18:00:00'), 40: Timestamp('2020-09-30 00:00:00'), 41: Timestamp('2020-09-30 06:00:00'), 42: Timestamp('2020-09-30 12:00:00'), 43: Timestamp('2020-09-30 18:00:00'), 44: Timestamp('2020-10-01 00:00:00'), 45: Timestamp('2020-10-01 06:00:00'), 46: Timestamp('2020-10-01 12:00:00'), 47: Timestamp('2020-10-01 18:00:00'), 48: Timestamp('2020-10-02 00:00:00'), 49: Timestamp('2020-10-02 06:00:00'), 50: Timestamp('2020-10-02 12:00:00'), 51: Timestamp('2020-10-02 18:00:00'), 52: Timestamp('2020-10-03 00:00:00'), 53: Timestamp('2020-10-03 06:00:00'), 54: Timestamp('2020-10-03 12:00:00'), 55: Timestamp('2020-10-03 18:00:00'), 56: Timestamp('2020-10-04 00:00:00'), 57: Timestamp('2020-10-04 06:00:00'), 58: Timestamp('2020-10-04 12:00:00'), 59: Timestamp('2020-10-04 18:00:00'), 60: Timestamp('2020-10-05 00:00:00')}, '2m_temp_prod': {0: 299.346777266481, 1: 294.0395117035824, 2: 292.9592738522704, 3: 301.3180464757325, 4: 300.62356652087135, 5: 295.3372490145483, 6: 293.70255086586366, 7: 301.74835531223954, 8: 300.63094530701716, 9: 295.19553201608596, 10: 293.61613871139605, 11: 302.1631229776387, 12: 300.9153730135594, 13: 295.15968865817507, 14: 293.4150837111033, 15: 301.41874976967574, 16: 300.2591302298784, 17: 294.35648855874496, 18: 292.60893943975975, 19: 300.69793228145136, 20: 299.21051997362474, 21: 293.03793188187217, 22: 290.7864320360987, 23: 298.83351270175194, 24: 297.3528693941268, 25: 291.19369512544074, 26: 289.09521928196455, 27: 297.6441935083638, 28: 296.7296754953959, 29: 290.9897312443865, 30: 289.32126319033677, 31: 298.7574828448876, 32: 298.12901959834915, 33: 292.5899489699376, 34: 290.89859256270717, 35: 300.43330551865887, 36: 299.4213926560918, 37: 293.6473005538043, 38: 291.78953606425546, 39: 300.96031965551913, 40: 299.8616920790521, 41: 293.9476866306047, 42: 292.00448821963647, 43: 301.2204786022793, 44: 300.1545728526989, 45: 294.30968496479613, 46: 292.3264718663577, 47: 301.6422264431679, 48: 300.6382119018213, 49: 294.9066383821488, 50: 292.9206232319892, 51: 302.28532470110673, 52: 301.118337633183, 53: 295.580361365072, 54: 293.5071232611944, 55: 302.4993713158521, 56: 301.1771411161832, 57: 295.87429820309063, 58: 293.8387870006977, 59: 302.3844741742686, 60: 300.92076629492163}, '2m_temp_area': {0: 299.79923425166567, 1: 294.4433522483179, 2: 293.1829305163771, 3: 301.76751592578194, 4: 300.97964970416314, 5: 295.58816311209137, 6: 293.944118250348, 7: 302.20448177813586, 8: 301.0782745090335, 9: 295.36833103932497, 10: 293.7448705435944, 11: 302.2414068531493, 12: 300.97154909133104, 13: 295.09171791304016, 14: 293.29872586829305, 15: 301.63398450993316, 16: 300.39729350367077, 17: 294.4765532352668, 18: 292.6348308697987, 19: 300.7196467769189, 20: 299.2413397435659, 21: 293.1571305856655, 22: 290.9298399188299, 23: 299.0361571649767, 24: 297.6784967259681, 25: 291.6225850820778, 26: 289.5307387179231, 27: 298.1675193109706, 28: 297.2851193736129, 29: 291.6047803054954, 30: 289.8725165929944, 31: 299.2259541567033, 32: 298.5645974740215, 33: 292.9844928941141, 34: 291.16692080415885, 35: 300.6640689360243, 36: 299.63105315816887, 37: 293.7973759485704, 38: 291.8748714049971, 39: 301.07558737962773, 40: 299.9769492884242, 41: 294.06556034682825, 42: 292.0989825175383, 43: 301.3224275326778, 44: 300.2429318269633, 45: 294.4016189433437, 46: 292.39193470959646, 47: 301.691478296914, 48: 300.693790365317, 49: 294.95729295702677, 50: 292.94815058578274, 51: 302.1922658603409, 52: 301.01878139937173, 53: 295.47413908911483, 54: 293.41960123543845, 55: 302.34370446075303, 56: 301.05227283650527, 57: 295.72013507339597, 58: 293.6867376233815, 59: 302.19133411599717, 60: 300.81799340051333}, 'total_precip_prod': {0: 0.0, 1: 0.19106968995229195, 2: 0.1557650112297922, 3: 0.42176822127339436, 4: 0.3635719686978164, 5: 0.624238127750141, 6: 0.7363658332723646, 7: 0.6637602529624484, 8: 0.8303668371501847, 9: 0.9645353650727154, 10: 0.9080621631493283, 11: 0.7407109615632305, 12: 1.2188155467167008, 13: 1.4934816133864144, 14: 0.8673540204216558, 15: 0.4536446670226782, 16: 0.5838893851141779, 17: 0.28997017910749373, 18: 0.396575809101194, 19: 0.3017425722928852, 20: 0.2600037974259506, 21: 0.10090977019496428, 22: 0.1120663639424084, 23: 0.11820662603244433, 24: 0.11351422163952127, 25: 0.0354462928350286, 26: 0.08973231687769569, 27: 0.11707786940722151, 28: 0.07097838855834729, 29: 0.03411315769121203, 30: 0.09051350215127027, 31: 0.11314915610119855, 32: 0.14753233086329737, 33: 0.1845242398275337, 34: 0.2518174918229658, 35: 0.17011312187047897, 36: 0.2749232044764252, 37: 0.2972017724997405, 38: 0.3552475267861772, 39: 0.27943871692889416, 40: 0.31981605728486306, 41: 0.19735958986038185, 42: 0.2806487307465678, 43: 0.2830918175327961, 44: 0.3288561686915008, 45: 0.1744303376099874, 46: 0.30609645936303326, 47: 0.21925050665230758, 48: 0.20281755796768022, 49: 0.06620804878284778, 50: 0.17684099550656973, 51: 0.19853145136367492, 52: 0.2563854573673008, 53: 0.24201876323456878, 54: 0.41939251801295807, 55: 0.36402178359658277, 56: 0.37120895070913373, 57: 0.281792653746079, 58: 0.5868870658107626, 59: 0.4927117680726645, 60: 0.5223743530274055}, 'total_precip_area': {0: 0.0, 1: 0.2869521154242563, 2: 0.21660564402622137, 3: 0.4856913604264373, 4: 0.5011640941544891, 5: 0.7562519498474788, 6: 0.7839821183163472, 7: 0.5862595162666312, 8: 0.8974715199444484, 9: 0.9610750641626696, 10: 0.7601911282379429, 11: 0.793906823631679, 12: 1.2383530336279156, 13: 1.0049543280590305, 14: 0.6820852351772055, 15: 0.442434189821785, 16: 0.6589929874909368, 17: 0.32723035127408134, 18: 0.4144798865599254, 19: 0.368782410513661, 20: 0.36137157927969976, 21: 0.1635789497829729, 22: 0.16409778482825316, 23: 0.16768421904602382, 24: 0.1808376986943956, 25: 0.058153150056705245, 26: 0.1269072928484502, 27: 0.14562713451681256, 28: 0.11216762073388148, 29: 0.062161294677683845, 30: 0.13994272590442386, 31: 0.1662560753829063, 32: 0.20624170840852787, 33: 0.216927525300605, 34: 0.26804054777036235, 35: 0.20587583527311556, 36: 0.297848309035157, 37: 0.2836083045675833, 38: 0.33686407205543173, 39: 0.276288892940258, 40: 0.3569925386418611, 41: 0.2258142673918131, 42: 0.30458249604018844, 43: 0.2882872466330577, 44: 0.36861110970625127, 45: 0.1945528585156628, 46: 0.2996099093542968, 47: 0.2370841370242934, 48: 0.24429531409451738, 49: 0.09534765734716107, 50: 0.19399822393477686, 51: 0.24311804406545334, 52: 0.3062432174815857, 53: 0.27031885272433986, 54: 0.40763842665734645, 55: 0.3546261915245615, 56: 0.4085152708749088, 57: 0.30056439476727764, 58: 0.5493651343645845, 59: 0.4937984226857489, 60: 0.531137675746978}}
{'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-20 06:00:00'), 2: Timestamp('2020-09-20 12:00:00'), 3: Timestamp('2020-09-20 18:00:00'), 4: Timestamp('2020-09-21 00:00:00'), 5: Timestamp('2020-09-21 06:00:00'), 6: Timestamp('2020-09-21 12:00:00'), 7: Timestamp('2020-09-21 18:00:00'), 8: Timestamp('2020-09-22 00:00:00'), 9: Timestamp('2020-09-22 06:00:00'), 10: Timestamp('2020-09-22 12:00:00'), 11: Timestamp('2020-09-22 18:00:00'), 12: Timestamp('2020-09-23 00:00:00'), 13: Timestamp('2020-09-23 06:00:00'), 14: Timestamp('2020-09-23 12:00:00'), 15: Timestamp('2020-09-23 18:00:00'), 16: Timestamp('2020-09-24 00:00:00'), 17: Timestamp('2020-09-24 06:00:00'), 18: Timestamp('2020-09-24 12:00:00'), 19: Timestamp('2020-09-24 18:00:00'), 20: Timestamp('2020-09-25 00:00:00'), 21: Timestamp('2020-09-25 06:00:00'), 22: Timestamp('2020-09-25 12:00:00'), 23: Timestamp('2020-09-25 18:00:00'), 24: Timestamp('2020-09-26 00:00:00'), 25: Timestamp('2020-09-26 06:00:00'), 26: Timestamp('2020-09-26 12:00:00'), 27: Timestamp('2020-09-26 18:00:00'), 28: Timestamp('2020-09-27 00:00:00'), 29: Timestamp('2020-09-27 06:00:00'), 30: Timestamp('2020-09-27 12:00:00'), 31: Timestamp('2020-09-27 18:00:00'), 32: Timestamp('2020-09-28 00:00:00'), 33: Timestamp('2020-09-28 06:00:00'), 34: Timestamp('2020-09-28 12:00:00'), 35: Timestamp('2020-09-28 18:00:00'), 36: Timestamp('2020-09-29 00:00:00'), 37: Timestamp('2020-09-29 06:00:00'), 38: Timestamp('2020-09-29 12:00:00'), 39: Timestamp('2020-09-29 18:00:00'), 40: Timestamp('2020-09-30 00:00:00'), 41: Timestamp('2020-09-30 06:00:00'), 42: Timestamp('2020-09-30 12:00:00'), 43: Timestamp('2020-09-30 18:00:00'), 44: Timestamp('2020-10-01 00:00:00'), 45: Timestamp('2020-10-01 06:00:00'), 46: Timestamp('2020-10-01 12:00:00'), 47: Timestamp('2020-10-01 18:00:00'), 48: Timestamp('2020-10-02 00:00:00'), 49: Timestamp('2020-10-02 06:00:00'), 50: Timestamp('2020-10-02 12:00:00'), 51: Timestamp('2020-10-02 18:00:00'), 52: Timestamp('2020-10-03 00:00:00'), 53: Timestamp('2020-10-03 06:00:00'), 54: Timestamp('2020-10-03 12:00:00'), 55: Timestamp('2020-10-03 18:00:00'), 56: Timestamp('2020-10-04 00:00:00'), 57: Timestamp('2020-10-04 06:00:00'), 58: Timestamp('2020-10-04 12:00:00'), 59: Timestamp('2020-10-04 18:00:00'), 60: Timestamp('2020-10-05 00:00:00')}, '2m_temp_prod': {0: 299.4681144428137, 1: 294.4593329079582, 2: 293.54097959722486, 3: 301.26850218370805, 4: 300.4714986353434, 5: 295.6384695410705, 6: 294.2218375466852, 7: 301.7426496375417, 8: 300.4512511041961, 9: 295.3463775610583, 10: 294.00147574962756, 11: 302.04475406462933, 12: 300.7236658622478, 13: 295.24334771914255, 14: 293.7074020631231, 15: 301.7624504225804, 16: 300.4964931687989, 17: 294.75437504400884, 18: 293.1177542350017, 19: 301.0425274988938, 20: 299.46808452778663, 21: 293.52076913001173, 22: 291.3670613175431, 23: 299.3793965992101, 24: 297.7263093286383, 25: 291.7095202331552, 26: 289.6578323905783, 27: 298.15977187729777, 28: 296.9884342085861, 29: 291.43100183368114, 30: 289.77806100941024, 31: 299.00450242361666, 32: 298.15618404166, 33: 292.73800154126627, 34: 291.07457059787265, 35: 300.5683567053199, 36: 299.3708029435806, 37: 293.67890398828234, 38: 291.954942552734, 39: 301.1051505527471, 40: 299.7999782544342, 41: 294.0840368998602, 42: 292.31118300204673, 43: 301.3976538328202, 44: 300.07823191309984, 45: 294.45023381986016, 46: 292.62675527729584, 47: 301.7900197148812, 48: 300.5590082986463, 49: 294.9896801203776, 50: 293.1241761449157, 51: 302.36329836985607, 52: 300.97122596719737, 53: 295.59237808816266, 54: 293.67785157848175, 55: 302.64353699591527, 56: 301.1035750112269, 57: 295.9278212054364, 58: 294.0324357819224, 59: 302.4832018963981, 60: 300.8427400317463}, '2m_temp_area': {0: 299.4903718737801, 1: 294.2924835295078, 2: 293.30135473218274, 3: 301.3528640298525, 4: 300.5939881510472, 5: 295.70744811622785, 6: 294.2521907610824, 7: 301.7100774359669, 8: 300.44506163325167, 9: 295.238423311095, 10: 293.8509583022139, 11: 301.6235104014979, 12: 300.38743669809435, 13: 294.8808530776623, 14: 293.33325543225476, 15: 301.4149251854487, 16: 300.1796642776425, 17: 294.39239805130165, 18: 292.66981534530925, 19: 300.5419360845211, 20: 299.0580229759339, 21: 293.0690025289753, 22: 290.9169097601789, 23: 298.92199569749033, 24: 297.4673382419046, 25: 291.4599993026732, 26: 289.452388873785, 27: 297.98953222540143, 28: 296.98633186869347, 29: 291.4589865652578, 30: 289.8288914753008, 31: 298.94272407170354, 32: 298.23250648648224, 33: 292.83814534455996, 34: 291.11886718509857, 35: 300.40881750455577, 36: 299.31958687273186, 37: 293.607006890439, 38: 291.81444341023143, 39: 300.8265637430714, 40: 299.6454967052681, 41: 293.9106555122605, 42: 292.1023114379392, 43: 301.12502226064896, 44: 299.91897396641514, 45: 294.27870082533354, 46: 292.4308511036389, 47: 301.51583391071864, 48: 300.39429493794523, 49: 294.825486042682, 50: 292.93778771753085, 51: 302.0071005199438, 52: 300.74239613744925, 53: 295.38249105817647, 54: 293.45710875169135, 55: 302.25559951138456, 56: 300.8353769061142, 57: 295.6525398583873, 58: 293.7632451527435, 59: 302.0551372494585, 60: 300.54910101224283}, 'total_precip_prod': {0: 0.0, 1: 0.4344171852327147, 2: 0.4351379346608463, 3: 0.9091314020100694, 4: 0.7838464939768977, 5: 1.0303630570555136, 6: 1.168093427310163, 7: 0.8635649194545677, 8: 1.180842617204555, 9: 1.0926651254176625, 10: 0.8283334033945278, 11: 0.8444548557416743, 12: 1.3230640130302405, 13: 1.2194759096975982, 14: 0.7328129706460099, 15: 0.47450659159057845, 16: 0.5808363103089833, 17: 0.28338951318682726, 18: 0.39698079867625957, 19: 0.37495920568614616, 20: 0.33256622409109604, 21: 0.09931783596352596, 22: 0.1293731106563246, 23: 0.14798406577292791, 24: 0.18096669978617885, 25: 0.05295233242627148, 26: 0.11851136022355316, 27: 0.12519627617895362, 28: 0.09146508542790638, 29: 0.04829302677559146, 30: 0.11041590211762027, 31: 0.14573868900912046, 32: 0.18215252985155908, 33: 0.18280908457277276, 34: 0.2248243307367063, 35: 0.17947363978224146, 36: 0.2924242956937053, 37: 0.2586747544378083, 38: 0.33937087683999867, 39: 0.2928781027298378, 40: 0.3935333755687144, 41: 0.2365752557775799, 42: 0.3215975487263272, 43: 0.32446922789494864, 44: 0.40520923133523234, 45: 0.18850232039108583, 46: 0.3206818541123286, 47: 0.2592275400999963, 48: 0.2610206474998234, 49: 0.0825337926678261, 50: 0.19092392007472064, 51: 0.2515463341682229, 52: 0.32613213524316664, 53: 0.23563493853109554, 54: 0.4110033936434478, 55: 0.38235885945226183, 56: 0.42730548177205857, 57: 0.2815641222074818, 58: 0.5961723489376685, 59: 0.5535344292974106, 60: 0.590918096887046}, 'total_precip_area': {0: 0.0, 1: 0.3874422960619588, 2: 0.3534671039565543, 3: 0.7409486316169753, 4: 0.6940791707728421, 5: 1.050536962899086, 6: 1.1507307395821944, 7: 0.8040717430552817, 8: 1.2714251971087065, 9: 1.238737113112408, 10: 0.874670190928147, 11: 1.0056286133748165, 12: 1.4698900412759783, 13: 1.003917146674298, 14: 0.6760869472842964, 15: 0.46827391738334945, 16: 0.6192899499044445, 17: 0.30060294065641396, 18: 0.41819106385838506, 19: 0.42650150247172797, 20: 0.37798023230687594, 21: 0.13500750520245205, 22: 0.14298402753104766, 23: 0.15948345822813867, 24: 0.18379226168252366, 25: 0.06000665823151941, 26: 0.1208072573649105, 27: 0.1350174494403403, 28: 0.11033956479303804, 29: 0.06970661157888385, 30: 0.16430003817188624, 31: 0.20396331622388164, 32: 0.2434592812259675, 33: 0.2349487648431431, 34: 0.2746628172448139, 35: 0.2223445586696201, 36: 0.32000417926146, 37: 0.2856880861377167, 38: 0.3563173295637319, 39: 0.2961985136805571, 40: 0.39572744360892087, 41: 0.25261863325235423, 42: 0.3480723202390621, 43: 0.33450860690381784, 44: 0.40810079238884994, 45: 0.2246481215656477, 46: 0.322536138199169, 47: 0.26302935652500986, 48: 0.26877015748360655, 49: 0.10435240067851569, 50: 0.23626983714582017, 51: 0.2921901873852093, 52: 0.3573174778311772, 53: 0.2690948499009291, 54: 0.43485595266861593, 55: 0.3857033635552736, 56: 0.45008050524046406, 57: 0.31559742112580763, 58: 0.5897464914238096, 59: 0.5591084829706243, 60: 0.6031174118554716}}
As you can see, the dataframes are related to each other. The first dataframe contains 6-hourly data from a forecast model that goes out 15 days for 4 different variables. The second dataframe contains 24-hourly data that describes the change in the value of the specific variable in df_vals from 24 hours ago (the delta). As such, I want to plot a line graph for the actual values on the y1 axis, and a bar graph to show the change in 24 hours on the y2 axis.
I have been able to plot the values from df_vals as a simple line graph and make it interactive so the user can switch between between different dataframes, but also different variables within the dataframe. Code for that here:
app = JupyterDash(__name__)
cols=df_vals['corn'].columns[1:]
app.layout = html.Div([
html.Div([
html.Div([
dcc.Dropdown(
id='variables',
options=[{'label': i, 'value': i} for i in cols],
value='2m_temp_prod'
)
]),
html.Div([
dcc.Dropdown(
id='crop_dropdown',
options=[{'label': i, 'value': i} for i in df_vals.keys()],
value='corn'
)
]),
dcc.Graph(id='plot')])
])
#app.callback(
Output('crop-dropdown', 'options'),
[Input('variables', 'value')])
def update_crop_dropdown(crop):
return [{'label': i, 'value': i} for i in df_vals[crop]]
#app.callback(
Output('plot', 'figure'),
[Input('crop_dropdown', 'value'),
Input('variables', 'value')])
def update_graph(crops, variable_name):
if crops:
return go.Figure(data=go.Scatter(x=df_vals[crops]['time'], y=df_vals[crops][variable_name], mode='lines'))
else:
return go.Figure(data=[])
app.run_server(mode='inline')
I want the similar column names in df_vals and df_deltas to be plotted together. For example, the column name '2m_temp_prod' in df_vals and '2m_temp_24hdelta_prod' in df_deltas would be plotted together. The same idea applies for the other 3 columns.
I am confused how I let the app know that I want to update both the first and second y-axis, but only provide one dropdown to do so.
An example plot is provided where I was able to accomplish this task, but as a stand alone, non-interactive plot. The purpose of the example is to make sure what I am trying to achieve is clear.
I've prepared a setup using Dash that should take all your considerations into account.
You can select crop type ['corn', 'soybeans', 'winterwheat', 'springwheat']
You can select which columns to display on the primary y-axis for each croptype in df_vals.
And you can select which columns to display on the secondary y-axis for each croptype in df_deltas.
Since the datasets in df_vals and df_deltas are displayed on the primary and secondary y-axis respectively, they are not part of the menu selections. The buttons provided should do be sufficient if I'm understanding your question correctly.
Plot 1: Crop=Corn, df_vals=2m_temp_prod, df_deltas=2m_prod_24hdelta_prod
Plot 2: Crop=Soybeans, df_vals=total_precip_prod, df_deltas=total_precip_24hdelta_area
Complete code:
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State, ClientsideFunction
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
import numpy as np
from plotly.subplots import make_subplots
from pandas import Timestamp
# data
df_vals = {'corn': pd.DataFrame({'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-20 06:00:00'), 2: Timestamp('2020-09-20 12:00:00'), 3: Timestamp('2020-09-20 18:00:00'), 4: Timestamp('2020-09-21 00:00:00'), 5: Timestamp('2020-09-21 06:00:00'), 6: Timestamp('2020-09-21 12:00:00'), 7: Timestamp('2020-09-21 18:00:00'), 8: Timestamp('2020-09-22 00:00:00'), 9: Timestamp('2020-09-22 06:00:00'), 10: Timestamp('2020-09-22 12:00:00'), 11: Timestamp('2020-09-22 18:00:00'), 12: Timestamp('2020-09-23 00:00:00'), 13: Timestamp('2020-09-23 06:00:00'), 14: Timestamp('2020-09-23 12:00:00'), 15: Timestamp('2020-09-23 18:00:00'), 16: Timestamp('2020-09-24 00:00:00'), 17: Timestamp('2020-09-24 06:00:00'), 18: Timestamp('2020-09-24 12:00:00'), 19: Timestamp('2020-09-24 18:00:00'), 20: Timestamp('2020-09-25 00:00:00'), 21: Timestamp('2020-09-25 06:00:00'), 22: Timestamp('2020-09-25 12:00:00'), 23: Timestamp('2020-09-25 18:00:00'), 24: Timestamp('2020-09-26 00:00:00'), 25: Timestamp('2020-09-26 06:00:00'), 26: Timestamp('2020-09-26 12:00:00'), 27: Timestamp('2020-09-26 18:00:00'), 28: Timestamp('2020-09-27 00:00:00'), 29: Timestamp('2020-09-27 06:00:00'), 30: Timestamp('2020-09-27 12:00:00'), 31: Timestamp('2020-09-27 18:00:00'), 32: Timestamp('2020-09-28 00:00:00'), 33: Timestamp('2020-09-28 06:00:00'), 34: Timestamp('2020-09-28 12:00:00'), 35: Timestamp('2020-09-28 18:00:00'), 36: Timestamp('2020-09-29 00:00:00'), 37: Timestamp('2020-09-29 06:00:00'), 38: Timestamp('2020-09-29 12:00:00'), 39: Timestamp('2020-09-29 18:00:00'), 40: Timestamp('2020-09-30 00:00:00'), 41: Timestamp('2020-09-30 06:00:00'), 42: Timestamp('2020-09-30 12:00:00'), 43: Timestamp('2020-09-30 18:00:00'), 44: Timestamp('2020-10-01 00:00:00'), 45: Timestamp('2020-10-01 06:00:00'), 46: Timestamp('2020-10-01 12:00:00'), 47: Timestamp('2020-10-01 18:00:00'), 48: Timestamp('2020-10-02 00:00:00'), 49: Timestamp('2020-10-02 06:00:00'), 50: Timestamp('2020-10-02 12:00:00'), 51: Timestamp('2020-10-02 18:00:00'), 52: Timestamp('2020-10-03 00:00:00'), 53: Timestamp('2020-10-03 06:00:00'), 54: Timestamp('2020-10-03 12:00:00'), 55: Timestamp('2020-10-03 18:00:00'), 56: Timestamp('2020-10-04 00:00:00'), 57: Timestamp('2020-10-04 06:00:00'), 58: Timestamp('2020-10-04 12:00:00'), 59: Timestamp('2020-10-04 18:00:00'), 60: Timestamp('2020-10-05 00:00:00')}, '2m_temp_prod': {0: 299.346777266481, 1: 294.0395117035824, 2: 292.9592738522704, 3: 301.3180464757325, 4: 300.62356652087135, 5: 295.3372490145483, 6: 293.70255086586366, 7: 301.74835531223954, 8: 300.63094530701716, 9: 295.19553201608596, 10: 293.61613871139605, 11: 302.1631229776387, 12: 300.9153730135594, 13: 295.15968865817507, 14: 293.4150837111033, 15: 301.41874976967574, 16: 300.2591302298784, 17: 294.35648855874496, 18: 292.60893943975975, 19: 300.69793228145136, 20: 299.21051997362474, 21: 293.03793188187217, 22: 290.7864320360987, 23: 298.83351270175194, 24: 297.3528693941268, 25: 291.19369512544074, 26: 289.09521928196455, 27: 297.6441935083638, 28: 296.7296754953959, 29: 290.9897312443865, 30: 289.32126319033677, 31: 298.7574828448876, 32: 298.12901959834915, 33: 292.5899489699376, 34: 290.89859256270717, 35: 300.43330551865887, 36: 299.4213926560918, 37: 293.6473005538043, 38: 291.78953606425546, 39: 300.96031965551913, 40: 299.8616920790521, 41: 293.9476866306047, 42: 292.00448821963647, 43: 301.2204786022793, 44: 300.1545728526989, 45: 294.30968496479613, 46: 292.3264718663577, 47: 301.6422264431679, 48: 300.6382119018213, 49: 294.9066383821488, 50: 292.9206232319892, 51: 302.28532470110673, 52: 301.118337633183, 53: 295.580361365072, 54: 293.5071232611944, 55: 302.4993713158521, 56: 301.1771411161832, 57: 295.87429820309063, 58: 293.8387870006977, 59: 302.3844741742686, 60: 300.92076629492163}, '2m_temp_area': {0: 299.79923425166567, 1: 294.4433522483179, 2: 293.1829305163771, 3: 301.76751592578194, 4: 300.97964970416314, 5: 295.58816311209137, 6: 293.944118250348, 7: 302.20448177813586, 8: 301.0782745090335, 9: 295.36833103932497, 10: 293.7448705435944, 11: 302.2414068531493, 12: 300.97154909133104, 13: 295.09171791304016, 14: 293.29872586829305, 15: 301.63398450993316, 16: 300.39729350367077, 17: 294.4765532352668, 18: 292.6348308697987, 19: 300.7196467769189, 20: 299.2413397435659, 21: 293.1571305856655, 22: 290.9298399188299, 23: 299.0361571649767, 24: 297.6784967259681, 25: 291.6225850820778, 26: 289.5307387179231, 27: 298.1675193109706, 28: 297.2851193736129, 29: 291.6047803054954, 30: 289.8725165929944, 31: 299.2259541567033, 32: 298.5645974740215, 33: 292.9844928941141, 34: 291.16692080415885, 35: 300.6640689360243, 36: 299.63105315816887, 37: 293.7973759485704, 38: 291.8748714049971, 39: 301.07558737962773, 40: 299.9769492884242, 41: 294.06556034682825, 42: 292.0989825175383, 43: 301.3224275326778, 44: 300.2429318269633, 45: 294.4016189433437, 46: 292.39193470959646, 47: 301.691478296914, 48: 300.693790365317, 49: 294.95729295702677, 50: 292.94815058578274, 51: 302.1922658603409, 52: 301.01878139937173, 53: 295.47413908911483, 54: 293.41960123543845, 55: 302.34370446075303, 56: 301.05227283650527, 57: 295.72013507339597, 58: 293.6867376233815, 59: 302.19133411599717, 60: 300.81799340051333}, 'total_precip_prod': {0: 0.0, 1: 0.19106968995229195, 2: 0.1557650112297922, 3: 0.42176822127339436, 4: 0.3635719686978164, 5: 0.624238127750141, 6: 0.7363658332723646, 7: 0.6637602529624484, 8: 0.8303668371501847, 9: 0.9645353650727154, 10: 0.9080621631493283, 11: 0.7407109615632305, 12: 1.2188155467167008, 13: 1.4934816133864144, 14: 0.8673540204216558, 15: 0.4536446670226782, 16: 0.5838893851141779, 17: 0.28997017910749373, 18: 0.396575809101194, 19: 0.3017425722928852, 20: 0.2600037974259506, 21: 0.10090977019496428, 22: 0.1120663639424084, 23: 0.11820662603244433, 24: 0.11351422163952127, 25: 0.0354462928350286, 26: 0.08973231687769569, 27: 0.11707786940722151, 28: 0.07097838855834729, 29: 0.03411315769121203, 30: 0.09051350215127027, 31: 0.11314915610119855, 32: 0.14753233086329737, 33: 0.1845242398275337, 34: 0.2518174918229658, 35: 0.17011312187047897, 36: 0.2749232044764252, 37: 0.2972017724997405, 38: 0.3552475267861772, 39: 0.27943871692889416, 40: 0.31981605728486306, 41: 0.19735958986038185, 42: 0.2806487307465678, 43: 0.2830918175327961, 44: 0.3288561686915008, 45: 0.1744303376099874, 46: 0.30609645936303326, 47: 0.21925050665230758, 48: 0.20281755796768022, 49: 0.06620804878284778, 50: 0.17684099550656973, 51: 0.19853145136367492, 52: 0.2563854573673008, 53: 0.24201876323456878, 54: 0.41939251801295807, 55: 0.36402178359658277, 56: 0.37120895070913373, 57: 0.281792653746079, 58: 0.5868870658107626, 59: 0.4927117680726645, 60: 0.5223743530274055}, 'total_precip_area': {0: 0.0, 1: 0.2869521154242563, 2: 0.21660564402622137, 3: 0.4856913604264373, 4: 0.5011640941544891, 5: 0.7562519498474788, 6: 0.7839821183163472, 7: 0.5862595162666312, 8: 0.8974715199444484, 9: 0.9610750641626696, 10: 0.7601911282379429, 11: 0.793906823631679, 12: 1.2383530336279156, 13: 1.0049543280590305, 14: 0.6820852351772055, 15: 0.442434189821785, 16: 0.6589929874909368, 17: 0.32723035127408134, 18: 0.4144798865599254, 19: 0.368782410513661, 20: 0.36137157927969976, 21: 0.1635789497829729, 22: 0.16409778482825316, 23: 0.16768421904602382, 24: 0.1808376986943956, 25: 0.058153150056705245, 26: 0.1269072928484502, 27: 0.14562713451681256, 28: 0.11216762073388148, 29: 0.062161294677683845, 30: 0.13994272590442386, 31: 0.1662560753829063, 32: 0.20624170840852787, 33: 0.216927525300605, 34: 0.26804054777036235, 35: 0.20587583527311556, 36: 0.297848309035157, 37: 0.2836083045675833, 38: 0.33686407205543173, 39: 0.276288892940258, 40: 0.3569925386418611, 41: 0.2258142673918131, 42: 0.30458249604018844, 43: 0.2882872466330577, 44: 0.36861110970625127, 45: 0.1945528585156628, 46: 0.2996099093542968, 47: 0.2370841370242934, 48: 0.24429531409451738, 49: 0.09534765734716107, 50: 0.19399822393477686, 51: 0.24311804406545334, 52: 0.3062432174815857, 53: 0.27031885272433986, 54: 0.40763842665734645, 55: 0.3546261915245615, 56: 0.4085152708749088, 57: 0.30056439476727764, 58: 0.5493651343645845, 59: 0.4937984226857489, 60: 0.531137675746978}}),
'soybeans':pd.DataFrame({'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-20 06:00:00'), 2: Timestamp('2020-09-20 12:00:00'), 3: Timestamp('2020-09-20 18:00:00'), 4: Timestamp('2020-09-21 00:00:00'), 5: Timestamp('2020-09-21 06:00:00'), 6: Timestamp('2020-09-21 12:00:00'), 7: Timestamp('2020-09-21 18:00:00'), 8: Timestamp('2020-09-22 00:00:00'), 9: Timestamp('2020-09-22 06:00:00'), 10: Timestamp('2020-09-22 12:00:00'), 11: Timestamp('2020-09-22 18:00:00'), 12: Timestamp('2020-09-23 00:00:00'), 13: Timestamp('2020-09-23 06:00:00'), 14: Timestamp('2020-09-23 12:00:00'), 15: Timestamp('2020-09-23 18:00:00'), 16: Timestamp('2020-09-24 00:00:00'), 17: Timestamp('2020-09-24 06:00:00'), 18: Timestamp('2020-09-24 12:00:00'), 19: Timestamp('2020-09-24 18:00:00'), 20: Timestamp('2020-09-25 00:00:00'), 21: Timestamp('2020-09-25 06:00:00'), 22: Timestamp('2020-09-25 12:00:00'), 23: Timestamp('2020-09-25 18:00:00'), 24: Timestamp('2020-09-26 00:00:00'), 25: Timestamp('2020-09-26 06:00:00'), 26: Timestamp('2020-09-26 12:00:00'), 27: Timestamp('2020-09-26 18:00:00'), 28: Timestamp('2020-09-27 00:00:00'), 29: Timestamp('2020-09-27 06:00:00'), 30: Timestamp('2020-09-27 12:00:00'), 31: Timestamp('2020-09-27 18:00:00'), 32: Timestamp('2020-09-28 00:00:00'), 33: Timestamp('2020-09-28 06:00:00'), 34: Timestamp('2020-09-28 12:00:00'), 35: Timestamp('2020-09-28 18:00:00'), 36: Timestamp('2020-09-29 00:00:00'), 37: Timestamp('2020-09-29 06:00:00'), 38: Timestamp('2020-09-29 12:00:00'), 39: Timestamp('2020-09-29 18:00:00'), 40: Timestamp('2020-09-30 00:00:00'), 41: Timestamp('2020-09-30 06:00:00'), 42: Timestamp('2020-09-30 12:00:00'), 43: Timestamp('2020-09-30 18:00:00'), 44: Timestamp('2020-10-01 00:00:00'), 45: Timestamp('2020-10-01 06:00:00'), 46: Timestamp('2020-10-01 12:00:00'), 47: Timestamp('2020-10-01 18:00:00'), 48: Timestamp('2020-10-02 00:00:00'), 49: Timestamp('2020-10-02 06:00:00'), 50: Timestamp('2020-10-02 12:00:00'), 51: Timestamp('2020-10-02 18:00:00'), 52: Timestamp('2020-10-03 00:00:00'), 53: Timestamp('2020-10-03 06:00:00'), 54: Timestamp('2020-10-03 12:00:00'), 55: Timestamp('2020-10-03 18:00:00'), 56: Timestamp('2020-10-04 00:00:00'), 57: Timestamp('2020-10-04 06:00:00'), 58: Timestamp('2020-10-04 12:00:00'), 59: Timestamp('2020-10-04 18:00:00'), 60: Timestamp('2020-10-05 00:00:00')}, '2m_temp_prod': {0: 299.4681144428137, 1: 294.4593329079582, 2: 293.54097959722486, 3: 301.26850218370805, 4: 300.4714986353434, 5: 295.6384695410705, 6: 294.2218375466852, 7: 301.7426496375417, 8: 300.4512511041961, 9: 295.3463775610583, 10: 294.00147574962756, 11: 302.04475406462933, 12: 300.7236658622478, 13: 295.24334771914255, 14: 293.7074020631231, 15: 301.7624504225804, 16: 300.4964931687989, 17: 294.75437504400884, 18: 293.1177542350017, 19: 301.0425274988938, 20: 299.46808452778663, 21: 293.52076913001173, 22: 291.3670613175431, 23: 299.3793965992101, 24: 297.7263093286383, 25: 291.7095202331552, 26: 289.6578323905783, 27: 298.15977187729777, 28: 296.9884342085861, 29: 291.43100183368114, 30: 289.77806100941024, 31: 299.00450242361666, 32: 298.15618404166, 33: 292.73800154126627, 34: 291.07457059787265, 35: 300.5683567053199, 36: 299.3708029435806, 37: 293.67890398828234, 38: 291.954942552734, 39: 301.1051505527471, 40: 299.7999782544342, 41: 294.0840368998602, 42: 292.31118300204673, 43: 301.3976538328202, 44: 300.07823191309984, 45: 294.45023381986016, 46: 292.62675527729584, 47: 301.7900197148812, 48: 300.5590082986463, 49: 294.9896801203776, 50: 293.1241761449157, 51: 302.36329836985607, 52: 300.97122596719737, 53: 295.59237808816266, 54: 293.67785157848175, 55: 302.64353699591527, 56: 301.1035750112269, 57: 295.9278212054364, 58: 294.0324357819224, 59: 302.4832018963981, 60: 300.8427400317463}, '2m_temp_area': {0: 299.4903718737801, 1: 294.2924835295078, 2: 293.30135473218274, 3: 301.3528640298525, 4: 300.5939881510472, 5: 295.70744811622785, 6: 294.2521907610824, 7: 301.7100774359669, 8: 300.44506163325167, 9: 295.238423311095, 10: 293.8509583022139, 11: 301.6235104014979, 12: 300.38743669809435, 13: 294.8808530776623, 14: 293.33325543225476, 15: 301.4149251854487, 16: 300.1796642776425, 17: 294.39239805130165, 18: 292.66981534530925, 19: 300.5419360845211, 20: 299.0580229759339, 21: 293.0690025289753, 22: 290.9169097601789, 23: 298.92199569749033, 24: 297.4673382419046, 25: 291.4599993026732, 26: 289.452388873785, 27: 297.98953222540143, 28: 296.98633186869347, 29: 291.4589865652578, 30: 289.8288914753008, 31: 298.94272407170354, 32: 298.23250648648224, 33: 292.83814534455996, 34: 291.11886718509857, 35: 300.40881750455577, 36: 299.31958687273186, 37: 293.607006890439, 38: 291.81444341023143, 39: 300.8265637430714, 40: 299.6454967052681, 41: 293.9106555122605, 42: 292.1023114379392, 43: 301.12502226064896, 44: 299.91897396641514, 45: 294.27870082533354, 46: 292.4308511036389, 47: 301.51583391071864, 48: 300.39429493794523, 49: 294.825486042682, 50: 292.93778771753085, 51: 302.0071005199438, 52: 300.74239613744925, 53: 295.38249105817647, 54: 293.45710875169135, 55: 302.25559951138456, 56: 300.8353769061142, 57: 295.6525398583873, 58: 293.7632451527435, 59: 302.0551372494585, 60: 300.54910101224283}, 'total_precip_prod': {0: 0.0, 1: 0.4344171852327147, 2: 0.4351379346608463, 3: 0.9091314020100694, 4: 0.7838464939768977, 5: 1.0303630570555136, 6: 1.168093427310163, 7: 0.8635649194545677, 8: 1.180842617204555, 9: 1.0926651254176625, 10: 0.8283334033945278, 11: 0.8444548557416743, 12: 1.3230640130302405, 13: 1.2194759096975982, 14: 0.7328129706460099, 15: 0.47450659159057845, 16: 0.5808363103089833, 17: 0.28338951318682726, 18: 0.39698079867625957, 19: 0.37495920568614616, 20: 0.33256622409109604, 21: 0.09931783596352596, 22: 0.1293731106563246, 23: 0.14798406577292791, 24: 0.18096669978617885, 25: 0.05295233242627148, 26: 0.11851136022355316, 27: 0.12519627617895362, 28: 0.09146508542790638, 29: 0.04829302677559146, 30: 0.11041590211762027, 31: 0.14573868900912046, 32: 0.18215252985155908, 33: 0.18280908457277276, 34: 0.2248243307367063, 35: 0.17947363978224146, 36: 0.2924242956937053, 37: 0.2586747544378083, 38: 0.33937087683999867, 39: 0.2928781027298378, 40: 0.3935333755687144, 41: 0.2365752557775799, 42: 0.3215975487263272, 43: 0.32446922789494864, 44: 0.40520923133523234, 45: 0.18850232039108583, 46: 0.3206818541123286, 47: 0.2592275400999963, 48: 0.2610206474998234, 49: 0.0825337926678261, 50: 0.19092392007472064, 51: 0.2515463341682229, 52: 0.32613213524316664, 53: 0.23563493853109554, 54: 0.4110033936434478, 55: 0.38235885945226183, 56: 0.42730548177205857, 57: 0.2815641222074818, 58: 0.5961723489376685, 59: 0.5535344292974106, 60: 0.590918096887046}, 'total_precip_area': {0: 0.0, 1: 0.3874422960619588, 2: 0.3534671039565543, 3: 0.7409486316169753, 4: 0.6940791707728421, 5: 1.050536962899086, 6: 1.1507307395821944, 7: 0.8040717430552817, 8: 1.2714251971087065, 9: 1.238737113112408, 10: 0.874670190928147, 11: 1.0056286133748165, 12: 1.4698900412759783, 13: 1.003917146674298, 14: 0.6760869472842964, 15: 0.46827391738334945, 16: 0.6192899499044445, 17: 0.30060294065641396, 18: 0.41819106385838506, 19: 0.42650150247172797, 20: 0.37798023230687594, 21: 0.13500750520245205, 22: 0.14298402753104766, 23: 0.15948345822813867, 24: 0.18379226168252366, 25: 0.06000665823151941, 26: 0.1208072573649105, 27: 0.1350174494403403, 28: 0.11033956479303804, 29: 0.06970661157888385, 30: 0.16430003817188624, 31: 0.20396331622388164, 32: 0.2434592812259675, 33: 0.2349487648431431, 34: 0.2746628172448139, 35: 0.2223445586696201, 36: 0.32000417926146, 37: 0.2856880861377167, 38: 0.3563173295637319, 39: 0.2961985136805571, 40: 0.39572744360892087, 41: 0.25261863325235423, 42: 0.3480723202390621, 43: 0.33450860690381784, 44: 0.40810079238884994, 45: 0.2246481215656477, 46: 0.322536138199169, 47: 0.26302935652500986, 48: 0.26877015748360655, 49: 0.10435240067851569, 50: 0.23626983714582017, 51: 0.2921901873852093, 52: 0.3573174778311772, 53: 0.2690948499009291, 54: 0.43485595266861593, 55: 0.3857033635552736, 56: 0.45008050524046406, 57: 0.31559742112580763, 58: 0.5897464914238096, 59: 0.5591084829706243, 60: 0.6031174118554716}})}
df_deltas = {'corn': pd.DataFrame({'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-21 00:00:00'), 2: Timestamp('2020-09-22 00:00:00'), 3: Timestamp('2020-09-23 00:00:00'), 4: Timestamp('2020-09-24 00:00:00'), 5: Timestamp('2020-09-25 00:00:00'), 6: Timestamp('2020-09-26 00:00:00'), 7: Timestamp('2020-09-27 00:00:00'), 8: Timestamp('2020-09-28 00:00:00'), 9: Timestamp('2020-09-29 00:00:00'), 10: Timestamp('2020-09-30 00:00:00'), 11: Timestamp('2020-10-01 00:00:00'), 12: Timestamp('2020-10-02 00:00:00'), 13: Timestamp('2020-10-03 00:00:00'), 14: Timestamp('2020-10-04 00:00:00'), 15: Timestamp('2020-10-05 00:00:00')}, '2m_temp_24hdelta_prod': {0: -0.33056551535638423, 1: -0.06352654836543402, 2: -0.276225357129493, 3: -0.7788107233764435, 4: -0.12182345812042986, 5: 0.4844870528180379, 6: -0.7795401737865336, 7: -1.3736563255451468, 8: -0.7925949910485673, 9: -0.6084065411465024, 10: -0.6532739602739552, 11: -0.5334182358612463, 12: -0.3214601096392593, 13: 0.005672046711691303, 14: 0.294874775721931, 15: 0.0}, '2m_temp_24hdelta_area': {0: -0.2942234811255382, 1: -0.06695257736951654, 2: -0.2382484006633631, 3: -0.747444491858053, 4: -0.008792915536150941, 5: 0.25843822574146474, 6: -0.8653840134962728, 7: -1.3306357244341183, 8: -0.77807373911064, 9: -0.6101878063800394, 10: -0.6602593986352374, 11: -0.5377113682438022, 12: -0.3489377042704134, 13: -0.12997682663355925, 14: 0.08031697715852158, 15: 0.0}, 'total_precip_24hdelta_prod': {0: -1.4417379991236094, 1: -3.24276961794111, 2: -1.9549294932106442, 3: 1.0805492310759035, 4: -2.8572102912370547, 5: -0.6036102993252254, 6: 0.068152851219848, 7: -0.37760433118519804, 8: -0.2182212103223477, 9: 0.14385291749053375, 10: -0.09611101762074367, 11: -0.44170367466228744, 12: -0.9926136091953488, 13: -0.26638156415402015, 14: -0.0435076228174961, 15: 0.0}, 'total_precip_24hdelta_area': {0: -0.8969484573159082, 1: -2.002193332285897, 2: -1.3265684841453602, 3: 0.5232970017014504, 4: -1.9744318826572878, 5: -0.4756557183821801, 6: 0.09401319045911638, 7: -0.2480262049680989, 8: -0.16164720991353132, 9: 0.12918646107950632, 10: -0.025385234499880355, 11: -0.37094005589542123, 12: -0.7621485373750772, 13: -0.21974376949919702, 14: -0.06387744429696394, 15: 0.0}}),
'soybeans':pd.DataFrame({'time': {0: Timestamp('2020-09-20 00:00:00'), 1: Timestamp('2020-09-21 00:00:00'), 2: Timestamp('2020-09-22 00:00:00'), 3: Timestamp('2020-09-23 00:00:00'), 4: Timestamp('2020-09-24 00:00:00'), 5: Timestamp('2020-09-25 00:00:00'), 6: Timestamp('2020-09-26 00:00:00'), 7: Timestamp('2020-09-27 00:00:00'), 8: Timestamp('2020-09-28 00:00:00'), 9: Timestamp('2020-09-29 00:00:00'), 10: Timestamp('2020-09-30 00:00:00'), 11: Timestamp('2020-10-01 00:00:00'), 12: Timestamp('2020-10-02 00:00:00'), 13: Timestamp('2020-10-03 00:00:00'), 14: Timestamp('2020-10-04 00:00:00'), 15: Timestamp('2020-10-05 00:00:00')}, '2m_temp_24hdelta_prod': {0: -0.41072100441995785, 1: -0.11297222900273206, 2: -0.3134558546203517, 3: -0.7055797268403201, 4: -0.14038170579478787, 5: 0.2625117005223226, 6: -0.7101408224478941, 7: -1.2573241419225902, 8: -0.7906424533010389, 9: -0.661536032998284, 10: -0.6822789549363446, 11: -0.5744259308508103, 12: -0.4206210457410846, 13: -0.08431581171659296, 14: 0.2654961632050054, 15: 0.0}, '2m_temp_24hdelta_area': {0: -0.36772899690348704, 1: -0.05348366544356093, 2: -0.24183851104639115, 3: -0.6953305273210354, 4: -0.045424744173602496, 5: 0.19001500780368247, 6: -0.821935103347599, 7: -1.265563237441084, 8: -0.7328581189116925, 9: -0.6143048783236709, 10: -0.6633462127739507, 11: -0.5190203572096471, 12: -0.39498226425933547, 13: -0.10526773964830015, 14: 0.19258302497068705, 15: 0.0}, 'total_precip_24hdelta_prod': {0: -0.6776698356563055, 1: -1.7516299539550721, 2: -1.270815712012518, 3: 0.5996647217068156, 4: -2.2466045890287396, 5: -0.6706621597096377, 6: 0.06719919310410405, 7: -0.3572052717181586, 8: -0.2512178851633781, 9: 0.1313636195701381, 10: 0.08179245850142033, 11: -0.3036658888458674, 12: -0.8200237460316371, 13: -0.32927495598480583, 14: -0.08189579932441349, 15: 0.0}, 'total_precip_24hdelta_area': {0: -0.7530386453128719, 1: -1.7027556154743797, 2: -1.0907657669920294, 3: 0.429094034006764, 4: -1.9424324271143407, 5: -0.5370968484676638, 6: 0.044404490985521246, 7: -0.30794153270377544, 8: -0.2471378005907392, 9: 0.1212115468745879, 10: 0.07486462848670472, 11: -0.3507116185903665, 12: -0.7247205225779287, 13: -0.25431240691233936, 14: -0.06960793926681186, 15: 0.0}})}
# managed indexes
df_vals['corn'].set_index('time', inplace=True)
df_vals['soybeans'].set_index('time', inplace=True)
df_deltas['corn'].set_index('time', inplace=True)
df_deltas['soybeans'].set_index('time', inplace=True)
# Dash app setup
app = JupyterDash(external_stylesheets=[dbc.themes.SLATE])
# controls
controls = dbc.Card(
[ dbc.FormGroup(
[
dbc.Label("Crop"),
dcc.Dropdown(
id="Crop",
options=[
{"label": col, "value": col} for col in list(df_vals.keys())
],
value=list(df_vals.keys())[0],
clearable=False,
),
]
),
dbc.FormGroup(
[
dbc.Label("df_vals"),
dcc.Dropdown(
id="val_cols",
options=[
{"label": col, "value": col} for col in df_vals[list(df_vals.keys())[0]].columns
],
value=df_vals[list(df_vals.keys())[0]].columns[0],
clearable=False,
),
]
),
dbc.FormGroup(
[
dbc.Label("df_deltas"),
dcc.Dropdown(
id="delta_cols",
options=[
{"label": col, "value": col} for col in df_deltas[list(df_deltas.keys())[0]].columns
],
value=df_deltas[list(df_deltas.keys())[0]].columns[0],
clearable=False
),
]
),
],
body=True,
)
# Layout
app.layout = dbc.Container(
[
html.H1("Plotly Dash"),
html.Hr(),
dbc.Row([
dbc.Col([
dbc.Row([
dbc.Col(controls)
], align="start"),
dbc.Row([
dbc.Col([
html.Br(),
dbc.Row([
dbc.Col([html.Div(id = 'txt1')
])
]),
html.Br(),
dbc.Row([
dbc.Col([html.Div(id = 'txt2')])
])
])
])
],xs = 4)
,
dbc.Col([
dbc.Row([
dbc.Col([html.Div(id = 'plot_title')],)
]),
dbc.Row([
dbc.Col(dcc.Graph(id="crop-graph")),
#dbc.Col(dcc.Graph(id="cluster-graph"))
])
])
],),
],
fluid=True,
)
#app.callback(
Output("crop-graph", "figure"),
[ Input("Crop", "value"),
Input("val_cols", "value"),
Input("delta_cols", "value"),
#Input("cluster-count", "value"),
],
)
def crop_graph(Crop, val, delta):
# plotly figure setup
fig = make_subplots(specs=[[{"secondary_y": True}]])
# traces for df_vals
fig.add_trace(go.Scatter(name=val, x=df_vals[Crop].index, y=df_vals[Crop][val], mode = 'lines'), secondary_y=False,
)
# traces for df_deltas
fig.add_trace(go.Scatter(name=delta, x=df_deltas[Crop].index, y=df_deltas[Crop][delta], mode = 'lines'), secondary_y=True,
)
# aesthetics
fig.update_layout(template="plotly_dark", plot_bgcolor='#272B30', paper_bgcolor='#272B30')
fig.update_layout(margin= {'t':150, 'b':0, 'r': 0, 'l': 0, 'pad': 0})
fig.update_layout(hovermode = 'x')
fig.update_layout(showlegend=True, legend=dict(x=1,y=0.85))
fig.update_layout(title = dict(text = 'Crop: '+ Crop + ', Vals: ' + val + ', Deltas: '+delta))
return(fig)
app.run_server(mode='external', port = 8099)