Question
受Matplotlib here中这个例子的启发,我想制作一个类似的例子.我遇到了以下问题:How can I relate the coordinates of the matplotlin graphics with my real world coordinates?
Example
用下图说明:
- 我需要
inset_axis
相当精确地位于红色点 - 我已经测试了所有四个角的
left_inset_ax = fig.add_axes([1, 0, .05, .05], facecolor='k')
个(参见黑色方块) - 在有色区域内,我可以使用局部坐标(0. 1、0.. 1),但我没有找到与外部坐标的关系
- phersilk我可以设置
myCoordinates=True
- I have also tried this example here, but I this is hand taylored, whereas I need an automated solution without hard coded figures.
我有不同的types of 100 graphics.其中之一是下面的.它们是在polar
设置中生成的
- plt.plot(DF_histo_cumsum.index,DF_histo_cumsum,c =' k ',lw=0.7),或通过
- b = ax0.bar(np.deg2rad(self.WD_cc),Nbar[:,j],Label=self.WD_cc,Color=self.ws_colors[j-1])
So they live in a different world of coordinates that is not easy to access for direct scaling.
Solutions and results
只要图表非常简单,给定的here看起来就有希望.我以它为基础添加更多功能,例如标题、x和y标签、等距非方形设置、不同数量的网格点.该方法是
-
生成2维数据
-
Select 其中的几个
inset_axis
-
转换为本地(0. 1)双向坐标
-
显示
inset_axis
-
使用不同数量的网格点
import numpy as np import matplotlib.pyplot as plt def scaleXY(xgr,ygr,xT,yT): xgr = (xgr-xT.min())/(xT.max()-xT.min()) ygr = (ygr-yT.min())/(yT.max()-yT.min()) return xgr, ygr nx, ny = 150, 60 # choose: number of data ix = np.linspace(-5, 20, nx)*100 iy = np.linspace(-1, 15, ny)*100 xq, yq = np.meshgrid(ix,iy, indexing='ij') # generate data dx, dy = 50, 30 # choose xp,yp = xq[::dx,::dy], yq[::dx,::dy] # select a few of them xgr, ygr = scaleXY(xp,yp,xq,yq) # scale them to (0..1) with plt.style.context('fast'): fig = plt.figure(figsize=(10,10)) ax1 = fig.add_subplot(111) ax1.scatter(xq,yq, s=5) # plot the base data ax1.scatter(xp,yp, c='lime', s=300, alpha = 0.6) # plot the testing points ins = ax1.inset_axes([xgr[2,1], ygr[2,1], 0.2,0.2]) # check the inset positioning ins = ax1.inset_axes([xgr[1,1], ygr[1,1], 0.2,0.2]) # check the inset positioning ins = ax1.inset_axes([xgr[0,0], ygr[0,0], 0.2,0.2]) # check the inset positioning ins = ax1.inset_axes([xgr[2,0], ygr[2,0], 0.2,0.2]) # check the inset positioning ax1.set_aspect('equal') title = 'Even more dangerous area' ax1.set_title(title,fontweight='bold', fontsize=17) ax1.set_xlabel('x-direction', fontsize=17) ax1.set_ylabel('y-direction', fontsize=17) plt.show()
作为results,可以看到图形的坐标与用户的坐标分开.这就引出了一个问题:How can I relate the coordinates of the matplotlin graphics with my real world coordinates?
I would appreciate any hints to other running solutions - thank you!