Re: Ploting an arc and shading the area within the arc using Sage

259 views
Skip to first unread message

P Purkayastha

unread,
Jul 8, 2012, 7:03:47 PM7/8/12
to sage-s...@googlegroups.com
Look up the documentation here (or in your notebook interface) - there is a command called arc, and there is a command called polygon:



On Sunday, July 8, 2012 10:44:31 AM UTC-4, The Doctor (Michael) wrote:
Hullo.

I am new to Sage, but am hoping that Sage will provide a good alternative to Maple, and MatLab; and that I can use Sage in my new job.

Basically, I am now a solution author for Chegg and will be writing solutions for Calculus and Analysis textbooks mainly. For my 1st assignment, I need to be able to draw various regions (domains) to aid in setting up iterated integrals in rectangular and polar coordinates (and eventually spherical and cylcindrical coordinates as well).


Right now, I have 2 questions.

(1) I need to use Sage to graph three-fourths of a circle with radius = 4 (from 0 to 3*pi/2). Then I also need for the area between this arc and the x-axis (for Quadrants 1 and 2) and the y-axis (for Quadrant 3) to be shaded and I need x and y labels for the coordinate system.

(2) I need to be able to draw a region D, where D is a triangular region with vertices (0,0), (2,1), (0,3).

I have literally just started using Sage (and am not *terribly* efficient with MatLab, and Maple) and need some help here. Thanks,

-Michael D

kcrisman

unread,
Jul 10, 2012, 11:41:46 AM7/10/12
to sage-s...@googlegroups.com
You may find http://sagemath.org/doc/reference/sage/plot/graphics.html#sage.plot.graphics.Graphics.show and the stuff about legends, text, etc.  Read the doc carefully - there are a lot of examples!

On Tuesday, July 10, 2012 7:41:34 AM UTC-4, The Doctor (Michael) wrote:
Thanks for the help. For number (1) above I have the basic code, but need to add a label (R), the coordinate axis labels. and need to shade the area in. I am not quite sure how to do this. Here is my basic code. Please help. Thanks.

arc((0,0), 4, sector=(0,3*pi/2))

Pablo Fernandez

unread,
Jul 10, 2012, 12:07:58 PM7/10/12
to sage-s...@googlegroups.com
>
> On Tuesday, July 10, 2012 7:41:34 AM UTC-4, The Doctor (Michael) wrote:
>>
>> Thanks for the help. For number (1) above I have the basic code, but need
>> to add a label (R), the coordinate axis labels. and need to shade the area
>> in. I am not quite sure how to do this. Here is my basic code. Please help.
>> Thanks.
>>
>> arc((0,0), 4, sector=(0,3*pi/2))
>>

Hi!
I am rather new to sage too! I have tried your examples. I've used
polar_plot() instead of arc() for figure 1 (there is disk() aswell):


myArc = polar_plot(4, (0, 3*pi/2), ymin=-4, color='red', fill=True,
fillcolor= 'green', fillalpha=0.2, axes_labels=['X', 'Y'],
fontsize=15)

radiusLine = line2d([ (0,0), (2*sqrt(2), 2*sqrt(2)) ], color='black',
thickness=2)

mytext = text("R",(1.6, 2), fontsize = 20, color='black')

figure1 = myArc + radiusLine + mytext


And for figure 2, I used polygon():

figure2 = polygon([(0,0), (2,1), (0,3)], alpha=0.5)


Show up, figure 1:

figure1.show()

You can always save the figures in svg format for further editing with
a WYSIWYG svg editor. I use inkscape.

figure1.save('/home/username/Desktop/figure1.svg')

You can read the doc about any function right in the notebook. For
example, just type:

polygon?

Cheers! Pablo

P Purkayastha

unread,
Jul 12, 2012, 1:36:19 AM7/12/12
to sage-s...@googlegroups.com
Hi,

  Sage uses matplotlib to render arcs. Unfortunately, it seems matplotlib.patches.Arc objects can not be filled; see

As for labels for coordinate axes, look at the documentation. Your plot command will be like this:
a = arc((0,0), 4, sector=(0,3*pi/2))
a.axes_labels(['x', 'y'])

For a generic text label, you can use the text command:

t = text('an arc', (1,2))
(a+t).show() # this will combine and show the above two

It also seems that matplotlib's pyplot.title functionality is not exposed in Sage.

On Tuesday, July 10, 2012 7:48:17 PM UTC+8, The Doctor (Michael) wrote:
I have the code for a basic graph of number (1), but am unsure how to add a label (R), labels for the coordinate axes, and how to shade in the area under this curve. Any help would be greatly appreciated.

The Doctor (Michael)

unread,
Jul 12, 2012, 8:09:06 AM7/12/12
to sage-s...@googlegroups.com
Thanks Pablo! 

I guess I would also like to know how to add arrows for the coordinate axes, and thanks again!

Jason Grout

unread,
Jul 12, 2012, 9:08:09 AM7/12/12
to sage-s...@googlegroups.com
On 7/12/12 7:09 AM, The Doctor (Michael) wrote:
> I guess I would also like to know how to add arrows for the coordinate
> axes, and thanks again!

