# Rotate a 2d plot

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

Sort by » oldest newest most voted 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")

more

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.

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.