# ListDensity plot does not plot the actual data set

I have a dataset here here. It consists of three columns. It x values or the column 1 is not the same for all the data sets for corresponding z values (or in other words, x values are not equally spaced). But I would like to make a 2D color plot. It turns out that when I use the following

ListDensityPlot[data, InterpolationOrder -> 0]


will give a very wierd value.

If you look at the data set one can easily understand that it only has a three data sets combined. In principle the image must have three strips along the horizontal axis, instead, it has many strips.

Clearly, ListDesnity plot is not doing a good job here.How do I fix this?

EDIT

The point is that it has 3 x 651 (Y x X) data points. Clearly, from the plot, it is very clear that it has not been plotting that many data points when the Interpolation is zero. That is pretty weird by the way.

I need the actual data points without any sort of additional filtering like smoothing or anything.

Instead, of plotting {x,y,z} values directly in ListDensityPlot, I had used Matrix method something like the following, first make the partition the z value as 31x3 Matrix, then plot in list density plot. But this clearly does not work here since x values for three different sets (y) are not uniform. Or in other words for example. I have a data something like this

set1 = {{x,1,z11},{{x,2,z12},{x,3,z13}};
set2 = {{x2,1.1,z21},{{x2,2.5,z22},{x2,2.7,z23}}
set3={{x3,1.1,z31},{{x3,2.2,z32},{x3,3.5,z33}}
mydata=Join[set1,set2,set].


what I do not have is something like this.

set1 = {{x,1,z11},{{x,2,z12},{x,3,z13}};
set2 = {{x2,1,z21},{{x2,2,z22},{x2,3,z23}}
set3={{x3,1,z21},{{x3,2,z22},{x3,3,z23}}
Notmydata=Join[set1,set2,set].

• I do not fully understand your question, but I can see that the plot looks weird. To get rid of the white rectangles, use PlotRange -> All. These have values that are clipped otherwise. I do not understand what you mean by x not being equally spaced. The x values are the same for y=200, 300 and 400. Also, I do see three strips along the horizontal axis, so I do not understand this comment. Use Mesh -> All to see them better. However, the middle strip is made of many more parts than the top and bottom.This is weird and looks incorrect. The middle is wider, but that's fixable. Commented Mar 24, 2017 at 12:43
• Is you data structured as {{x, y, z}, ...} or {{x, ...}, {y, ...}, {z, ...}}? To me, it looks like your data is in the latter form, not the former. Commented Mar 24, 2017 at 12:57
• why are you using InterpolationOrder->0? Commented Mar 24, 2017 at 13:56
• A few points: 1) you have some strings in your data, which you should fix, but that doesn't change that 2) there does seem to be a bug here in the interpolation algorithm, and finally 3) for pure data visualization I don't see why you don't use a ListLinePlot like this Commented Mar 24, 2017 at 16:00
• @TM90 I'm sorry, I don't know what to do here. It's been known for some time that the interpolation used for lists of {x, y f(x,y)} is much worse than when it is for an array. But they don't seem to want to fix this. I still think for this data the plot with three lines is better, bug if you want the plot with three stripes of uniform width you could do that just graphics primitives fairly easily. I can look more closely some time this weekend maybe Commented Mar 25, 2017 at 1:27

## 3 Answers

I use MatrixPlot when I don't want interpolation.

mat = Table[
Select[DeleteCases[data, {_, _, ""}], #[[2]] == i &][[1 ;; 176,
3]], {i, {200, 300, 400}}];

MatrixPlot[mat, ImagePadding -> None,
PlotRangePadding -> None, AspectRatio -> Full]


I just took 176 elements on each row to have a square matrix, I reckon you can fix the data yourself someway you see fitting.

EDIT

To label the frame, there is another answer already here

 MatrixPlot[list,
FrameTicks -> {
{{1, 2, 3}, None}, {{{1, 101}, {2, 167}, {3, 188}, {4, 205}}, {{1, "Alice"}, {2,"Bob"}}}
}]

• +1 - this is the closest to what OP is asking for. If you can add in the FrameTicks to match those from ListDensityPlot I imagine it's just right Commented Mar 25, 2017 at 12:47
• But the issue with this is that we can not define any labels or automatic ticks for this particular plot.
– TM90
Commented Mar 29, 2017 at 8:59
• @JasonB, Do you use Mathematica for publishing purpose. I would love to know some nice options. I was considering using them for publishing. Do you have any thought on that ?
– TM90
Commented Mar 29, 2017 at 9:00
\$Version

(*  "11.1.0 for Mac OS X x86 (64-bit) (March 16, 2017)"  *)

data = Import["http://pastebin.com/raw/1eMDPXHB"] // ToExpression;


Eliminate elements with missing z values

data2 = DeleteCases[data, {_, _, ""}];


For comparison purposes look at data with ListPlot3D

ListPlot3D[data2, PlotRange -> All]


{xMin, xMax} = MinMax[data2[[All, 1]]]

(*  {-0.03, 0.15}  *)


Use Manipulate to enable selective trimming of the data

Manipulate[
ListDensityPlot[
Select[data2, #[[1]] <= xmax &],
PlotRange -> All],
{{xmax, xMax}, 0, xMax, xMax/20, Appearance -> "Labeled"}]


• @ Bob Halon, Thanks but again this is not I want, as I mentioned. if the data is interpolated by Mathematica, then there is absolutely no point of making this plot, I am sorry to say so.
– TM90
Commented Mar 25, 2017 at 0:41

here is one way to present that data.

cf[200] = Blue
cf[300] = Green
cf[400] = Red
Graphics3D[{cf[#[[2]]], Point[#]} & /@
Select[data, VectorQ[#, NumberQ] &], BoxRatios -> {1, 1/4, 1/2},
Axes -> True]


The problem with the contour plot is just that its noisy data. You'll need to do some smoothing or functional fitting to get a nice looking contour plot.

(The Select is filtering out a few points that are strings in the input data )

Try 2: smoothing each row. Using AspectRatio makes it look a bit less weird too.

ListDensityPlot[
Flatten[Table[
{#[[1]], y, #[[2]]} & /@ Transpose[MapAt[MeanFilter[#, 5] &,
Transpose[
Select[data,
VectorQ[#, NumberQ] && #[[2]] == y &][[All, {1,
3}]]], {2}]], {y, {200, 300, 400}}], 1],
InterpolationOrder -> 0, AspectRatio -> 1/5]


• @ george2079 But this is not what I want, I want density 2d plot
– TM90
Commented Mar 24, 2017 at 15:06
• @ george2079, Thanks again this is not I want. As I mentioned before however noisy the curve us, It must plot the actual data. If you look at this data in the second row (y axis from 250 to 350 range) it has many steps along as compared first and third rows. That is pretty wierd
– TM90
Commented Mar 25, 2017 at 0:45