legends on plots

267 views
Skip to first unread message

tvn

unread,
Mar 6, 2014, 2:02:04 PM3/6/14
to sage-s...@googlegroups.com
Hi, I try to add legend on a plot (created below) but doesn't seem to have any success.  There was no error, the legends just won't show up.


var('x y')
xrange = (x,0,5)
yrange = (y,0,5)
f1 = implicit_plot(x**2-4*x+y**2==0,xrange,yrange,color='red',linewidth=5,legend_label='x^2 - 4x + y^2 == 0') 
f2 = implicit_plot(x*y==1,xrange,yrange,linewidth=5,legend_label='x*y==1')
result = f1+f2
result.legend(True)
result.show(legend_loc='upper right',frame=True)


Liam Dalton

unread,
Mar 6, 2014, 2:24:00 PM3/6/14
to sage-s...@googlegroups.com
I have similar issues when running your code. However, the same code works if you do not do implicit plotting:

var('x y')
xrange = (x,0,5)
yrange = (y,0,5)
f1 = plot(x**2,xrange,color='red',legend_label='x^2 - 4x + y^2 == 0') 
f2 = plot(x,xrange,legend_label='x*y==1')
result = f1+f2
result.legend(True)
result.show(legend_loc='upper right',frame=True)

To me this implies that the problem is with implicit plotting.

kcrisman

unread,
Mar 6, 2014, 2:48:11 PM3/6/14
to sage-s...@googlegroups.com


On Thursday, March 6, 2014 2:24:00 PM UTC-5, Liam Dalton wrote:
I have similar issues when running your code. However, the same code works if you do not do implicit plotting:

var('x y')
xrange = (x,0,5)
yrange = (y,0,5)
f1 = plot(x**2,xrange,color='red',legend_label='x^2 - 4x + y^2 == 0') 
f2 = plot(x,xrange,legend_label='x*y==1')
result = f1+f2
result.legend(True)
result.show(legend_loc='upper right',frame=True)

To me this implies that the problem is with implicit plotting.


Correct - I've been playing with this for the past few minutes, and contour plots (of which implicit plots are a subset) simply don't do this, even though there is code in the ContourPlot class to handle legends!  http://git.sagemath.org/sage.git/tree/src/sage/plot/contour_plot.py#n191 for implicit plot situation.

var('x y')
P = contour_plot(x^2+y^2==1,(x,-1,1),(y,-1,1),plot_points=150, contours=(0,0), fill=False, cmap=["blue"],legend_label='test')
print P.legend()
P.legend(True)
print P.legend()
print P[0].options()['legend_label']
show(P)

shows this.  Not only this, but the legend is inexplicably not set to True.

I have a feeling that we need to do what we do in plot/disk.py or something; my guess (only a guess) is that the matplotlib thing that does contour plots (contour) only accepts labels directly for the individual contours, which is http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.clabel

In fact, http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.contour seems to confirm that we shouldn't be trying to label contour plots with legends. (Which makes sense, since they have their own labeling mechanism).  However, for implicit plots, this might cause a problem.  A workaround like at the bottom of 


might work.

kcrisman

unread,
Mar 6, 2014, 2:51:01 PM3/6/14
to sage-s...@googlegroups.com

ThanhVu (Vu) Nguyen

unread,
Mar 6, 2014, 3:05:49 PM3/6/14
to sage-s...@googlegroups.com
legend would work on plot but I can't run my code on plot  (it won't take the variable y)  

plot(x**2-4*x+y**2==0,xrange,yrange,color='red',linewidth=5,legend_label='x^2 - 4x + y^2 == 0')

Traceback (click to the left of this block for traceback)
...
ValueError: free variable: y

Vu, 


--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/YLKxkk1H04g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages