Search code examples
stata

How do I format the numerical labels in a legend?


In Stata, the following code produces a contour plot:

sysuse auto, clear
twoway (contour headroom mpg price), legend(on)

The plot looks as follows:

enter image description here

How do I change the legend to automatically format its labels as 1.0, 2.0 etc.?

I would like to do this without having to manually create labels and pass them to the legend option.

In my actual application, I'm trying to get this to work for a map that comes from the spmap command, but it's difficult to provide a minimal working example with that command.


Solution

  • It looks like you want to re-format the numbering in the clegend of the contour plot (as opposed to the 'traditional' legend).

    In this case, you just need to specify the zlabel option and the required format:

    sysuse auto, clear
    twoway contour headroom mpg price, zlabel(#5, format(%2.1f))
    

    This will produce the desired output:

    enter image description here