For now, you'd have to manually draw the arrows, like the answer to this
sage-support post:
https://groups.google.com/forum/?fromgroups#!topic/sage-support/N6hjJbNicjg

Here is a workaround from a matplotlib developer that uses straight
matplotlib: http://trac.sagemath.org/sage_trac/ticket/10740. You don't
necessarily need to understand this; I just post it in case a Sage
developer wants a pretty straightforward graphics project to enable
arrowheads on axes.

Thanks,

Jason



The Doctor (Michael)

unread,
Jul 12, 2012, 7:22:45 PM7/12/12
to sage-s...@googlegroups.com
I really appreciate all the help. I almost have what I am looking for, but when I run all the code, I do not get the graph that it should be. Here is my code for #1:

myArc=polar_plot(4,(0,3*pi/2),ymin=4,fill=true,fillcolor='blue',axes_labels=['x','y'],fontsize =16)

b=arrow((-4,0),(4,0),color="black")

c=arrow((0,-4),(0,4),color="black")

t=text("R",(2,2),fontsize=20,color='black')

figure1=myArc+b+c+t

figure1.show()

If someone would not ind pointing out where I am going wrong, I would be grateful, and many thanks!!!

-Michael D

Jason Grout

unread,
Jul 12, 2012, 11:58:35 PM7/12/12
to sage-s...@googlegroups.com
On 7/12/12 6:22 PM, The Doctor (Michael) wrote:
> myArc=polar_plot(4,(0,3*pi/2),ymin=4,fill=true,fillcolor='blue',axes_labels=['x','y'],fontsize
> =16)

Your ymin is 4. Shouldn't it be -4?

Thanks,

Jason



The Doctor (Michael)

unread,
Jul 15, 2012, 6:49:32 PM7/15/12
to sage-s...@googlegroups.com
Yes,that is correct Jason.Thanks. 

i have another question along the same vein. I need to plot a polar region between 2 circles of radius 1 and 2, from pi/4 <= theta <M= 3*pi/4. Here is what I tried, but I got a bunch of errors:

a=polar_plot(1,2,(pi/4,3*pi/4),ymin=-2,color='black',
fill=True,fillcolor='blue',axes_labels=['x','y'],fontsize =16)

Thanks,
-Michael D

achrzesz

unread,
Jul 16, 2012, 1:57:13 AM7/16/12
to sage-s...@googlegroups.com


On Monday, July 16, 2012 12:49:32 AM UTC+2, The Doctor (Michael) wrote:
Yes,that is correct Jason.Thanks. 

i have another question along the same vein. I need to plot a polar region between 2 circles of radius 1 and 2, from pi/4 <= theta <M= 3*pi/4. Here is what I tried, but I got a bunch of errors:

a=polar_plot(1,2,(pi/4,3*pi/4),ymin=-2,color='black',
fill=True,fillcolor='blue',axes_labels=['x','y'],fontsize =16)

Thanks,
-Michael D

l1=[(cos(phi),sin(phi)) for phi in srange(n(pi)/4,3*n(pi)/4,0.01)]
l2=[(2*cos(phi),2*sin(phi)) for phi in srange(n(pi)/4,3*n(pi)/4,0.01)]
l2.reverse()
p=polygon(l1+l2)
l=line(l1+l2+[(cos(n(pi)/4),sin(n(pi)/4))],color='black')
(p+l).show(xmin=-2,xmax=2,ymin=-2,ymax=2,axes_labels=['x','y'],fontsize =16)
 

The Doctor (Michael)

unread,
Jul 16, 2012, 11:05:47 AM7/16/12
to sage-s...@googlegroups.com
Thanks that is what I was looking or; with just one more thing.What would be the simplest way to add the circles of radius 1 and 2, and the lines theta = pi/4 and theta = 3*pi/4? Thanks again.

achrzesz

unread,
Jul 16, 2012, 12:06:32 PM7/16/12
to sage-s...@googlegroups.com
l1=[(cos(phi),sin(phi)) for phi in srange(n(pi)/4,3*n(pi)/4,0.01)]
l2=[(2*cos(phi),2*sin(phi)) for phi in srange(n(pi)/4,3*n(pi)/4,0.01)]
l2.reverse()
p=polygon(l1+l2)
c1=circle((0,0), 1,rgbcolor=(0,0,0))
c2=circle((0,0), 2,rgbcolor=(0,0,0))
l0=line([(-2*cos(n(pi)/4),2*sin(n(pi)/4)),(0,0),(2*cos(n(pi)/4),2*sin(n(pi)/4))],color='black')
(p+l0+c1+c2).show(xmin=-2,xmax=2,ymin=-2,ymax=2,axes_labels=['x','y'],fontsize =16)


The Doctor (Michael)

unread,
Jul 16, 2012, 2:27:25 PM7/16/12
to sage-s...@googlegroups.com
Thanks a lot. On my figure number (2) above:

And for figure 2, I used polygon(): 

figure2 = polygon([(0,0), (2,1), (0,3)], alpha=0.5) 

