Ask Your Question
1

Rotate a 2d plot

asked 2016-11-16 14:35:41 +0100

Paul Bryan gravatar image

I have a 2d plot with multiple arrows, lines etc. and I would like to rotate the entire thing. Here's a simple example without rotation. The real example has more objects on it so I'd rather rotate the entire plot than each individual object.

O = vector((0, 0))
Tp = vector((1, 0))
Np = vector((0, 1))

p = plot([])

p += arrow2d(O, Tp, color="blue")
p += arrow2d(O, Np, color="blue")

p.show(axes=False, aspect_ratio=1)

How can I go about rotating the entire plot by a fixed angle? I have only been able to find information about rotations for 3d plots.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-11-16 15:05:57 +0100

ndomes gravatar image

updated 2016-11-19 00:07:44 +0100

For example:

O = vector((0, 0))
Tp = vector((1, 0))
Np = vector((0, 1))

scene = [[Tp,Np],[Tp+Np,-Np]]

alpha = pi/6
R = matrix(2,2,[cos(alpha),-sin(alpha),sin(alpha),cos(alpha)])

p = Graphics()
for obj in scene:
    p += sum([plot(v) for v in obj])
    p += sum([plot(R*v,color='red') for v in obj])

p.show(axes=False, aspect_ratio=1)

Supplement: You can use PIL to rotate an entire plot. PIL is included in Sage.

P1 = plot(x^2,-2,2,thickness=2)
P1.save('./data/test1.png',xmin=-2,xmax=2,ymin=-2,ymax=2,axes=False)
P2 = point((0,0),color='black',transparent=True) # a dummy plot to draw the axes
P2.save('./data/test2.png',xmin=-2,xmax=2,ymin=-2,ymax=2)
from PIL import Image
background = Image.open(DATA+"test2.png")
overlay = Image.open(DATA+"test1.png")
overlay = overlay.rotate(-45).resize(background.size)
new_img = Image.blend(background, overlay, 0.7)
new_img.save("new.png")
edit flag offensive delete link more

Comments

Thanks! That's effectively what I have done - rotate each object, and it's not so bad if you do it the way you have.

Paul Bryan gravatar imagePaul Bryan ( 2016-11-16 15:14:59 +0100 )edit

Actually, although this solution answers the example in the question, it does not seem to solve my general problem. I have other kinds of graphics objects such as lines, polygons, text labels etc. and some of the vectors will not be based at the same point. I want to be able to rotate the entire scene including such objects. So I essentially need to be able to rotate the entire viewport about a point. At this stage, my solution is to save the image and rotate it using an external editor, but it would certainly by preferable to be able to perform transformations on entire plots from within sage. I think matplotlib has this kind of functionality, but I don't yet know how to achieve it.

Paul Bryan gravatar imagePaul Bryan ( 2016-11-18 15:24:00 +0100 )edit

I edited my answer.

ndomes gravatar imagendomes ( 2016-11-19 00:08:10 +0100 )edit

cool, thanks :)

Paul Bryan gravatar imagePaul Bryan ( 2016-11-19 21:50:35 +0100 )edit

One more solution (pure sage): http://sagecell.sagemath.org/?q=rizsql

ndomes gravatar imagendomes ( 2016-11-19 22:00:07 +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: 2016-11-16 14:35:41 +0100

Seen: 3,062 times

Last updated: Nov 19 '16