如何将matplotlib绘图旋转90度?

时间:2021-04-04 23:41:01

I have created a figure in matplotlib which contains three subplots, one in the top left quadrant, one in the top right quadrant, and one in the bottom right quadrant. The top right figure contains a two-d image, and the other two plots are the projection onto the Y and X axis respectively. I'd like to rotate the top left quadrant subplot through 90deg counterclockwise, so that the x-axis of that plot lies along the y-axis of the 2-d plot.

我在matplotlib中创建了一个图形它包含三个子图,一个在左上角,一个在右上角,一个在右下角。右上角是一个二维图像,另外两个图分别是在Y轴和X轴上的投影。我想把左上象限的子图沿逆时针方向旋转90deg,这样这个图的x轴就沿着二维图的y轴。

For the subplot, I realize I could flip the x and y data, rotate the axis labels, create a plot title on the left hand side, etc. But I was hoping to find a single call which would just rotate the whole, finished plot through 90deg. But I can't find one.

对于subplot,我意识到我可以翻转x和y数据,旋转轴标签,在左手边创建一个plot标题,等等。但我找不到。

Is there a simple way to do this?

有简单的方法吗?

4 个解决方案

#1


9  

Many of the pyplot 1D plots seem to have "orientation" or "pivot" options within their own arguments. For example, from matplotlib.org example of histogram:

pyplot 1D中的许多图在它们自己的参数中似乎都有“方向”或“轴心”选项。例如,来自matplotlib.org的直方图示例:

matplotlib.pyplot.hist(x, 
                       bins=10, 
                       range=None, 
                       normed=False, 
                       weights=None, 
                       cumulative=False, 
                       bottom=None, 
                       histtype=u'bar', 
                       align=u'mid', 
                       orientation=u'vertical', 
                       rwidth=None, 
                       log=False, 
                       color=None, 
                       label=None, 
                       stacked=False, 
                       hold=None, 
                       **kwargs)

Just change to horizontal (orientation=u'vertical')

只需改变水平方向(方向=u'vertical')

#2


4  

Another interesting parameter for a lot of functions is transform (unlike orientation or pivot this parameter can also be used in e.g. plot).

对于许多函数来说,另一个有趣的参数是转换(与方向或支点不同,这个参数也可以用于绘图)。

The transform parameter allows you to add a transformation, specified by a Transform object. For the sake of example, this is how you would rotate the plot of some random data:

转换参数允许您添加由转换对象指定的转换。举个例子,这是你如何旋转一些随机数据的图:

import numpy
from matplotlib import pyplot, transforms

data = numpy.random.randn(100)

# first of all, the base transformation of the data points is needed
base = pyplot.gca().transData
rot = transforms.Affine2D().rotate_deg(90)

# define transformed line
line = pyplot.plot(data, 'r--', transform= rot + base)
# or alternatively, use:
# line.set_transform(rot + base)

pyplot.show()

For an example on how to rotate a patch, see this answer, which was also the source of inspiration for this answer.

有关如何旋转补丁的示例,请参见这个答案,这也是这个答案的灵感来源。


update

I recently found out that the transform parameter does not work as expected when using pyplot.scatter (and other PathCollections). In this case, you might want to use the offset_transform. See this answer for more information on how to the offset_transform can be set.

我最近发现,当使用pyplot时,转换参数不像预期的那样工作。分散(和其他PathCollections)。在这种情况下,您可能需要使用offset_transform。有关如何设置offset_transform的更多信息,请参见此答案。

#3


3  

The easiest way I could imagine would be to use the scipy rotate method on the figure. However, this requires the Python Imaging Library, which is not available for Python 3.x.

我能想到的最简单的方法是在图上使用scipy rotate方法。但是,这需要Python映像库,这对于Python 3.x是不可用的。

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt

Your_Plot = plt.plot(X,Y)
Rotated_Plot = ndimage.rotate(Your_Plot, 90)

plt.figure(figsize=(12.5, 2.5))

