I made a plot of a matrix data (around 3000 columns x 2000 row) using ArrayPlot. The followings are the code and output.

As it is quite difficult to identify the value from a continuous color table, I would like to ask how to plot the graph with a discrete color table and the number of discrete colors can be defined by myself.

Thanks a lot in advance!

ArrayPlot[data, PlotRange -> Automatic,DataReversed -> {True, False}, ColorFunction -> ColorData["LightTemperatureMap"],PlotLegends -> {Style["Height (m)"], Placed[Automatic, Right]}] 

enter image description here

  • $\begingroup$ See here how to make a custom legend. $\endgroup$
    – corey979
    Commented Mar 4, 2017 at 10:38
  • $\begingroup$ hi @corey979, thank you for the suggestion. I understand that discrete color table can be made via ContourPlot. However, it takes a very very long to generate a ContourPlot as the size of data is very large. So I select ArrayPlot. $\endgroup$
    – wkong
    Commented Mar 4, 2017 at 16:19

1 Answer 1


From your example Using a given colour table with Image? might be applied here.
Please load the renderImage code from my answer there before evaluating the code below.

data = Import["http://exampledata.wolfram.com/hailey.dem.gz", "Data"];

{1201, 1201}
legend = SwatchLegend[
   Array[ColorData["LightTemperatureMap"], 5, {0, 1}],
   Array[N, 5, {Min@data, Max@data}]

img = 
    Rescale @ data,
    ColorData["LightTemperatureMap"], 5, ImageSize -> 300];

Labeled[img, legend, Right]

enter image description here

If this performs well for you I can write this into a single function, if you need help doing that.

  • $\begingroup$ This is exactly what I need!! I will copy your code myself. Thanks a lot! $\endgroup$
    – wkong
    Commented Mar 6, 2017 at 11:01
  • $\begingroup$ Hi @Mr.Wizard In your example, there are 5 colors. May I ask what range each colour refers to? For example, the blue one is from 954 to 1615.5? The 2nd question is can I define another plot range? For example, range of the above data is 954 to 3600. Can I set the legend like {900, 1800, 2700, 3600}? Many thanks! $\endgroup$
    – wkong
    Commented Mar 7, 2017 at 2:02
  • $\begingroup$ @wkong I'll try to come back to this and provide better examples, but for now a brief comment: the original code uses Round; that could be replaced with Floor or Ceiling for different effects, or more complicated methods like Nearest or Interpolation could also be used. Additionally the legend above is likely poor because it doesn't really represent what is shown; explicit ranges like [1800, 2400] might be more clear. Whatever method is used (if Round is replaced) should be synchronized with the legend for accuracy. Clearly this will take a little work. $\endgroup$
    – Mr.Wizard
    Commented Mar 7, 2017 at 3:58

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.