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.

enter image description here

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?


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}}

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}}
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Mar 24, 2017 at 12:43
  • $\begingroup$ 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. $\endgroup$
    – rcollyer
    Mar 24, 2017 at 12:57
  • 1
    $\begingroup$ why are you using InterpolationOrder->0? $\endgroup$
    – george2079
    Mar 24, 2017 at 13:56
  • 1
    $\begingroup$ 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 $\endgroup$
    – Jason B.
    Mar 24, 2017 at 16:00
  • 1
    $\begingroup$ @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 $\endgroup$
    – Jason B.
    Mar 25, 2017 at 1:27

3 Answers 3


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]

enter image description here

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.


To label the frame, there is another answer already here

 FrameTicks -> {
 {{1, 2, 3}, None}, {{{1, 101}, {2, 167}, {3, 188}, {4, 205}}, {{1, "Alice"}, {2,"Bob"}}}
  • $\begingroup$ +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 $\endgroup$
    – Jason B.
    Mar 25, 2017 at 12:47
  • $\begingroup$ But the issue with this is that we can not define any labels or automatic ticks for this particular plot. $\endgroup$
    – TM90
    Mar 29, 2017 at 8:59
  • $\begingroup$ @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 ? $\endgroup$
    – TM90
    Mar 29, 2017 at 9:00

(*  "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]

enter image description here

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

(*  {-0.03, 0.15}  *)

Use Manipulate to enable selective trimming of the data

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

enter image description here

  • $\begingroup$ @ 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. $\endgroup$
    – TM90
    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]

enter image description here

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.

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

enter image description here

  • $\begingroup$ @ george2079 But this is not what I want, I want density 2d plot $\endgroup$
    – TM90
    Mar 24, 2017 at 15:06
  • $\begingroup$ @ 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 $\endgroup$
    – TM90
    Mar 25, 2017 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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