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 example), anyone know if this can be done?

One way I can think of is to use a rotation filter on all the data so that it appears rotated, but then the plot itself will still be in the original orientation.

I want to be able to use the matplotlib interactive features, so saving as an image and then rotating won't work.

Also, I want to use pyplot functions to draw the plot, so using a different library for the plotting is not an ideal solution.

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
def pcolormesh_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))
```

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

```
def pcolormesh_45deg(C, ax=None, xticks=None, xticklabels=None, yticks=None,
yticklabels=None, aspect='equal', rotation=45,
*args, **kwargs):
import itertools
if ax is None:
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 is None else xticks
yticks = np.linspace(0, n - 1, n, dtype=int) if yticks is None else yticks
if xticks is not None:
xticklabels = xticks if xticklabels is None else xticklabels
for tick, label, in zip(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 is not None:
yticklabels = yticks if yticklabels is None else yticklabels
for tick, label, in zip(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
```

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow