Home > Enterprise >  How to plot multiple lines from data in a JSON file
How to plot multiple lines from data in a JSON file

Time:10-01

I have a json file containing x and y values (x is the number of steps while y are the values of the loss function). I want to plot this but i am not a programmer by profession so i am having a bit of a hard time unpacking these values first into a dataframe and then plotting it.

[
  {
    "x": [49,99,149,199,249,299,349,399,449,499,549,599,649,699,749,799,849,899,949,999,1049,1099,1149,1199,1249,1299,1349,1399,1449,1499,1549,1599,1649,1699,1749,1799,1849,1899,1949,1999,2049,2099,2149,2199,2249,2299,2349,2399,2449,2499,2549,2599,2649,2699,2749,2799,2849,2899,2949,2999,3049,3099,3149,3199,3249,3299,3349,3399,3449,3499,3549,3599,3649,3699,3749,3799,3849,3899,3949,3999,4049,4099,4149,4199,4249,4299,4349,4399,4449,4499,4549,4599,4649,4699,4749,4799,4849,4899,4949,4999],
    "y": [0.8078442215919495,0.6924954056739807,0.637282133102417,0.5666537284851074,0.4263683557510376,0.29408636689186096,0.22852952778339386,0.19844533503055573,0.19280606508255005,0.16006380319595337,0.18059080839157104,0.27476057410240173,0.16974058747291565,0.13871365785598755,0.15893904864788055,0.11171692609786987,0.09724593907594681,0.1499839723110199,0.17183662950992584,0.1576225459575653,0.12323413789272308,0.12138434499502182,0.09343212097883224,0.10920502990484238,0.11386646330356598,0.09778832644224167,0.14350709319114685,0.13651356101036072,0.15092197060585022,0.09928392618894577,0.12458140403032303,0.10494255274534225,0.10528195649385452,0.13158175349235535,0.0788651630282402,0.09537861496210098,0.08444802463054657,0.09358047693967819,0.11348600685596466,0.11421093344688416,0.13024120032787323,0.09298647195100784,0.10095459222793579,0.12882551550865173,0.06175372377038002,0.11043348163366318,0.07273056358098984,0.07597362995147705,0.07747262716293335,0.10011449456214905,0.09275206923484802,0.11640968173742294,0.07466059923171997,0.06264252215623856,0.10496743023395538,0.12706291675567627,0.07618371397256851,0.05997024476528168,0.0738396868109703,0.07691851258277893,0.04857499152421951,0.08904716372489929,0.09522826969623566,0.0718853548169136,0.06508275866508484,0.0604521669447422,0.0327882245182991,0.043644022196531296,0.0742092877626419,0.06414887309074402,0.05483072251081467,0.06133958697319031,0.05271274596452713,0.051848240196704865,0.04392067342996597,0.07057813555002213,0.051781173795461655,0.19890470802783966,0.046647973358631134,0.04351358115673065,0.06602110713720322,0.05368412658572197,0.046410009264945984,0.05693329870700836,0.05165236443281174,0.03220716118812561,0.05031270533800125,0.05368302762508392,0.048431843519210815,0.055765118449926376,0.03884245455265045,0.05483575165271759,0.027131063863635063,0.042943283915519714,0.03232723847031593,0.043425388634204865,0.03555495664477348,0.05412492901086807,0.0535135492682457,0.03369136527180672],
    "type": "scattergl",
    "name": "train_loss"
  }
]

Multiple curves which I want in the same plot:

