Color and Legend with Gadfly - how can I control them?

I would like to ask you if you have any advice… as I can’t sort out one issue when creating plots with Gadfly in Julia v0.5.

I need to plot matrices, which have discrete values (from 0 to 127), so that each value is plotted with one colour. See the attached examples.

I create them with the following line:

plot( x = jjj - 1 , y = iii - 1 , color = iiijjjvalues ,
     Scale.color_discrete , 
     Guide.xticks( ticks = collect( 0 : 5 : number_of_observations_arm_2 ) ) ,
     Guide.yticks( ticks = collect( 0 : 5 : number_of_observations_arm_1 ) ) ,
     Guide.xlabel( "# successes on arm 1 (out of $number_of_observations_arm_2)" ) , 
     Guide.ylabel( "# successes on arm 0 (out of $number_of_observations_arm_1)" ) ,
     Guide.colorkey( "Consecutive \n failures" ) ,
     Coord.cartesian( yflip = true , fixed = true ) ,
     Theme( panel_fill = nothing , highlight_width = 0pt ,
            default_point_size = my_point_size , plot_padding = 5pt ,
            major_label_font_size = 11pt , minor_label_font_size = 9pt ) 

You can see in the legend that the same sequence of colours appears in the two plots: blue, gold, pink, green, violet, red, grey, etc. These colours appear in the plot in this order if you follow the first column from top to bottom, then then second column, etc.

However the legend then looks weird, because it would be natural to have the colours ordered by the values they correspond to (0, 1, 2, 3, 4, etc.). Moreover, as the colour ordering prevails, the same values are plotted in different colours in these two plots, which is not very helpful - I need to include many figures like this and I would prefer if I only needed one legend, the same for all of them.

I wonder whether you would have a suggestion for a quick fix or where I could get some help with it. Basically, I need to get control of how the colours are assigned to values and how they appear in the legend.

Thanks a lot!




have you thought about making an image instead of a plot?

not sure what you mean.

Rather than going through the plotting machinery, you could just map each value to a different color yourself, and then generate an image from those mapped colors. This is extremely easy to do with the Images.jl package:

You can save the resulting image as a PNG, and then embed that in your document. I realize that this doesn’t solve your legend problem, but it might be a simpler way to visualize your data.


Take a look at Scale.color_discrete_hue by fixing the color scale between your two plots you should be able to have a consistent choice of colors in your plots. You can have even more control with Scale.color_discrete_manual and create an appropriate Theme to reuse your settings across many plots.

Thanks to eveyone for replying. Anyone interested, please see my solution here: