Message Boards Message Boards

GROUPS:

Create dynamic PlotLegends?

Posted 1 month ago
200 Views
|
4 Replies
|
2 Total Likes
|

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.

4 Replies

Have you tried this:

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}, 
     PlotLegends -> Automatic]}]]

It adds labels one at a time.

Hi Gianluca, when using PlotLegends -> Automatic the legend doesn't show for the first curve, it works fine from the moment the second curve is printed. Also I would like to be able to customize the legend by specifying where it should be placed or for example I want to add InputField where the users can specify any name they want for each curve.

You can keep track of the input fields in a variable:

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

This is great, and I thought dynamic PlotLegends will require long and complex code. Thank you a lot Gianluca.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract