在matplotlib python

时间:2019-05-14 01:31:45

标签: python matplotlib plot axes affinetransform

我正在尝试绘制相对于现有轴旋转的图像。 当前,我正在使用matplotlib-但是,如果需要切换到其他位置,则可以(只要可以从Python调用即可)。

我已经尝试过使用float_axes和简单的Affine2D,但努力理解如何控制新轴的位置并在其中显示图形。在这两种情况下,我似乎都缺少一些非常简单的东西-但无法知道可能会有什么。

图1

#%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import numpy as np
fig,ax = plt.subplots()
y=[1,2,3,0,5,3,2,1,2,3,2,1,2,3,2,1,2,3,3,4]
x=np.arange(len(y))
plt.plot(x,y)
ax.set_xlim((0,np.max(x)+1))
ax.set_ylim((0,np.max(y)))
ax.set_xticks(x)

plot_extents = 0, 10, 0, 10
transform = Affine2D().scale(1,1).rotate_deg(20)
helper = floating_axes.GridHelperCurveLinear(transform, plot_extents)
ax1 = floating_axes.FloatingSubplot(fig, 111, grid_helper=helper)

fig.add_subplot(ax1)
aux_ax = ax1.get_aux_axes(transform)

aux_ax.imshow(np.arange(100).reshape([10,10]))
plt.show()

Figure2

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

def do_plot(ax, Z, transform):
    im = ax.imshow(Z, interpolation='none',
                   origin='lower',
                   extent=[-3, 3, -4, 2], clip_on=True)

    trans_data = transform + ax.transData
    im.set_transform(trans_data)

    # display intended extent of the image
    x1, x2, y1, y2 = im.get_extent()
    ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "y--",
            transform=trans_data)
    ax.set_xlim(-5, 5)
    ax.set_ylim(-6, 6)


# prepare image and figure
fig, ax = plt.subplots()
ax.plot(np.arange(9)-4,np.random.permutation(9)-4)
Z = np.arange(100).reshape([10,10])

# image rotation
do_plot(ax, Z, mtransforms.Affine2D().scale(1,1).rotate_deg(30))

plt.show()

图1 看起来像这样:

figure1

如您所见,其imshow图相对于其轴偏移(这在图或散点图上没有发生)。改变角度也会改变浮动轴的原点(相对于主轴),我不知道该如何控制。

图2 看起来像这样:

enter image description here

这一次我看不到浮动轴点-所以不知道图像绘图是否关闭-但也不清楚如何控制偏移量。

我要构建的东西是这样的:

fig,ax=plt.subplots()
ax.plot([1,2,3,4,5],[5,2,1,2,5])
rotated_ax = ax.create_new_axes(originx=2.6, originy=3.5,\
                                width=10, hight=9, rotation=20)
#or alternatively
rotated_ax = ax.create_new_axes(originx=2.6, originy=3.5,\
                     right_top_corner_x=12, right_top_corner_y=13, projection='rect')
rotated_ax.imshow(np.arange(90).reshape([10,9]))
plt.show()

这应该在原始轴上绘制一条线。 然后在其顶部创建新轴,使其左下角(原点)位于点(2.6,3.5),高度为9,宽度= 10,并逆时针旋转20度。

我确定上面的描述中缺少某些内容-但我很高兴有人澄清并提出一些可能的解决方法。

0 个答案:

没有答案