plt.subplot(2,2,1)
plt.imshow(Rotated_Plot, cmap=plt.cm.gray)
plt.axis('off')

plt.show()

That's not a very nice way to do it though, I think a wrapper function or just manually swapping X and Y data, rotating axis labels, etc. would be far easier.

这并不是一个很好的方法,我认为包装函数或者只是手工交换X和Y数据,旋转轴标签等等会容易得多。

#4


0  

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig=plt.figure() 
ax=fig.add_subplot(111,projection='3d')

# for rotate the axes and update.
for angle in range(0,360): 
    ax.view_init(30,angle)

plt.show()

#1


9  

Many of the pyplot 1D plots seem to have "orientation" or "pivot" options within their own arguments. For example, from matplotlib.org example of histogram:

pyplot 1D中的许多图在它们自己的参数中似乎都有“方向”或“轴心”选项。例如,来自matplotlib.org的直方图示例:

matplotlib.pyplot.hist(x, 
                       bins=10, 
                       range=None, 
                       normed=False, 
                       weights=None, 
                       cumulative=False, 
                       bottom=None, 
                       histtype=u'bar', 
                       align=u'mid', 
                       orientation=u'vertical', 
                       rwidth=None, 
                       log=False, 
                       color=None, 
                       label=None, 
                       stacked=False, 
                       hold=None, 
                       **kwargs)

Just change to horizontal (orientation=u'vertical')

只需改变水平方向(方向=u'vertical')

#2


4  

Another interesting parameter for a lot of functions is transform (unlike orientation or pivot this parameter can also be used in e.g. plot).

对于许多函数来说,另一个有趣的参数是转换(与方向或支点不同,这个参数也可以用于绘图)。

The transform parameter allows you to add a transformation, specified by a Transform object. For the sake of example, this is how you would rotate the plot of some random data:

转换参数允许您添加由转换对象指定的转换。举个例子,这是你如何旋转一些随机数据的图:

import numpy
from matplotlib import pyplot, transforms

data = numpy.random.randn(100)

# first of all, the base transformation of the data points is needed
base = pyplot.gca().transData
rot = transforms.Affine2D().rotate_deg(90)

# define transformed line
line = pyplot.plot(data, 'r--', transform= rot + base)
# or alternatively, use:
# line.set_transform(rot + base)

pyplot.show()

For an example on how to rotate a patch, see this answer, which was also the source of inspiration for this answer.

有关如何旋转补丁的示例,请参见这个答案,这也是这个答案的灵感来源。


update

I recently found out that the transform parameter does not work as expected when using pyplot.scatter (and other PathCollections). In this case, you might want to use the offset_transform. See this answer for more information on how to the offset_transform can be set.

我最近发现,当使用pyplot时,转换参数不像预期的那样工作。分散(和其他PathCollections)。在这种情况下,您可能需要使用offset_transform。有关如何设置offset_transform的更多信息,请参见此答案。

#3


3  

The easiest way I could imagine would be to use the scipy rotate method on the figure. However, this requires the Python Imaging Library, which is not available for Python 3.x.

我能想到的最简单的方法是在图上使用scipy rotate方法。但是,这需要Python映像库,这对于Python 3.x是不可用的。

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt

Your_Plot = plt.plot(X,Y)
Rotated_Plot = ndimage.rotate(Your_Plot, 90)

plt.figure(figsize=(12.5, 2.5))

plt.subplot(2,2,1)
plt.imshow(Rotated_Plot, cmap=plt.cm.gray)
plt.axis('off')

plt.show()

That's not a very nice way to do it though, I think a wrapper function or just manually swapping X and Y data, rotating axis labels, etc. would be far easier.

这并不是一个很好的方法,我认为包装函数或者只是手工交换X和Y数据,旋转轴标签等等会容易得多。

#4


0  

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig=plt.figure() 
ax=fig.add_subplot(111,projection='3d')

# for rotate the axes and update.
for angle in range(0,360): 
    ax.view_init(30,angle)

plt.show()