I have tried adding axes with:    axes_label=['x','y'], but sage ignores that request and prints the graph without any axes. How can I fix that? Thanks again,

-Michael D

The Doctor (Michael)

unread,
Jul 16, 2012, 2:43:43 PM7/16/12
to sage-s...@googlegroups.com
One additional thing:

i need to plot a circular region between 2 circles of radius 3 and 6, with 3*pi/2 to pi/2 and I have tried the following code but keep getting an Assertion error:

l1=[(3*cos(phi),3*sin(phi)) for phi in srange(3*n(pi)/2,n(pi)/2,0.01)]
l2=[(6*cos(phi),6*sin(phi)) for phi in srange(3*n(pi)/2,n(pi)/2,0.01)]
l2.reverse()
figure4=polygon(l1+l2)
figure4.show()
figure4.save('/home/thedoctor/Documents/figure4.svg')

Thanks a lot!

Jason Grout

unread,
Jul 16, 2012, 2:57:11 PM7/16/12
to sage-s...@googlegroups.com
On 7/16/12 1:43 PM, The Doctor (Michael) wrote:
> l1=[(3*cos(phi),3*sin(phi)) for phi in srange(3*n(pi)/2,n(pi)/2,0.01)]
> l2=[(6*cos(phi),6*sin(phi)) for phi in srange(3*n(pi)/2,n(pi)/2,0.01)]
> l2.reverse()

If I do

print l1+l2

here, I see your list is empty. Perhaps your sranges are empty?

Jason



achrzesz

unread,
Jul 16, 2012, 3:56:52 PM7/16/12
to sage-s...@googlegroups.com
l1=[(3*cos(phi),3*sin(phi)) for phi in srange(n(pi)/2,3*n(pi)/2,0.01)]
l2=[(6*cos(phi),6*sin(phi)) for phi in srange(n(pi)/2,3*n(pi)/2,0.01)]
l2.reverse()
figure4=polygon(l1+l2)
figure4.show(xmin=-6,xmax=6)

achrzesz

unread,
Jul 16, 2012, 4:10:39 PM7/16/12
to sage-s...@googlegroups.com
#You probably need the right-hand  sem-iring:

l1=[(3*cos(phi),3*sin(phi)) for phi in srange(-n(pi)/2,n(pi)/2,0.01)]
l2=[(6*cos(phi),6*sin(phi)) for phi in srange(-n(pi)/2,n(pi)/2,0.01)]
l2.reverse()
figure4=polygon(l1+l2)
figure4.show(xmin=-6,xmax=6)


achrzesz

unread,
Jul 16, 2012, 4:35:29 PM7/16/12
to sage-s...@googlegroups.com
# This command works for me:

polygon([(0,0), (2,1), (0,3)], alpha=0.5, axes_labels=['x','y'])

The Doctor (Michael)

unread,
Jul 17, 2012, 2:23:56 PM7/17/12
to sage-s...@googlegroups.com
Hullo.I have tried the code below, but I get no axes at all:

p=polygon([(-1,0), (1,0), (1,1)],alpha=0.5,axes_labes=['x','y'],fontsize=15) 

Not sure what I am doing wrong here.

Jason Grout

unread,
Jul 17, 2012, 2:44:27 PM7/17/12
to sage-s...@googlegroups.com
On 7/17/12 1:23 PM, The Doctor (Michael) wrote:
> Hullo.I have tried the code below, but I get no axes at all:
>
> p=polygon([(-1,0), (1,0),
> (1,1)],alpha=0.5,axes_labes=['x','y'],fontsize=15)

It seems that several times now, you've had typos in your code. It
might be a good idea to look carefully for those, and pay attention to
the warnings that indicate that you didn't spell an option correctly.
In fact, the warning I get when I run that code explicitly says:

verbose 0 (138: primitive.py, options) WARNING: Ignoring option
'axes_labes'=['x', 'y']

So that's a clue that you have a typo somewhere. Notice above that you
have "axes_labes" instead of "axes_labels".

Jason

Michael Dykes

unread,
Jul 17, 2012, 2:52:36 PM7/17/12
to sage-s...@googlegroups.com
Thanks Jason,

I saw an error message, but did not know exactly what it meant.i do now, and will look more carefully at my spelling and be more careful. I feel well, embarrassed to ask questions about code and have the error be a simple spelling error. Sorry, and thanks again,

-Michael D



--
To post to this group, send email to sage-s...@googlegroups.com
To unsubscribe from this group, send email to sage-support+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org



--
-In Christ,
-Michael D

Jason Grout

unread,
Jul 17, 2012, 6:41:41 PM7/17/12
to sage-s...@googlegroups.com
On 7/17/12 1:52 PM, Michael Dykes wrote:
> Thanks Jason,
>
> I saw an error message, but did not know exactly what it meant.i do now,
> and will look more carefully at my spelling and be more careful. I feel
> well, embarrassed to ask questions about code and have the error be a
> simple spelling error. Sorry, and thanks again,
>

No problem. I've certainly had my share of typo mistakes that I just
couldn't find until I asked for help.

Jason


Reply all
Reply to author
Forward
0 new messages