Python 如何将 matplotlib 图旋转 90 度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22540449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I rotate a matplotlib plot through 90 degrees?
提问by user3443148
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 轴上的投影。我想将左上象限子图逆时针旋转 90 度,以便该图的 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.
对于子图,我意识到我可以翻转 x 和 y 数据,旋转轴标签,在左侧创建一个图标题等。但我希望找到一个可以旋转整个完成图的调用通过 90 度。但我找不到一个。
Is there a simple way to do this?
有没有一种简单的方法可以做到这一点?
回答by nicsqr
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 一维图似乎在它们自己的参数中都有“方向”或“枢轴”选项。例如,来自 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')
只需更改为水平 ( orientation=u'vertical')
回答by Mr Tsjolder
Another interesting parameter for a lot of functions is transform(unlike orientationor pivotthis parameter can also be used in e.g. plot).
许多函数的另一个有趣参数是transform(与orientationor不同,pivot此参数也可用于 eg plot)。
The transformparameter allows you to add a transformation, specified by a Transformobject. For the sake of example, this is how you would rotate the plot of some random data:
该transform参数允许您添加由Transform对象指定的转换。举例来说,这是旋转一些随机数据图的方式:
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 transformparameter 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 answerfor more information on how to the offset_transformcan be set.
我最近发现该transform参数在使用pyplot.scatter(和其他PathCollections)时没有按预期工作。在这种情况下,您可能希望使用offset_transform. 有关如何设置的更多信息,请参阅此答案offset_transform。
回答by sameer_nubia
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()

