I am trying to plot a function over a 2 dimensional region, which takes integer and half integer values.

However, due to numerical approximations and errors, the calculated value of the function sometimes becomes 0.99 or 1.01 instead of exactly 1. When I make a contour plot, it gives a certain color between 0 and 1, another color between 1 and 2, and so on. As a result, 0.99 and 1.01 acquire different colors (while I want both of them to be the same color, because they represent 1).

What would be an efficient way to plot different integers (approximately, upto numerical errors) and half integers with different colors in a contour (or a similar) plot?

Also, the function takes values between -2 and 2, so I don't need to take care of all integers.

I cannot use floor function because that will send both 0.99 (should be 1) and 0.01 (should be 0) to 0.

  • 3
    $\begingroup$ Can you use Round for the integers and Round[#, 1/2] & for the half-integers? $\endgroup$
    – thorimur
    Apr 11, 2021 at 18:51
  • 2
    $\begingroup$ @thorimur or simply Round[#, 1/2]& $\endgroup$
    – mikado
    Apr 11, 2021 at 22:18


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.