1
$\begingroup$

I would like to graph the following points and their respective line of fit on the same graph, but I would like to know if it is possible that the points are colored with different colors just as in the graph that I annex.

expected colors

The graph that I managed to obtain was:

Show[p1, p2]

common graph

p1 = ListPlot[{{0, 0}, {16, 1.09}, {17, 1.08}, {18, 1.2}, {19, 
1.27}, {20, 1.56}, {21, 1.42}, {22, 1.47}, {23, 1.53}, {24, 
1.65}, {25, 1.67}, {26, 1.73}, {27, 1.8}, {28, 1.87}, {29, 
2.91}, {30, 2.02}, {31, 2.07}, {32, 2.13}, {33, 2.17}, {34, 
2.27}, {35, 2.33}, {36, 2.37}, {37, 2.41}, {38, 2.53}, {39, 
2.46}, {40, 2.67}, {41, 2.73}, {42, 2.8}, {43, 2.83}, {44, 
2.93}, {45, 3}, {46, 3.07}, {47, 3.5}, {48, 3.2}, {49, 3.29}, {50,
 3.33}}, PlotTheme -> "Detailed"]

p2 = Plot[{0.0677 x}, {x, 0, 65}, PlotTheme -> "Detailed"]

I already graphed the points in a very common way but I would like the points to look colored like the previous graph. Thanks in advance.

$\endgroup$

2 Answers 2

1
$\begingroup$
data = {{0, 0}, {16, 1.09}, {17, 1.08}, {18, 1.2}, {19, 1.27}, {20, 
    1.56}, {21, 1.42}, {22, 1.47}, {23, 1.53}, {24, 1.65}, {25, 
    1.67}, {26, 1.73}, {27, 1.8}, {28, 1.87}, {29, 2.91}, {30, 
    2.02}, {31, 2.07}, {32, 2.13}, {33, 2.17}, {34, 2.27}, {35, 
    2.33}, {36, 2.37}, {37, 2.41}, {38, 2.53}, {39, 2.46}, {40, 
    2.67}, {41, 2.73}, {42, 2.8}, {43, 2.83}, {44, 2.93}, {45, 
    3}, {46, 3.07}, {47, 3.5}, {48, 3.2}, {49, 3.29}, {50, 3.33}};

p1 = ListPlot[data, PlotTheme -> "Detailed", ColorFunction -> Hue];

p2 = Plot[{0.0677 x}, {x, 0, 65}];

Show[p1, p2]

enter image description here

Or colored according to the x-coordinate, instead of the y-coordinate:

p1b = ListPlot[data, PlotTheme -> "Detailed", ColorFunction -> Function[{x, y}, Hue[x]]];
Show[p1b,p2]

enter image description here

Or perhaps

p1c = ListPlot[data, PlotTheme -> "Detailed", ColorFunction -> "Rainbow"];
Show[p1c, p2]

enter image description here

$\endgroup$
1
$\begingroup$

You can make each point a separate data set by mapping List onto the data. However, the colors cycle every 15 data points.

Show[
 ListPlot[List /@ {{0, 0}, {16, 1.09}, {17, 1.08}, {18, 1.2}, {19, 1.27}, {20,
      1.56}, {21, 1.42}, {22, 1.47}, {23, 1.53}, {24, 1.65}, {25, 1.67}, {26, 
     1.73}, {27, 1.8}, {28, 1.87}, {29, 2.91}, {30, 2.02}, {31, 2.07}, {32, 
     2.13}, {33, 2.17}, {34, 2.27}, {35, 2.33}, {36, 2.37}, {37, 2.41}, {38, 
     2.53}, {39, 2.46}, {40, 2.67}, {41, 2.73}, {42, 2.8}, {43, 2.83}, {44, 
     2.93}, {45, 3}, {46, 3.07}, {47, 3.5}, {48, 3.2}, {49, 3.29}, {50, 3.33}},
  PlotTheme -> "Detailed"],
 Plot[{0.0677 x}, {x, 0, 65},
  PlotTheme -> "Detailed",
  PlotStyle -> AbsoluteThickness[.7]]]

enter image description here

Alternatively you can use ColorFunction to vary the color as a function of x (#1) or y (#2).

Show[
 ListPlot[{{0, 0}, {16, 1.09}, {17, 1.08}, {18, 1.2}, {19, 1.27}, {20, 
    1.56}, {21, 1.42}, {22, 1.47}, {23, 1.53}, {24, 1.65}, {25, 1.67}, {26, 
    1.73}, {27, 1.8}, {28, 1.87}, {29, 2.91}, {30, 2.02}, {31, 2.07}, {32, 
    2.13}, {33, 2.17}, {34, 2.27}, {35, 2.33}, {36, 2.37}, {37, 2.41}, {38, 
    2.53}, {39, 2.46}, {40, 2.67}, {41, 2.73}, {42, 2.8}, {43, 2.83}, {44, 
    2.93}, {45, 3}, {46, 3.07}, {47, 3.5}, {48, 3.2}, {49, 3.29}, {50, 
    3.33}},
  PlotTheme -> "Detailed",
  ColorFunction -> (ColorData["Rainbow"][#1] &)],
 Plot[{0.0677 x}, {x, 0, 65},
  PlotTheme -> "Detailed",
  PlotStyle -> AbsoluteThickness[.7]]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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