1
$\begingroup$

I have a 1000x1000 matrix the MatrixPlot of which gives this graph:

MatrixPlot of data

The code is

matrixplot1 =  MatrixPlot[data, DataReversed -> {True, False},
PlotLegends -> Automatic]

Since this matrix is obtained by sampling a set of data with X and Y coordinates in 1000x1000 grid the number in the i-th and j-th row and column indicates the number of occurrences between i-1 and j-1.

I would like to have the data on the Log10 scale and on the 0-1 interval for both X and Y axes. For these reasons I did the following:

  1. I transformed the matrix in a {x,y,z} list as follows
densityData =   Flatten[ParallelTable[{(j - 0.5)/1000, (i - 0.5)/1000,
data[[i, j]]}, {i, 1, 1000}, {j, 1, 1000}], 1];
  1. Then I used the DensityListPlot function:
graphDensityPlotLog = 
  ListDensityPlot[densityData,
   PlotLegends -> BarLegend[Automatic, LegendMarkerSize -> 0.4*imagesize],
   ScalingFunctions -> "Log10",
   FrameStyle -> Directive[Thick, Black],
   ColorFunction -> ColorData[{"SunsetColors", "Reverse"}],
   FrameLabel -> {"Quantity X", "Quantity Y"},
   FrameTicksStyle -> Directive[Black, 1.1*fontsize],
   LabelStyle -> Directive[Black, 1.3*fontsize],
   PlotRangePadding -> 0,
   ImageSize -> imagesize];

The result is Result of the ListDensityPlot

Because of the interpolation, there are colored bands in zones that are without any data. So I tried to obtain the same graph directly by the MatrixPlot with the following code:

log10Mod[x_] := If[x == 0, 0, Log10[x]];
log10Mod /@ {0, 1, 2, 3}

graphDensityPlotLog2 = 
 MatrixPlot[data, DataReversed -> {True, False},
  FrameLabel -> {"Quantity X", "Quantity Y"},
  PlotRange -> {{1, 550}, {1, 1000}},
  PlotRangePadding -> 20,
  FrameStyle -> Directive[Thick, Black],
  FrameTicksStyle -> Directive[Black, 1.1*fontsize],
  LabelStyle -> Directive[Black, 1.3*fontsize],
  ImageSize -> imagesize,
  ColorFunction -> (ColorData[{"SunsetColors", "Reverse"}][
      Rescale[log10Mod[#], log10Mod /@ {0, 200}]] &),
  ColorFunctionScaling -> False,
  PlotLegends -> 
   Placed[BarLegend[{(ColorData[{"SunsetColors", "Reverse"}][
         Rescale[#, log10Mod /@ {0, 200}]] &), log10Mod /@ {0, 200}}, 
     LegendMarkerSize -> 400, 
     LabelStyle -> Directive[Black, 25, FontFamily -> "Times"]], {1.0,
      0.5}]]

The result is the following: Result of the second try with MatrixPlot

I am not able to find a way to obtain the graph I obtain with the ListDensityPlot but from the MatrixPlot and without those wrong bands.

Thank you for your help!

Edit: based on the comment I made the following graph

fticks[min_, max_, bigstep_, smallstep_] := 
 Table[If[Mod[i, bigstep] == 0, {i + min, 
    ToString[If[IntegerQ[i + min], i + min, (i + min)/1.]], {.01, 
     0}}, {i + min, " ", {.005, 0}}], {i, 0, max - min, smallstep}]
fticksNoText[min_, max_, bigstep_, smallstep_] := 
 Table[If[Mod[i, bigstep] == 0, {i + min, " ", {.01, 0}}, {i + min, 
    " ", {.005, 0}}], {i, 0, max - min, smallstep}]
fticksNoBounds[min_, max_, bigstep_, smallstep_] := 
 Table[If[Mod[i, bigstep] == 0 && i != 0 && 
    i != (max - min), {i + min, 
    ToString[If[IntegerQ[i + min], i + min, (i + min)/1.]], {.01, 
     0}}, {i + min, " ", {.005, 0}}], {i, 0, max - min, smallstep}]
fticksReversed[min_, max_, bigstep_, smallstep_] := 
 Table[If[Mod[i, bigstep] == 0, {i + min, 
    ToString[If[IntegerQ[max - i], max - i, (max - i)/1.]], {.01, 
     0}}, {i + min, " ", {.005, 0}}], {i, 0, max - min, smallstep}]

graphDensityPlotLog3 = 
 MatrixPlot[data, DataReversed -> {True, False},
  FrameLabel -> {"Quantity X", "Quantity Y"},
  PlotRangePadding -> 0,
  DataRange -> {{0, 1}, {0, 1}},
  PlotRange -> {{0, 1}, {0, 0.55}},
  FrameStyle -> Directive[Thick, Black],
  FrameTicksStyle -> Directive[Black, 1.1*fontsize],
  FrameTicks -> {{fticksReversed[0, 1, 1/10, 1/100], 
     fticksNoText[0, 1, 1/10, 1/100]}, {fticksNoBounds[0, 1, 2/10, 
      4/100], fticksNoText[0, 1, 2/10, 4/100]}},
  LabelStyle -> Directive[Black, 1.3*fontsize],
  ImageSize -> imagesize,
  AspectRatio -> 1,
  ColorFunction -> (ColorData[{"SunsetColors", "Reverse"}][
      Rescale[log10Mod[#], log10Mod /@ {0, 200}]] &),
  ColorFunctionScaling -> False,
  PlotLegends -> 
   Placed[BarLegend[{(ColorData[{"SunsetColors", "Reverse"}][
         Rescale[#, log10Mod /@ {0, 200}]] &), log10Mod /@ {0, 200}}, 
     LegendMarkerSize -> 400, 
     LabelStyle -> Directive[Black, 25, FontFamily -> "Times"]], {1.0,
      0.5}]]

The result is Result of the third MatrixPlot

I had to define the ticks in order to put them in a MatrixPlot going from zero to one.

The problem of the legend bar remains to be solved.

$\endgroup$
10
  • 1
    $\begingroup$ You can try MatrixPlot[data, DataRange -> {{0, 1}, {0, 1}}] or MatrixPlot[Log10@data, DataRange -> {{0, 1}, {0, 1}}]? $\endgroup$
    – kglr
    Commented Feb 13, 2020 at 11:03
  • $\begingroup$ Another possibility is ListDensityPlot[Table[Sin[j^2 + i], {i, 0, Pi, 0.5}, {j, 0, Pi, 0.5}], Mesh -> None, InterpolationOrder -> 0, PlotRange -> All] $\endgroup$
    – yarchik
    Commented Feb 13, 2020 at 11:04
  • $\begingroup$ @kglr adding DataRange solves one problem, thanks. Doing Log10@data instead gives a plot with strange colors. $\endgroup$
    – Knomes
    Commented Feb 13, 2020 at 12:27
  • $\begingroup$ @yarchik using ListDensityPlot in this way does not give the results I search. $\endgroup$
    – Knomes
    Commented Feb 13, 2020 at 12:27
  • 1
    $\begingroup$ And what is the problem? Can you be more specific? $\endgroup$
    – yarchik
    Commented Feb 13, 2020 at 16:07

0

Your Answer

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

Browse other questions tagged or ask your own question.