Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.