2
$\begingroup$

I'd like to plot multiple sets of data, along with trend line fits for each. I can do this by plotting the fits, and then including the data in an Epilog. But I'd like the color of the data points to match the color of the curves in Plot. Is there an easy way to access the default colors of plots?

I've done what I want here:

enter image description here

But, I had to do clunky things. First, I assigned the output of the Plot to a variable out. Then in the Epilog, I called stuff like out[[1, 1, 2, 1, 2, 2, 2]] to get the correct color. There has to be a better way.

TIA

$\endgroup$
  • $\begingroup$ Maybe not quite what you asked, but an easy way to extract the colors used in some plot is to use out//Cases[#,_RGBColor,Infinity]&. More generally if you want the colors to be the same you can combine the ListPlot and Plot with Show, specifying in both the same list of colors to be used for the plot (using the PlotStyle option) $\endgroup$ – glS Jul 12 '16 at 23:09
  • 4
    $\begingroup$ The default color of Plot can be found here. $\endgroup$ – Jack LaVigne Jul 12 '16 at 23:43
  • $\begingroup$ In addition to the two marked duplicates (links inserted above your question) you may find (17252) of interest, especially xslittlegrass's restylePlot2. Please try to apply what you learn from all of these to your problem. If you have trouble let me know and I'll try to help; if I can't you can post a new question. $\endgroup$ – Mr.Wizard Jul 13 '16 at 5:50
  • $\begingroup$ Thanks all. Sorry about the dup. $\endgroup$ – rhomboidRhipper Jul 13 '16 at 13:40
3
$\begingroup$

Use Show to combine a ListPlot of the data and Plot of the fit functions. If the data is listed in the same order as the fit functions then the data will match the fit color.

dataA = {{0, 21}, {4, 18}, {9, 10}};
dataB = {{0, 45}, {5, 44}, {10, 38}};
dataC = {{0, 32}, {6, 22}, {11, 26}};

fitA = LinearModelFit[dataA, x, x];
fitB = LinearModelFit[dataB, x, x];
fitC = LinearModelFit[dataC, x, x];

Show[
 Plot[{fitA[x], fitB[x], fitC[x]}, {x, 0, 12},
  PlotLabels -> {"Data A", "Data B", "Data C"}, AxesOrigin -> {0, 0}],
 ListPlot[{dataA, dataB, dataC}]
 ]

enter image description here

$\endgroup$
  • $\begingroup$ You can just use Plot[{modelA /. fitA, modelB /. fitB}, {x, 0, 10}], instead of going to the effort to create a function that you can pass the variable x to. $\endgroup$ – Brett Champion Jul 13 '16 at 0:37
  • 1
    $\begingroup$ …and if you use LinearModelFit[], you can get a function that is quite easily plotted. $\endgroup$ – J. M. will be back soon Jul 13 '16 at 0:51

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