我在matplotlib中创建了一个图形,其中包含三个子图,一个在左上象限,一个在右上象限,一个在右下象限。右上图包含一个二维图像,另外两个图分别是Y轴和X轴上的投影。我想将左上象限子图逆时针旋转90度,以使该图的x轴位于2-d图的y轴上。
对于子图,我意识到我可以翻转x和y数据,旋转轴标签,在左侧创建一个情节标题,等等。但是我希望找到一个只能旋转整个调用的单个调用,完成了90deg的情节。但我找不到一个。
有一种简单的方法吗?
答案 0 :(得分:9)
许多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)
只需更改为水平(orientation=u'vertical'
)
答案 1 :(得分:7)
许多函数的另一个有趣参数是transform
(与orientation
或pivot
不同,此参数也可用于plot
)。
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()
有关如何旋转补丁的示例,请参阅this answer,这也是此答案的灵感来源。
我最近发现使用transform
(和其他pyplot.scatter
)时,PathCollections
参数无效。在这种情况下,您可能希望使用offset_transform
。有关如何设置offset_transform
的详细信息,请参阅this answer。
答案 2 :(得分:1)
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()