First time here? Check out the FAQ!

Ask Your Question
1

Rotate a 2d plot

asked 8 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 8 years ago

ndomes gravatar image

updated 8 years ago

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")
Preview: (hide)
link

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 ( 8 years ago )

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 ( 8 years ago )

I edited my answer.

ndomes gravatar imagendomes ( 8 years ago )

cool, thanks :)

Paul Bryan gravatar imagePaul Bryan ( 8 years ago )

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

ndomes gravatar imagendomes ( 8 years ago )

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: 8 years ago

Seen: 3,367 times

Last updated: Nov 19 '16