Skip to content Skip to sidebar Skip to footer

Is There A Way To Rotate A Matplotlib Plot By 45 Degrees?

I am looking for a way to rotate a plot generated in matplotlib-pyplot (Python libraries) by 45 degrees (so that instead of a square shape you would have a diamond shape, for examp

Solution 1:

Ok so currently the only partial solution I found is to apply a rotation to the plot. This allows to use the interactive interface that matplotlib/pyplot offers.

For dot plots like plot() and scatter() this is trivial, but I was specifically interested in rotating imshow(). This link discusses the transform keyword that could have potentially been used for this task, but it is apparently not working.

Fortunately, I found a workaround using pcolormesh(). pcolormesh() plots a quadrilateral mesh and allows you to specify the corner coordinates. So, the answer is to just apply the relevant transformations to the corner coordinates. Note however, that pcolormesh() works a bit different than imshow - it plots your matrix flipped.

I haven't seen this solution anywhere on the web, so here is some code for pcolormesh()/imshow() rotated by 45 degrees:

import matplotlib.pyplot as plt
import numpy as np

defpcolormesh_45deg(C):

    n = C.shape[0]
    # create rotation/scaling matrix
    t = np.array([[1,0.5],[-1,0.5]])
    # create coordinate matrix and transform it
    A = np.dot(np.array([(i[1],i[0]) for i in itertools.product(range(n,-1,-1),range(0,n+1,1))]),t)
    # plot
    plt.pcolormesh(A[:,1].reshape(n+1,n+1),A[:,0].reshape(n+1,n+1),np.flipud(C))

Solution 2:

Based on Bitwise anwer, you can use the following function:

defpcolormesh_45deg(C, ax=None, xticks=None, xticklabels=None, yticks=None,
                     yticklabels=None, aspect='equal', rotation=45,
                     *args, **kwargs):
    import itertools

    if ax isNone:
        ax = plt.gca()
    n = C.shape[0]
    # create rotation/scaling matrix
    t = np.array([[1, .5], [-1, .5]])
    # create coordinate matrix and transform it
    product = itertools.product(range(n, -1, -1), range(0, n + 1, 1))
    A = np.dot(np.array([(ii[1], ii[0]) for ii in product]), t)
    # plot
    ax.pcolormesh((2 * A[:, 1].reshape(n + 1, n + 1) - n),
                  A[:, 0].reshape(n + 1, n + 1),
                  np.flipud(C), *args, **kwargs)

    xticks = np.linspace(0, n - 1, n, dtype=int) if xticks isNoneelse xticks
    yticks = np.linspace(0, n - 1, n, dtype=int) if yticks isNoneelse yticks

    if xticks isnotNone:
        xticklabels = xticks if xticklabels isNoneelse xticklabels
        for tick, label, inzip(xticks, xticklabels):
            ax.scatter(-n + tick + .5, tick + .5, marker='x', color='k')
            ax.text(-n + tick + .5, tick + .5, label,
                    horizontalalignment='right', rotation=-rotation)
    if yticks isnotNone:
        yticklabels = yticks if yticklabels isNoneelse yticklabels
        for tick, label, inzip(yticks, yticklabels):
            ax.scatter(tick + .5, n - tick - .5, marker='x', color='k')
            ax.text(tick + .5, n - tick - .5, label,
                    horizontalalignment='left', rotation=rotation)

    if aspect:
        ax.set_aspect(aspect)
    ax.set_xlim(-n, n)
    ax.set_ylim(-n, n)
    ax.plot([-n, 0, n, 0., -n], [0, n, 0, -n, 0], color='k')
    ax.axis('off')
    return ax

Solution 3:

Perhaps if you do it on a 3D plot?

http://matplotlib.1069221.n5.nabble.com/How-to-rotate-a-3D-plot-td19185.html

axes3d.view_init(elev, azim)

Solution 4:

Have you looked at PIL?

This code will rotate an image. So if you first output the plot to a file as an image, you could then do

importImageimg= Image.open("plot.jpg")
img2 = img.rotate(45)
img2.show()
img2.save("rotate.jpg")

Solution 5:

This post suggests that you can only do it "by hand".

It's possible to draw it, but you would have to do all the transformations/rotations by hand, including drawing the axes as Line2D instances and labels as Text instances (see for example the "Scatter3D" example in the archives). There is no easy, built-in, way to do it, currently. In the planned refactoring of the axis handling,

Post a Comment for "Is There A Way To Rotate A Matplotlib Plot By 45 Degrees?"