[{"x":[553,1107,1661,2215,2769,3323,3877,4431,4985],"y":[0.3421344757080078,0.21656209230422974,0.2768174707889557,0.21082060039043427,0.2538467049598694,0.24818505346775055,0.2378658652305603,0.2655095160007477,0.27708765864372253],"type":"scattergl","name":"val_loss"},{"x":[553,1107,1661,2215,2769,3323,3877,4431,4985],"y":[0.8641330599784851,0.922535240650177,0.9076007604598999,0.9262991547584534,0.9157357811927795,0.9193783402442932,0.9227780699729919,0.9205925464630127,0.9188926815986633],"type":"scattergl","name":"val_acc"},{"x":[49,99,149,199,249,299,349,399,449,499,549,599,649,699,749,799,849,899,949,999,1049,1099,1149,1199,1249,1299,1349,1399,1449,1499,1549,1599,1649,1699,1749,1799,1849,1899,1949,1999,2049,2099,2149,2199,2249,2299,2349,2399,2449,2499,2549,2599,2649,2699,2749,2799,2849,2899,2949,2999,3049,3099,3149,3199,3249,3299,3349,3399,3449,3499,3549,3599,3649,3699,3749,3799,3849,3899,3949,3999,4049,4099,4149,4199,4249,4299,4349,4399,4449,4499,4549,4599,4649,4699,4749,4799,4849,4899,4949,4999],"y":[0.8078442215919495,0.6924954056739807,0.637282133102417,0.5666537284851074,0.4263683557510376,0.29408636689186096,0.22852952778339386,0.19844533503055573,0.19280606508255005,0.16006380319595337,0.18059080839157104,0.27476057410240173,0.16974058747291565,0.13871365785598755,0.15893904864788055,0.11171692609786987,0.09724593907594681,0.1499839723110199,0.17183662950992584,0.1576225459575653,0.12323413789272308,0.12138434499502182,0.09343212097883224,0.10920502990484238,0.11386646330356598,0.09778832644224167,0.14350709319114685,0.13651356101036072,0.15092197060585022,0.09928392618894577,0.12458140403032303,0.10494255274534225,0.10528195649385452,0.13158175349235535,0.0788651630282402,0.09537861496210098,0.08444802463054657,0.09358047693967819,0.11348600685596466,0.11421093344688416,0.13024120032787323,0.09298647195100784,0.10095459222793579,0.12882551550865173,0.06175372377038002,0.11043348163366318,0.07273056358098984,0.07597362995147705,0.07747262716293335,0.10011449456214905,0.09275206923484802,0.11640968173742294,0.07466059923171997,0.06264252215623856,0.10496743023395538,0.12706291675567627,0.07618371397256851,0.05997024476528168,0.0738396868109703,0.07691851258277893,0.04857499152421951,0.08904716372489929,0.09522826969623566,0.0718853548169136,0.06508275866508484,0.0604521669447422,0.0327882245182991,0.043644022196531296,0.0742092877626419,0.06414887309074402,0.05483072251081467,0.06133958697319031,0.05271274596452713,0.051848240196704865,0.04392067342996597,0.07057813555002213,0.051781173795461655,0.19890470802783966,0.046647973358631134,0.04351358115673065,0.06602110713720322,0.05368412658572197,0.046410009264945984,0.05693329870700836,0.05165236443281174,0.03220716118812561,0.05031270533800125,0.05368302762508392,0.048431843519210815,0.055765118449926376,0.03884245455265045,0.05483575165271759,0.027131063863635063,0.042943283915519714,0.03232723847031593,0.043425388634204865,0.03555495664477348,0.05412492901086807,0.0535135492682457,0.03369136527180672],"type":"scattergl","name":"train_loss"},{"x":[49,99,149,199,249,299,349,399,449,499,549,599,649,699,749,799,849,899,949,999,1049,1099,1149,1199,1249,1299,1349,1399,1449,1499,1549,1599,1649,1699,1749,1799,1849,1899,1949,1999,2049,2099,2149,2199,2249,2299,2349,2399,2449,2499,2549,2599,2649,2699,2749,2799,2849,2899,2949,2999,3049,3099,3149,3199,3249,3299,3349,3399,3449,3499,3549,3599,3649,3699,3749,3799,3849,3899,3949,3999,4049,4099,4149,4199,4249,4299,4349,4399,4449,4499,4549,4599,4649,4699,4749,4799,4849,4899,4949,4999],"y":[0.375,0.5637500286102295,0.6387500166893005,0.706250011920929,0.8224999904632568,0.9012500047683716,0.9262499809265137,0.9350000023841858,0.9237499833106995,0.9537500143051147,0.9350000023841858,0.875,0.9212499856948853,0.949999988079071,0.9212499856948853,0.956250011920929,0.9637500047683716,0.9399999976158142,0.9350000023841858,0.9375,0.9549999833106995,0.9574999809265137,0.9642857313156128,0.9549999833106995,0.9574999809265137,0.9537500143051147,0.9462500214576721,0.9437500238418579,0.9437500238418579,0.9624999761581421,0.949999988079071,0.9637500047683716,0.9637500047683716,0.9464285969734192,0.9750000238418579,0.9637500047683716,0.9674999713897705,0.9662500023841858,0.9512500166893005,0.9599999785423279,0.9587500095367432,0.9612500071525574,0.9549999833106995,0.9524999856948853,0.9821428656578064,0.9612500071525574,0.9725000262260437,0.9700000286102295,0.9674999713897705,0.9624999761581421,0.96875,0.949999988079071,0.9700000286102295,0.9787499904632568,0.9662500023841858,0.9642857313156128,0.9725000262260437,0.981249988079071,0.9762499928474426,0.9712499976158142,0.9862499833106995,0.9637500047683716,0.9674999713897705,0.9712499976158142,0.9750000238418579,0.9800000190734863,0.9821428656578064,0.9887499809265137,0.9750000238418579,0.9837499856948853,0.9787499904632568,0.9787499904632568,0.9850000143051147,0.9775000214576721,0.9850000143051147,0.9762499928474426,0.981249988079071,0.9464285969734192,0.9862499833106995,0.9850000143051147,0.9762499928474426,0.9825000166893005,0.9862499833106995,0.9862499833106995,0.9800000190734863,0.9925000071525574,0.9837499856948853,0.9825000166893005,0.9821428656578064,0.9787499904632568,0.9862499833106995,0.9825000166893005,0.9887499809265137,0.9887499809265137,0.9900000095367432,0.9862499833106995,0.9887499809265137,0.9825000166893005,0.9825000166893005,0.9642857313156128],"type":"scattergl","name":"train_acc"}]

