I want to analysis a topography of the following image. I want to add a bar legend in this image associated with the appropriate colour of the image. Following is the image:enter image description here

For your reference Here I have attached a similar image which I got from another software.enter image description here How can I do this using Mathematica (I am using Mathematica 10)? Any suggestion will be really helpful. In my image white colour represent the particle with height 17nm and dark red represent background with height -2nm. i.e in bar legend white colour is 17nm and dark colour is -2nm.

  • $\begingroup$ An essential piece of the puzzle is missing in accomplishing what you desire: you must necessarily know what the colors are to be interpreted as (i.e., what does white signify? What does black or red indicate?) and be able to give this information to the Mathematica software. Once this is accomplished, construction of bar legends is a trivial task in comparison. $\endgroup$ Jun 18, 2021 at 3:29
  • 1
    $\begingroup$ white colour represent 17nm and dark red represent -2 nm height. and I want to construct a bar legend within this range. $\endgroup$
    – S.Pyne
    Jun 18, 2021 at 9:17

2 Answers 2


More of an extended comment, but perhaps have a look at this resource function?
(All credit goes to Mark Greenberg)

Using it quickly, I was able to get something like this:

img = Import["https://i.stack.imgur.com/NPQX8.jpg"];
cf = ResourceFunction["ImageColorFunction"][img, SampleOrder -> "Red"];
bar = BarLegend[{cf, {0, 1}}, LegendMarkerSize -> {25, 150}];
ImageCompose[img, bar // Rasterize, {Right, Top}, {Right, Top}]

enter image description here

PS: You mentioned you're using Mathematica 10, which I assume does not know of ResourceFunction. The source notebook from the resource function above seems to be using fairly standard functions, so perhaps you're in luck. In any case, the source-code should be useful to peruse.

  • 1
    $\begingroup$ +1 for addressing the image analysis and referencing the resource function link. $\endgroup$
    – Jagra
    Jun 17, 2021 at 13:39

Without the data you want to plot (or an approximation of the data) it becomes a little difficult to offer a full solution.

That said, the following (really offered as an extended comment), may provide you with some strategies to approach what you want to do.

It seems like you want to look at these sorts of: images:

enter image description here

and use a BarLegend.

You can find many examples in the documentation, e.g., see ComplexPlot

ComplexPlot[(z^2 + 1)/(z^2 - 1), {z, -2 - 2 I, 2 + 2 I}, PlotLegends -> Automatic]

enter image description here

Or ReliefPlot

ReliefPlot[ Table[i + Sin[i^2 + j^2], {i, -4, 4, .03}, {j, -4, 4, .03}], ColorFunction -> "SunsetColors", PlotLegends -> Automatic]

enter image description here

Essentially, if you select an appropriate type of Plot (ComplexPlot, ReliefPlot), to present the data, you'll have a ready option of using BarLegend.

You can additionally create a BarLegend and use Show to display it with an image.

Again, without a bit more information responders will likely need to guess what you want to do.

Maybe you just want to analyze the graphic image itself rather than generate it from the data of an Atomic force microscope (AFM) image.

If so, take a look in the documentation at: Image Processing & Analysis.

ImageMeasurements might then prove useful.

enter image description here

ImageMeasurements[image, "MeanIntensity"]

{0.194848, 1.}

ImageMeasurements[image, "Entropy"] // N



enter image description here

More information about what you need to do will help attract more specific answers.


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.