I am using matplotlib to generate a filled contour plot, please consider the below example as a sample contour plot. I want to read off the contour values from such a filled contour plot using opencv's mouse interaction modules. For example, if the uses hovers the mouse over this contour image, it should dynamically display the contour values as the mouse moves over the image. I have the opencv part figured out, but I am struggling to link the RGB values to the actual numeric values.

I think that in the background, matplotlib maps the colormap (jet, viridis, inferno etc) to the min and max values that will be shown in the contour plot, and segments it in accordance to the number of contours chosen to show the contour data. How do I then access these contours and their RGB values so that I can pass this mapping to opencv?

Sample filled contour plot


1 Answer 1


I do not know if this is the answer, but I think I am not wrong on that.

I have implemented contour lines and stuff for my own project and I think it is the same situation in all projects. In order to create graphics like that you do not explicitly create the curves, it would be too hard and I think almost impossible. Any algorithms does not try to create the curves itself, but split the space in small squares, and in each small square they test the values and draws very simple shapes. One well known algorithm is marching squares. Check the drawings from that page and you will soon understand how it works.

The algorithm approximates the curves that you finally see, but because the squares are small you do not see that they are only approximations. The trouble with such structure is that it contains a lot of information and it would be a great effort to keep that information somewhere and from that data to resemble the curves that you want. The usual approach is simply to not keep the whole stuff but run the algorithms and draw directly on the raster image. It is much easier and does not waste memory for the eventuality that somebody would want to do something with that.

I am not saying that the library you use does not provide the data structure from which you can resemble the iso curves, since I do not know, I say only that I would not offer some feature since anyway it is rarely used and requires a lot of resources.

Going further, I think that what you need is not the iso curve information, but a function which given a color would return you a value. A better place to find that transformation would be the scale object, not the graph itself. As far as I know matplotlib offers a scale object.

  • $\begingroup$ Hey, thanks for explaining how contour plotting actually works in the background. So if I understand correct, matplotlib has a scale object that basically has the information/ can transform the shown colors to its corresponding numeric value? So by default, I guess every contour plot has its scale object defined in the background, I just need to access the object related my contour plot - is this what you are suggesting? $\endgroup$ May 3, 2022 at 15:44
  • $\begingroup$ Yes. The scale has two functions: one transformer and it’s inverse $\endgroup$
    – rapaio
    May 3, 2022 at 17:26

Your Answer

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

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