# Marker color not consistent

I have a simple plot with a legend in different colors. The result will be correct or incorrect depending of the order of the graphic elements.

This code gives an incorrect result:

reset()
Plaut = Graphics()
Plaut+=point([1,1/2],color='blue',size=15,legend_label='Sample point', legend_color='blue')
Plaut+=plot(x/2,x,0,2,color='black', thickness=0.2,legend_label='black line', legend_color='black')
Plaut+=plot(-x/2+1,x,0,2,color='red', thickness=0.2,legend_label='red line', legend_color='red')
Plaut.set_legend_options(loc=(1.1,0), markerscale=1)
show(Plaut, title='Color of the marker - 1.')


see

while this one (the only difference with the first one being the order of the graphic elements) gives a correct result:

reset()
Plaut = Graphics()
Plaut+=plot(x/2,x,0,2,color='black', thickness=0.2,legend_label='black line', legend_color='black')
Plaut+=plot(-x/2+1,x,0,2,color='red', thickness=0.2,legend_label='red line', legend_color='red')
Plaut+=point([1,1/2],color='blue',size=15,legend_label='Sample point', legend_color='blue')
Plaut.set_legend_options(loc=(1.1,0), markerscale=1)
show(Plaut, title='Color of the marker - 2.')


Here, the result is as expected:

For this MWE the correction was easy but when I have a lot of elements it is time consuming and somewhat frustrating to manually find the right configuration. I did not find in the manual a rule to follow to directly get the correct result.

edit retag close merge delete

Sort by » oldest newest most voted

Hello, @c05772! This is not an answer per se, but I didn't have enough space in the comments section, so I am writing it here. This seems to be a bug. (I should point out that not only the labels are problematic, but also the lines. Indeed, observe the black line in the plot and in the legend box is actually blue! Indeed, if you remove the arguments controlling the legend parameters, the black line still will be blue.)

After deeply exploring every single line of every single function (a.k.a. subroutine) that your code calls, and the functions that those functions call, etc., my theory is that the plotting procedure first draws the lines and writes the texts of the legend labels, and then it applies colors. If this theory is correct, I could find two lines of code that are problematic. The first one is line 2775 of sage.plot.graphics, which calls

g._render_on_subplot(subplot)


for every object g stored in a plot object. The line doesn't seem to be the problem itself, since after rewriting it as

_render_on_subplot(g, subplot)


and copying the code for _render_on_subplot to the sage.plot.graphics and making it so that it accepts tow arguments instead of one, the lines and the dot are drawn correctly and with the corresponding colors, the only difference being that the order in which they are drawn is inverted with respect to your calls. It bugs me that I don't understand why this is the case: using the same code in different places produces different results.

The second problem seems to come from line 2847 of sage.plot.graphics. This line is

for txt, color in zip(leg.get_texts(), self._legend_colors):


Its purpose is to pair the text of every legend label with the corresponding color. Unfortunately, it seems to be the case that leg.get_texts() returns the legends in a different order than the order in which they were drawn. Indeed, if I add the line

print(txt, '--->', color)


just after it, I get

Text(0, 0, 'black line') ---> 'blue'
Text(0, 0, 'red line') ---> 'black'
Text(0, 0, 'Sample point') ---> 'red'


So, you can see the colors do not correspond.

Another possible explanation for this problem is that Matplotlib (the underlying plotting engine for these cases) stores the graphical objects in a different order than Sage does.

Unfortunately, I couldn't find a solution to this problem. Hopefully, the information here will help somebody identify and solve the problem.

more

I also though it could be a bug but it is so easy to claim a bug when you fail to get your result that I let someone more qualified (and obviously you are!) claim it :-) . Now, note that for me the legends are incorrect but the black line itself is "more or less" black, at least grey, not blue, ... one of us might be colour blind ?

( 2021-09-21 21:48:47 +0100 )edit

Hello, @c05772! You were right: the line is grey, not blue. I am not colour blind, but I need new glasses. Ha, ha! This potential bug is persistent and I couldn't find a solution, except for rewriting a large part of the plotting subroutine. Perhaps I will run some more experiments and then try to report it at Sage Trac.

( 2021-09-26 05:02:36 +0100 )edit