Produce a ListPlot with Dataset

Suppose I have the following Dataset:

I want to use ListPlot to produce a plot of v (voltage) vs. t (time).
Seems quite simple and straightforward, but I didn’t find an answer on the net.
I was thinking about doing

ListPlot[Thread[{mydata[All, "t"], mydata[All, "v"]}]]

but mydata[All, "t"] gives an error:

Failure[Dataset,Association["MessageTemplate" :> MessageName[Dataset, "partnotapplicable"],"MessageParameters" -> Association["Type" -> TypeSystem`Vector[TypeSystem`AnyType, 2], "Part" -> "t","Symbol" -> Part]]]

I wouldn’t be surprised to learn that there is a short way to do that, without the use of Thread.

Ehud,

Do either

dataset[ListPlot, {"t", "v"}]

dataset[ListPlot, {#t, #v} &]

To get your plot.

Datasets are not lists but you can turn them into lists or some other format with the second example above (applying a function to the data). Look at the planet example or the titanic example in the help on Dataset.

Regards,

Neil

Hmmm… Thanks very much for you reply!

But, something isn’t working for me.

I keep getting a “Failure” error - the one I wrote in the original post. Do you think it has something to do with the way I generate the csv file?

I am using Google Spreadsheet for this task, and the csv file is finely imported to the notebook.

I am also working on Mathematica 12.1 on Windows 7 64bit.

I wonder what I am missing here.

My notebook reads:

dataset = Import["filename.csv", "Dataset"]
dataset[ListPlot, {"t", "v"}]

I attached the sample .csv file to this post, if that could help.

sample_file.csv (31 Bytes)

Hi Ehud,

If the csv has a header row with column names then use this to Import as a Dataset.

ds = Import["~/Downloads/sample_file.csv", "Dataset", HeaderLines -> 1]

Notice that in this case the Dataset is rendered differently, the first row is a darker gray.

ds[ListPlot, {"t", "v"}]

Compare

InputForm@dataset
InputForm@ds

In addition to Rohit’s suggestion, SemanticImport will import the data as a dataset:

ds = SemanticImport["~/Downloads/sample_file.csv"]
ds[ListPlot, {"t", "v"}]

@ Neil Singer

and

@ Rohit Namjoshi,

Thanks very much indeed. Both your suggestions work.

This is a follow-up question. I figured this would be a good place to ask it. I can start a new thread if that is better.

I have a dataset that has about 10 named columns (string headers) and I can produce scatterplots using the construction described earlier in this thread:

dataset[ListPlot, {"Xheader", "Yheader"}]

However, after I perform a few data operations (filter, aggregate, segregate, subtotal, etc.) I end up with a two-level dataset (an association of associations) that no longer has string headers; it is all numerical values. It looks something like:

ExtractedData =<| x1 -> <| y11 -> z11, y12 -> z21, ..., y1n -> z1n |>, x2 -> <| y21 -> z21, y22 -> z22, ..., y2n -> z2n |>,...,xk-> <| yk1 -> zk1, yk2 -> zk2, ..., ykn -> zkn |> |>

Where the x, y, and z are now numerical values, not string headers.

Naturally, here I would like to plot z versus y for each x, etc. I have not found a ‘simple’ way to do that. The simplest way I have found is to map (nested map) onto my dataset a function like:

ListForPlotting = KeyValueMap[List, Map[KeyValueMap[List, #] &, ExtractedData]]

ListPlot[ ListForPlotting[[All,-1]] ]

and I can use ListForPlotting[[All,1]] for the legend.

This may not look too bad, but I was just wondering if there is a way to retain the ability to produce plots directly after doing the data processing. By ‘directly’ I mean that I do not need to turn the ExtractedData, which is still a dataset into a list. A way to plot ExtractedData directly as a dataset.

Thanks in advance for any ideas,
Otto Linsuain

Hey there

The Tabular function was introduced in version 14.2 (January 2025) and I think that this is the function to use for the use case I described in this thread (and perhaps pertians to your question as well).

Quoting the release announcement:

When should you use a Tabular, rather than, say a Dataset? Tabular is
specifically set up for data that is arranged in rows and columns—and
it supports many powerful operations that make sense for data in this
“rectangular” form. Dataset is more general; it can have an arbitrary
hierarchy of data dimensions, and so can’t in general support all the
“rectangular” data operations of Tabular. In addition, by being
specialized for “rectangular” data, Tabular can also be much more
efficient, and indeed we’re making use of the latest type-specific
methods for large-scale data handling.

In my opinion, ask a new question about your problem where you use Tabular and not Dataset.