2 replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
source | link

I have a list of associations :

testdata = {<|"label" -> "v1", "value1" -> -2, "value2" -> 6|>,
<|"label" -> "v2", "value1" -> 4, "value2" -> -4|>,
<|"label" -> "v3", "value1" -> 6, "value2" -> -5|>};

The first element of each association is the X axis label, each following key indicates the set of data it belongs to.

Using Vitali's answer herehere I have managed to get ListPlot to use each first element as X axis labels, but the code isn't the cleanest due to repeated usage of "testdata"

ListPlot[{testdata[[All, 2]], testdata[[All, 3]]}, Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, 1]], 
testdata[[All, 1]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

Using key names is a tiny bit clearer at the expense of verbosity:

ListPlot[{testdata[[All, "value1"]], testdata[[All, "value2"]]}, 
Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, "label"]], 
testdata[[All, "label"]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

I believe there must be a way to substitute in a pure function for this usage but am struggling to find it - any ideas?

Note that Im trying to avoid explicit use of Dataset due to its somewhat quirky behaviour in 10.0.x

BTW

Named keys in associations doesn't seem to support the ;; span specification syntax of Part - is this a bug?

testdata[[All,2;;3]] (* works *)
testdata[[All,"value1";;"value2"]] (* doesnt work *)  

I have a list of associations :

testdata = {<|"label" -> "v1", "value1" -> -2, "value2" -> 6|>,
<|"label" -> "v2", "value1" -> 4, "value2" -> -4|>,
<|"label" -> "v3", "value1" -> 6, "value2" -> -5|>};

The first element of each association is the X axis label, each following key indicates the set of data it belongs to.

Using Vitali's answer here I have managed to get ListPlot to use each first element as X axis labels, but the code isn't the cleanest due to repeated usage of "testdata"

ListPlot[{testdata[[All, 2]], testdata[[All, 3]]}, Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, 1]], 
testdata[[All, 1]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

Using key names is a tiny bit clearer at the expense of verbosity:

ListPlot[{testdata[[All, "value1"]], testdata[[All, "value2"]]}, 
Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, "label"]], 
testdata[[All, "label"]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

I believe there must be a way to substitute in a pure function for this usage but am struggling to find it - any ideas?

Note that Im trying to avoid explicit use of Dataset due to its somewhat quirky behaviour in 10.0.x

BTW

Named keys in associations doesn't seem to support the ;; span specification syntax of Part - is this a bug?

testdata[[All,2;;3]] (* works *)
testdata[[All,"value1";;"value2"]] (* doesnt work *)  

I have a list of associations :

testdata = {<|"label" -> "v1", "value1" -> -2, "value2" -> 6|>,
<|"label" -> "v2", "value1" -> 4, "value2" -> -4|>,
<|"label" -> "v3", "value1" -> 6, "value2" -> -5|>};

The first element of each association is the X axis label, each following key indicates the set of data it belongs to.

Using Vitali's answer here I have managed to get ListPlot to use each first element as X axis labels, but the code isn't the cleanest due to repeated usage of "testdata"

ListPlot[{testdata[[All, 2]], testdata[[All, 3]]}, Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, 1]], 
testdata[[All, 1]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

Using key names is a tiny bit clearer at the expense of verbosity:

ListPlot[{testdata[[All, "value1"]], testdata[[All, "value2"]]}, 
Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, "label"]], 
testdata[[All, "label"]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

I believe there must be a way to substitute in a pure function for this usage but am struggling to find it - any ideas?

Note that Im trying to avoid explicit use of Dataset due to its somewhat quirky behaviour in 10.0.x

BTW

Named keys in associations doesn't seem to support the ;; span specification syntax of Part - is this a bug?

testdata[[All,2;;3]] (* works *)
testdata[[All,"value1";;"value2"]] (* doesnt work *)  
1
source | link

Using ListPlot and lists of Associations

I have a list of associations :

testdata = {<|"label" -> "v1", "value1" -> -2, "value2" -> 6|>,
<|"label" -> "v2", "value1" -> 4, "value2" -> -4|>,
<|"label" -> "v3", "value1" -> 6, "value2" -> -5|>};

The first element of each association is the X axis label, each following key indicates the set of data it belongs to.

Using Vitali's answer here I have managed to get ListPlot to use each first element as X axis labels, but the code isn't the cleanest due to repeated usage of "testdata"

ListPlot[{testdata[[All, 2]], testdata[[All, 3]]}, Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, 1]], 
testdata[[All, 1]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

Using key names is a tiny bit clearer at the expense of verbosity:

ListPlot[{testdata[[All, "value1"]], testdata[[All, "value2"]]}, 
Joined -> True, 
Ticks -> {Transpose[{Range@Length@testdata[[All, "label"]], 
testdata[[All, "label"]]}], Automatic}, 
PlotLegends -> DeleteDuplicates@Keys@testdata[[All, 2 ;; 3]]]

I believe there must be a way to substitute in a pure function for this usage but am struggling to find it - any ideas?

Note that Im trying to avoid explicit use of Dataset due to its somewhat quirky behaviour in 10.0.x

BTW

Named keys in associations doesn't seem to support the ;; span specification syntax of Part - is this a bug?

testdata[[All,2;;3]] (* works *)
testdata[[All,"value1";;"value2"]] (* doesnt work *)