在努力改进我对 this question 的回答时,我跌跌撞撞地走进了死胡同。
我想要实现的,是在 matplotlib 中创建一个“假的”3D 瀑布图,其中各个线图(或可能的任何其他图类型)在图像素坐标并绘制在彼此后面。这部分已经工作正常,并且使用我的代码示例(见下文)你应该能够绘制十个 等效 线,它们在 x 中被 fig.dpi/10. 偏移- 和 y 方向,并通过 zorder 绘制在彼此后面。
请注意,我还添加了 fill_between() 以使“深度提示”zorder 更加可见。
我卡住的地方是我想添加一个“第三轴”,即一条线(稍后可能用一些刻度线格式化)与基线正确对齐(即 [ 0,0] 每行的数据单位)。
由于这不是一次性的事情(即解决方案不应该只能在静态像素坐标中工作),但必须在重新缩放时正确运行,所以这个问题可能会变得更加复杂, 尤其是在交互工作时。 如您所见,设置例如xlim 允许“按预期”重新缩放线条(如果您以交互方式尝试最好),但我尝试插入的红线( future 轴)并没有以相同的方式转置作为每个线图的基础。
我不是在寻找是依赖于 mpl_toolkits.mplot3d 的 Axes3D 的解决方案,因为这会导致 关于 zorder 和缩放的许多其他问题,这正是我想通过提出我自己的“假 3D 绘图”试图避免的问题。
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D,IdentityTransform
def offset(myFig,myAx,n=1,xOff=60,yOff=60):
"""
this function will apply a shift of n*dx, n*dy
where e.g. n=2, xOff=10 would yield a 20px offset in x-direction
"""
## scale by fig.dpi to have offset in pixels!
dx, dy = xOff/myFig.dpi , yOff/myFig.dpi
t_data = myAx.transData
t_off = mpl.transforms.ScaledTranslation( n*dx, n*dy, myFig.dpi_scale_trans)
return t_data + t_off
fig,axes=plt.subplots(nrows=1, ncols=3,figsize=(10,5))
ys=np.arange(0,5,0.5)
print(len(ys))
## just to have the lines colored in some uniform way
cmap = mpl.cm.get_cmap('viridis')
norm=mpl.colors.Normalize(vmin=ys.min(),vmax=ys.max())
## this defines the offset in pixels
xOff=10
yOff=10
for ax in axes:
## plot the lines
for yi,yv in enumerate(ys):
zo=(len(ys)-yi)
ax.plot([0,0.5,1],[0,1,0],color=cmap(norm(yv)),
zorder=zo, ## to order them "behind" each other
## here we apply the offset to each plot:
transform=offset(fig,ax,n=yi,xOff=xOff,yOff=yOff)
)
### optional: add a fill_between to make layering more obvious
ax.fill_between([0,0.5,1],[0,1,0],0,
facecolor=cmap(norm(yv)),edgecolor="None",alpha=0.1,
zorder=zo-1, ## to order them "behind" each other
## here we apply the offset to each plot:
transform=offset(fig,ax,n=yi,xOff=xOff,yOff=yOff)
)
##################################
####### this is the important bit:
ax.plot([0,2],[0,2],color='r',zorder=100,clip_on=False,
transform=ax.transData+mpl.transforms.ScaledTranslation(0.,0., fig.dpi_scale_trans)
)
## make sure to set them "manually", as autoscaling will fail due to transformations
for ax in axes:
ax.set_ylim(0,2)
axes[0].set_xlim(0,1)
axes[1].set_xlim(0,2)
axes[2].set_xlim(0,3)
### Note: the default fig.dpi is 100, hence an offset of of xOff=10px will become 30px when saving at 300dpi!
# plt.savefig("./test.png",dpi=300)
plt.show()
更新:
我现在在下面添加了一个动画,它显示了堆叠线在缩放/平移时的表现,以及它们的“基线”(蓝色圆圈)如何随绘图移动,而不是静态的 OriginLineTrans 解决方案(绿线)或我的转换线(红色,虚线)。
附着点观察到不同的变换并且可以通过以下方式插入:
ax.scatter([0],[0],edgecolors="b",zorder=200,facecolors="None",s=10**2,)
ax.scatter([0],[0],edgecolors="b",zorder=200,facecolors="None",s=10**2,transform=offset(fig,ax,n=len(ys)-1,xOff=xOff,yOff=yOff),label="attachment points")
请您参考如下方法:
问题归结为以下内容:
如何制作一条线
- 从轴坐标中的原点 (0,0) 开始
- 在物理坐标(像素空间)中以
角度的角度进化
通过使用 matpotlib 转换?
问题是轴坐标中的原点可能会因子图位置而异。所以我看到的唯一选择是创建一些自定义转换
- 转换为像素空间
- 转换为像素空间中的原点
- 按给定角度倾斜坐标系(例如,在 x 方向)
- 转换回轴的原点
可能是这样的
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtrans
class OriginLineTrans(mtrans.Transform):
input_dims = 2
output_dims = 2
def __init__(self, origin, angle, axes):
self.axes = axes
self.origin = origin
self.angle = angle # in radiants
super().__init__()
def get_affine(self):
origin = ax.transAxes.transform_point(self.origin)
trans = ax.transAxes + \
mtrans.Affine2D().translate(*(-origin)) \
.skew(self.angle, 0).translate(*origin)
return trans.get_affine()
fig, ax = plt.subplots()
ax.plot([0,0], [0,1], transform=OriginLineTrans((0,0), np.arctan(1), ax))
plt.show()
请注意,在原始问题的情况下,角度为 np.arctan(dx/dy)。
