2017-04-27 59 views
1

我试图在matplotlib中创建一组Axes.annotate()。我希望这些注释偏离点(类似于textcoords='offset pixels'),但在‘axes fraction’而不是绝对像素数。注释点偏移轴的大小的一个固定分数

使用数据坐标的问题是,当使用对数(或其他)尺度时,它会变得杂乱。

offset pixels的问题在于,如果更改图形的大小或dpi,外观会发生变化。

下面是概括的问题的尝试:

fig, axs = plt.subplots(1,2) 
axs[0].plot([1,2],[10,100]) 
axs[1].semilogy([1,2],[10,100]) 
for ax in axs: 
    ax.annotate('', xy=(1,10), xytext=(1,50), textcoords=('data', 'offset pixels'),arrowprops={'arrowstyle':'-', 'lw':3}) 
plt.show() 

fig, axs = plt.subplots(1,2, dpi=200) 
axs[0].plot([1,2],[10,100]) 
axs[1].semilogy([1,2],[10,100]) 
for ax in axs: 
    ax.annotate('', xy=(1,10), xytext=(1,50), textcoords=('data', 'offset pixels'),arrowprops={'arrowstyle':'-', 'lw':3}) 
plt.show() 

enter image description here enter image description here

我想有注解行具有相同的长度(相对于图的大小),不管图形的大小,还是轴的缩放比例。

这可能吗?

回答

2

我设法通过Matplotlib's Transformation Tutoria升,其中已经包含了我所需要的一切寻找解决我的问题。

要绘制相对于坐标轴长度恒定的东西,我使用了Axes.transLimits对象。但之前,我必须警惕我处理对数坐标轴的可能性,因此我还会使用对象来考虑坐标轴的(可能的)非线性缩放比例。

我在最后使用的代码是获取我的初始点的“轴”坐标,然后从该点开始简单绘图到该点的某个偏移量,指定我的坐标用轴表示坐标系

xax,yax = ax.transLimits.transform(ax.transScale.transform([x,y])) 
ax.plot([xax,xax], [y,y+offset], transform=ax.transAxes) 
0

如果要以轴单位为单位指定注释偏移量,请执行此操作。以下产生lentgh 20%轴分数线。

ax.annotate('', xy=(1,10), xytext=(1,0.2), textcoords=('data', 'axes fraction') 

enter image description here enter image description here

+0

使用“轴分数”作为你没有指定的行的端部的,所述y坐标应为坐标轴的高度,这是不一样的20%之说的长度该线应该是20%高,还是我误解了? –