1
$\begingroup$

I'm trying to plot the data with DateListPlot function, but having some issues with the output.

Here is what I'm doing. I'm downloading and parsing RAW JSON like this and extracting values section from it:

{
  "status": "success",
  "data": {
    "resultType": "matrix",
    "result": [
      {
        "metric": {
          "__name__": "metric_name"
        },
        "values": [
          [
            1556901190.699,
            "91158"
          ],
          [
            1556901220.699,
            "91160"
          ]
        ]
      }
    ]
  }
}

The raw dataset (after JSON processing) looks like below (with the first element for each of three lists below is a UnixTimeStamp):

Inn[1]:=  input = {{1.557343390585`*^9, "120587"}, {1.557343420585`*^9, 
   "120589"}, {1.557343450585`*^9, "120591"}}

Out[1]:=  {{1.55734*10^9, "120587"}, {1.55734*10^9, "120589"}, {1.55734*10^9, "120591"}}

Then, dates are being converted to time objects and the entire object converted to the list of lists:

In[115]:=  dates = Partition[Map[FromUnixTime, input[[All, 1]]], 1]

Out[115]

And datapoints converted to the list of list as well (for merging in the next phase):

In[116]:=  values = Partition[input[[All, 2]], 1]
Out[116]:=  {{"120587"}, {"120589"}, {"120591"}}

As the final step, I'm trying to plot the data using DateListPlot:

In[120]:=  DateListPlot[Join[dates, values, 2], PlotRange -> All]

But getting the following output:

DateListPlot

What am I missing?

Thanks in advance.

$\endgroup$
  • $\begingroup$ DateListPlot needs to plot {date, number} pairs, while you have {date, string} pairs. $\endgroup$ – Carl Woll May 9 at 20:22
  • $\begingroup$ Thank you, Carl! $\endgroup$ – Pavel Klymenko May 9 at 20:32
1
$\begingroup$

Thanks to Carl, I've made the following adjustment:

Instead of

In[116]:=  values = Partition[input[[All, 2]], 1]

I used the following code (using FromDigits function to convert strings to numbers)

In[116]:=  values = Partition[Map[FromDigits, input[[All, 2]]], 1]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.