有没有办法将 matplotlib 图旋转 45 度? [英] Is there a way to rotate a matplotlib plot by 45 degrees?

查看:36
本文介绍了有没有办法将 matplotlib 图旋转 45 度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将 matplotlib-pyplot(Python 库)中生成的绘图旋转 45 度的方法(例如,这样您将拥有菱形形状而不是方形),任何人都知道这是否可以完成了吗?

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.

我希望能够使用matplotlib交互功能,因此另存为图像然后旋转将无法正常工作.

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

另外,我想使用 pyplot 函数来绘制绘图,因此使用不同的库进行绘图并不是一个理想的解决方案.

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

推荐答案

好的,所以目前我找到的唯一部分解决方案是对绘图应用旋转.这允许使用matplotlib/pyplot提供的交互式界面.

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.

对于诸如plot()和scatter()之类的点图,这是微不足道的,但是我对旋转imshow()特别感兴趣.链接讨论了 transform 关键字,该关键字可能已用于此任务,但显然不起作用.

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.

幸运的是,我找到了使用pcolormesh()的解决方法.pcolormesh()绘制四边形网格,并允许您指定角坐标.因此,答案是仅将相关变换应用于角坐标.但是请注意,pcolormesh() 的工作方式与 imshow 略有不同 - 它绘制了翻转的矩阵.

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.

我在网上的任何地方都没有看到这个解决方案,所以这里有一些 pcolormesh()/imshow() 旋转 45 度的代码:

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))

这篇关于有没有办法将 matplotlib 图旋转 45 度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