The plot should look something like this:

training loss plot-link to picture

CodePudding user response:

  • Given the json file with multiple names
  • pandas uses matplotlib as the default plotting backend. In this case, it is easier to use seaborn, a high-level API for matplotlib.
    • enter image description here

      • Alternative with enter image description here

        display(df.head())

              x         y       type      name
        0   553  0.342134  scattergl  val_loss
        1  1107  0.216562  scattergl  val_loss
        2  1661  0.276817  scattergl  val_loss
        3  2215  0.210821  scattergl  val_loss
        4  2769  0.253847  scattergl  val_loss
        

        display(df.tail())

                x         y       type       name
        213  4799   0.98625  scattergl  train_acc
        214  4849   0.98875  scattergl  train_acc
        215  4899    0.9825  scattergl  train_acc
        216  4949    0.9825  scattergl  train_acc
        217  4999  0.964286  scattergl  train_acc
        

        test.json

        [{
                "x": [553, 1107, 1661, 2215, 2769, 3323, 3877, 4431, 4985],
                "y": [0.3421344757080078, 0.21656209230422974, 0.2768174707889557, 0.21082060039043427, 0.2538467049598694, 0.24818505346775055, 0.2378658652305603, 0.2655095160007477, 0.27708765864372253],
                "type": "scattergl",
                "name": "val_loss"
            }, {
                "x": [553, 1107, 1661, 2215, 2769, 3323, 3877, 4431, 4985],
                "y": [0.8641330599784851, 0.922535240650177, 0.9076007604598999, 0.9262991547584534, 0.9157357811927795, 0.9193783402442932, 0.9227780699729919, 0.9205925464630127, 0.9188926815986633],
                "type": "scattergl",
                "name": "val_acc"
            }, {
                "x": [49, 99, 149, 199, 249, 299, 349, 399, 449, 499, 549, 599, 649, 699, 749, 799, 849, 899, 949, 999, 1049, 1099, 1149, 1199, 1249, 1299, 1349, 1399, 1449, 1499, 1549, 1599, 1649, 1699, 1749, 1799, 1849, 1899, 1949, 1999, 2049, 2099, 2149, 2199, 2249, 2299, 2349, 2399, 2449, 2499, 2549, 2599, 2649, 2699, 2749, 2799, 2849, 2899, 2949, 2999, 3049, 3099, 3149, 3199, 3249, 3299, 3349, 3399, 3449, 3499, 3549, 3599, 3649, 3699, 3749, 3799, 3849, 3899, 3949, 3999, 4049, 4099, 4149, 4199, 4249, 4299, 4349, 4399, 4449, 4499, 4549, 4599, 4649, 4699, 4749, 4799, 4849, 4899, 4949, 4999],
                "y": [0.8078442215919495, 0.6924954056739807, 0.637282133102417, 0.5666537284851074, 0.4263683557510376, 0.29408636689186096, 0.22852952778339386, 0.19844533503055573, 0.19280606508255005, 0.16006380319595337, 0.18059080839157104, 0.27476057410240173, 0.16974058747291565, 0.13871365785598755, 0.15893904864788055, 0.11171692609786987, 0.09724593907594681, 0.1499839723110199, 0.17183662950992584, 0.1576225459575653, 0.12323413789272308, 0.12138434499502182, 0.09343212097883224, 0.10920502990484238, 0.11386646330356598, 0.09778832644224167, 0.14350709319114685, 0.13651356101036072, 0.15092197060585022, 0.09928392618894577, 0.12458140403032303, 0.10494255274534225, 0.10528195649385452, 0.13158175349235535, 0.0788651630282402, 0.09537861496210098, 0.08444802463054657, 0.09358047693967819, 0.11348600685596466, 0.11421093344688416, 0.13024120032787323, 0.09298647195100784, 0.10095459222793579, 0.12882551550865173, 0.06175372377038002, 0.11043348163366318, 0.07273056358098984, 0.07597362995147705, 0.07747262716293335, 0.10011449456214905, 0.09275206923484802, 0.11640968173742294, 0.07466059923171997, 0.06264252215623856, 0.10496743023395538, 0.12706291675567627, 0.07618371397256851, 0.05997024476528168, 0.0738396868109703, 0.07691851258277893, 0.04857499152421951, 0.08904716372489929, 0.09522826969623566, 0.0718853548169136, 0.06508275866508484, 0.0604521669447422, 0.0327882245182991, 0.043644022196531296, 0.0742092877626419, 0.06414887309074402, 0.05483072251081467, 0.06133958697319031, 0.05271274596452713, 0.051848240196704865, 0.04392067342996597, 0.07057813555002213, 0.051781173795461655, 0.19890470802783966, 0.046647973358631134, 0.04351358115673065, 0.06602110713720322, 0.05368412658572197, 0.046410009264945984, 0.05693329870700836, 0.05165236443281174, 0.03220716118812561, 0.05031270533800125, 0.05368302762508392, 0.048431843519210815, 0.055765118449926376, 0.03884245455265045, 0.05483575165271759, 0.027131063863635063, 0.042943283915519714, 0.03232723847031593, 0.043425388634204865, 0.03555495664477348, 0.05412492901086807, 0.0535135492682457, 0.03369136527180672],
                "type": "scattergl",
                "name": "train_loss"
            }, {
                "x": [49, 99, 149, 199, 249, 299, 349, 399, 449, 499, 549, 599, 649, 699, 749, 799, 849, 899, 949, 999, 1049, 1099, 1149, 1199, 1249, 1299, 1349, 1399, 1449, 1499, 1549, 1599, 1649, 1699, 1749, 1799, 1849, 1899, 1949, 1999, 2049, 2099, 2149, 2199, 2249, 2299, 2349, 2399, 2449, 2499, 2549, 2599, 2649, 2699, 2749, 2799, 2849, 2899, 2949, 2999, 3049, 3099, 3149, 3199, 3249, 3299, 3349, 3399, 3449, 3499, 3549, 3599, 3649, 3699, 3749, 3799, 3849, 3899, 3949, 3999, 4049, 4099, 4149, 4199, 4249, 4299, 4349, 4399, 4449, 4499, 4549, 4599, 4649, 4699, 4749, 4799, 4849, 4899, 4949, 4999],
                "y": [0.375, 0.5637500286102295, 0.6387500166893005, 0.706250011920929, 0.8224999904632568, 0.9012500047683716, 0.9262499809265137, 0.9350000023841858, 0.9237499833106995, 0.9537500143051147, 0.9350000023841858, 0.875, 0.9212499856948853, 0.949999988079071, 0.9212499856948853, 0.956250011920929, 0.9637500047683716, 0.9399999976158142, 0.9350000023841858, 0.9375, 0.9549999833106995, 0.9574999809265137, 0.9642857313156128, 0.9549999833106995, 0.9574999809265137, 0.9537500143051147, 0.9462500214576721, 0.9437500238418579, 0.9437500238418579, 0.9624999761581421, 0.949999988079071, 0.9637500047683716, 0.9637500047683716, 0.9464285969734192, 0.9750000238418579, 0.9637500047683716, 0.9674999713897705, 0.9662500023841858, 0.9512500166893005, 0.9599999785423279, 0.9587500095367432, 0.9612500071525574, 0.9549999833106995, 0.9524999856948853, 0.9821428656578064, 0.9612500071525574, 0.9725000262260437, 0.9700000286102295, 0.9674999713897705, 0.9624999761581421, 0.96875, 0.949999988079071, 0.9700000286102295, 0.9787499904632568, 0.9662500023841858, 0.9642857313156128, 0.9725000262260437, 0.981249988079071, 0.9762499928474426, 0.9712499976158142, 0.9862499833106995, 0.9637500047683716, 0.9674999713897705, 0.9712499976158142, 0.9750000238418579, 0.9800000190734863, 0.9821428656578064, 0.9887499809265137, 0.9750000238418579, 0.9837499856948853, 0.9787499904632568, 0.9787499904632568, 0.9850000143051147, 0.9775000214576721, 0.9850000143051147, 0.9762499928474426, 0.981249988079071, 0.9464285969734192, 0.9862499833106995, 0.9850000143051147, 0.9762499928474426, 0.9825000166893005, 0.9862499833106995, 0.9862499833106995, 0.9800000190734863, 0.9925000071525574, 0.9837499856948853, 0.9825000166893005, 0.9821428656578064, 0.9787499904632568, 0.9862499833106995, 0.9825000166893005, 0.9887499809265137, 0.9887499809265137, 0.9900000095367432, 0.9862499833106995, 0.9887499809265137, 0.9825000166893005, 0.9825000166893005, 0.9642857313156128],
                "type": "scattergl",
                "name": "train_acc"
            }
        ]
        

        CodePudding user response:

        Another variant:

        import pandas as pd
        import json
        import matplotlib.pyplot as plt
        data = [
            {"x": [49, 99, 149, 199, 249, 299, 349, 399, 449, 499, 549, 599, 649, 699, 749, 799, 849, 899, 949, 999, 1049, 1099, 1149, 1199, 1249, 1299, 1349, 1399, 1449, 1499, 1549, 1599, 1649, 1699, 1749, 1799, 1849, 1899, 1949, 1999, 2049, 2099, 2149, 2199, 2249, 2299, 2349, 2399, 2449, 2499, 2549, 2599, 2649, 2699, 2749, 2799, 2849, 2899, 2949, 2999, 3049, 3099, 3149, 3199, 3249, 3299, 3349, 3399, 3449, 3499, 3549, 3599, 3649, 3699, 3749, 3799, 3849, 3899, 3949, 3999, 4049, 4099, 4149, 4199, 4249, 4299, 4349, 4399, 4449, 4499, 4549, 4599, 4649, 4699, 4749, 4799, 4849, 4899, 4949, 4999],
             "y":[0.8078442215919495, 0.6924954056739807, 0.637282133102417, 0.5666537284851074, 0.4263683557510376, 0.29408636689186096, 0.22852952778339386, 0.19844533503055573, 0.19280606508255005, 0.16006380319595337, 0.18059080839157104, 0.27476057410240173, 0.16974058747291565, 0.13871365785598755, 0.15893904864788055, 0.11171692609786987, 0.09724593907594681, 0.1499839723110199, 0.17183662950992584, 0.1576225459575653, 0.12323413789272308, 0.12138434499502182, 0.09343212097883224, 0.10920502990484238, 0.11386646330356598, 0.09778832644224167, 0.14350709319114685, 0.13651356101036072, 0.15092197060585022, 0.09928392618894577, 0.12458140403032303, 0.10494255274534225, 0.10528195649385452, 0.13158175349235535, 0.0788651630282402, 0.09537861496210098, 0.08444802463054657, 0.09358047693967819, 0.11348600685596466, 0.11421093344688416, 0.13024120032787323, 0.09298647195100784, 0.10095459222793579, 0.12882551550865173, 0.06175372377038002, 0.11043348163366318, 0.07273056358098984, 0.07597362995147705, 0.07747262716293335, 0.10011449456214905, 0.09275206923484802, 0.11640968173742294, 0.07466059923171997, 0.06264252215623856, 0.10496743023395538, 0.12706291675567627, 0.07618371397256851, 0.05997024476528168, 0.0738396868109703, 0.07691851258277893, 0.04857499152421951, 0.08904716372489929, 0.09522826969623566, 0.0718853548169136, 0.06508275866508484, 0.0604521669447422, 0.0327882245182991, 0.043644022196531296, 0.0742092877626419, 0.06414887309074402, 0.05483072251081467, 0.06133958697319031, 0.05271274596452713, 0.051848240196704865, 0.04392067342996597, 0.07057813555002213, 0.051781173795461655, 0.19890470802783966, 0.046647973358631134, 0.04351358115673065, 0.06602110713720322, 0.05368412658572197, 0.046410009264945984, 0.05693329870700836, 0.05165236443281174, 0.03220716118812561, 0.05031270533800125, 0.05368302762508392, 0.048431843519210815, 0.055765118449926376, 0.03884245455265045, 0.05483575165271759, 0.027131063863635063, 0.042943283915519714, 0.03232723847031593, 0.043425388634204865, 0.03555495664477348, 0.05412492901086807, 0.0535135492682457, 0.03369136527180672],
             "type":"scattergl", "name":"train_loss"}
        ]
        data_json = json.dumps(data[0])
        df = pd.read_json(data_json)
        data_json_plot = df.plot(x='x', y='y')
        plt.show()
        
  • Related