Menu

Legend Question

2011-10-31
2012-09-07
  • Andy Lawton

    Andy Lawton - 2011-10-31

    I have a graph with two plots and two separate Y axis. If I add a legend to
    this plot, the colours for the legend are OK, but the second plot comes out
    red, when I am expecting blue.

    $leg=array("Gauge Error","Speed");
    
    $plot = new PHPlot; 
    $plot = new PHPlot(800, 350);
    $plot->SetDataColors(array('red',"blue"));
    $plot->SetLegend($leg);
    $plot->SetImageBorderType('plain');
    $plot->SetPrintImage(False);
    $plot->SetPlotType('lines');
    $plot->SetDataValues($gerror);
    $plot->SetPlotAreaWorld(NULL, -10, NULL, 10);
    $plot->SetPlotType('lines');
    
    # Set margins to leave room for plot 2 Y title on the right.
    $plot->SetMarginsPixels(50, 50);
    //Turn off X axis ticks and labels because they get in the way:
    $plot->SetXTickLabelPos('none');
    
    $plot->SetXTickPos('none');
    
    $plot->DrawGraph();
    
    //plot 2
    
    $plot->SetDrawPlotAreaBackground(False); // Cancel background
    $plot->SetPlotType('lines');
    $plot->SetDrawYGrid(False); // Cancel grid, already drawn
    $plot->SetDataValues($spd);
    $plot->SetPlotType('lines');
    $plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
    $plot->SetYTickPos('plotright');
    $plot->SetYTickLabelPos('plotright');
    //$plot->SetLegendPosition(0, 0, 'image', 0, 0, 5, 5);
    $plot->DrawGraph();
    
    # Now output the graph with both plots:
    $plot->PrintImage();
    

    If I then add $plot->SetDataColors('blue'); on the second plot, it does get
    plotted with a blue line and the first with a red line, but the legend colours
    are blue for both plots.

    What am I doing wrong?

     
  • lbayuk

    lbayuk - 2011-10-31

    You haven't shown what your data arrays look like, or exactly what you are
    trying to do, so this might not be the right answer. I assume you want the
    first plot (with a single data set) to be red, and the second plot (also with
    a single data set) to be blue, and a legend to show a red and a blue entry.

    Here's how you do it. For the first plot, exactly what you have now:

    $plot->SetDataColors(array('red',"blue"));
    $plot->SetLegend($leg);
    

    Note that 'blue' is only there for the legend. Your data array has a single
    data set and it will plot in red.

    For the second plot, include this:

    $plot->SetDataColors(array("blue"));
    $plot->SetLegend(NULL);
    

    Now your second plot will be in blue, and there will be no legend for this
    plot. (If you don't cancel the legend, you get a second one, but since you
    don't move it, it overlays the first.)

    Does that help?

    (FYI, you are creating 2 PHPlot objects - the 2nd replacing the first?)

     
  • Andy Lawton

    Andy Lawton - 2011-10-31

    Thanks for the reply. Your solution works perfectly for me.

    I am plotting two graphs on the same plot, but with different scales. That's
    why there's one Y axis on the left and one on the right. I just followed the
    example in the documentation. The data looks like this:

    $gerror[]=array("",$line[$ifound[1]]);
    $spd[]=array("",$line[$ifound[0]]);
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.