Ask Your Question
2

Marker color not consistent

asked 2021-09-20 00:31:45 +0100

c05772 gravatar image

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 Marker Color 1

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: image description

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-09-20 21:41:05 +0100

dsejas gravatar image

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.

edit flag offensive delete link more

Comments

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 ?

c05772 gravatar imagec05772 ( 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.

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-09-20 00:31:45 +0100

Seen: 595 times

Last updated: Sep 20 '21