1
$\begingroup$

How to add dynamic PlotLegends to the below code?

DynamicModule[{inputData = 0, dataSet = {}}, 
 Column[{
   InputField[Dynamic[inputData]], 
   Button["Plot", 
    AppendTo[dataSet, Table[inputData + i^j, {i, 5}, {j, 2}]]], 
   Spacer[5],
   Dynamic@ListPlot[dataSet, Joined -> True, ImageSize -> {300}]
   }]] 

I would like to have a PlotLegends which appears only when the Button is pressed and ListPlot prints the curve, otherwise it shouldn't be visible. Also every time the Button is pressed ListPlot appends the new curve to the old one, therefore the PlotLegends should print the name for every printed curve, but one at the time. How to achieve that?

I spent hours on searching some guidance which could give me at least an idea of how to start, but all I found is for the static PlotLegends. This is why I'm asking you for a help.

Cross-posted on Wolfram Community

$\endgroup$
1
$\begingroup$
DynamicModule[{inputData = 0, dataSet = {}, lbl = {}}, 
 Column@{InputField[Dynamic[inputData]], 
  Button["Plot", 
   AppendTo[dataSet, Table[inputData + i^j, {i, 5}, {j, 2}]]; 
   AppendTo[lbl, inputData]  (*Creating labels for the Legend*)
  ],  
  Spacer[5],
  Dynamic@ListPlot[dataSet, Joined -> True, ImageSize -> {300}, 
    If[dataSet != {}, PlotLegends -> lbl[[1 ;; Length@dataSet]], Joined -> True]]}]

I've repeated the Joined->Truein the last line to fill the slot for an initial case when there is not curves yet. You can use other ways.. enter image description here

|improve this answer|||||
$\endgroup$
  • $\begingroup$ It works great! Thank you $\endgroup$ – Kamila Mar 20 '19 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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