# How to plot a line density plot using data file

I am using the following code to generate a density plot using a data file (which has three columns). The first column is X axis and second column is Y axis and the third column is the color. It gives me a plot with different color points.

ListPlot[Table[
PlotStyle -> PointSize[Medium], Frame -> {True, True, True, True},
FrameTicks -> Automatic, FrameLabel -> {"A", "C"},
LabelStyle ->
Directive[Black, Bold, Medium, FontFamily -> "Helvetica"],
RotateLabel -> False, PlotRange -> Full]


Now suppose, instead of the points, I want a line connecting all the points and the color of the line should change according to the 3rd column. I don't really know whether it is possible to do or not. Can anyone suggest something!

• Can you add a little of the data so we know what you're working with? You probably want to check out ListLinePlot and ColorFunction. Commented Jan 21, 2019 at 15:38
• do you want to color the line based on (the third coordinate of) the first point or do you prefer to blend the colors of the two end points of the a line?
– kglr
Commented Jan 21, 2019 at 15:39
• @CarlLange: ListLinePlot does not work here. My data file has the numerical values of a function $f(x,y,z)$. I don't know the functional form of the function otherwise I could have used ParametricPlot. Commented Jan 21, 2019 at 15:47
• @kglr: As I have different color for different point (based on the value of 3rd coordinate at that point), I want to connect those two point using a linear interpolation and then color can go accordingly. Commented Jan 21, 2019 at 15:49
• Please do not use tags unrelated to the problem that are you asking about (is this problem specific to Mathematica Online?) Commented Jan 21, 2019 at 15:55

SeedRandom[123]
data = Join[RandomReal[10, {100, 2}], List /@ RandomReal[{0, Pi}, 100], 2];

ListPlot[Table[Style[{data[[i, {1, 2}]]}, Hue[data[[i, 3]]/(Pi)]], {i,
Length[data]}], PlotStyle -> PointSize[Medium],
Frame -> {True, True, True, True}, FrameTicks -> Automatic,
FrameLabel -> {"A", "C"},
LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Helvetica"],
RotateLabel -> False, PlotRange -> Full]


One possible way is to use Graphics:

Graphics[{Hue[#[[3]]/Pi], PointSize[Large], Point[#[[;; 2]]]} & /@ data,
Frame -> True, FrameLabel -> {"A", "C"},
LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Helvetica"],
RotateLabel -> False, AspectRatio -> 1/GoldenRatio]


To color each line base on the color of its first point:

Graphics[{Hue[#[[1, 3]]/Pi], Thick, Line[#[[All, ;; 2]]]} & /@ Partition[data, 2, 1],
Frame -> True, FrameLabel -> {"A", "C"},
LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Helvetica"],
RotateLabel -> False, AspectRatio -> 1/GoldenRatio]


To color each line by blending the colors of its endpoints:

Graphics[{Thick, Line[data[[All, ;; 2]], VertexColors -> (Hue /@ (data[[All, 3]]/Pi))]},
Frame -> True, FrameLabel -> {"A", "C"},
LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Helvetica"],
RotateLabel -> False, AspectRatio -> 1/GoldenRatio]


• Thanks dude. I must admit you are excellent in using mathematica. Commented Jan 21, 2019 at 16:44