[英]How can I rotate a matplotlib plot through 90 degrees?
我在 matplotlib 中創建了一個圖形,其中包含三個子圖,一個在左上象限,一個在右上象限,一個在右下象限。 右上角的圖包含一個二維圖像,另外兩個圖分別是在 Y 軸和 X 軸上的投影。 我想將左上象限子圖逆時針旋轉 90 度,以便該圖的 x 軸沿着二維圖的 y 軸。
對於子圖,我意識到我可以翻轉 x 和 y 數據,旋轉軸標簽,在左側創建一個圖標題等。但我希望找到一個可以旋轉整個完成圖的調用通過 90 度。 但我找不到一個。
有沒有一種簡單的方法可以做到這一點?
許多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'
)
許多函數的另一個有趣參數是transform
(與orientation
或pivot
不同,此參數也可用於例如plot
)。
transform
參數允許您添加由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()
有關如何旋轉補丁的示例,請參閱此答案 ,這也是此答案的靈感來源。
我最近發現使用pyplot.scatter
(和其他PathCollections
)時, transform
參數無法正常工作。 在這種情況下,您可能希望使用offset_transform
。 有關如何設置offset_transform
更多信息,請參閱此答案 。
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()
很簡單:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.