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. enter image description here

我有不同的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.
enter image description here

Solutions and results

enter image description here

只要图表非常简单,给定的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?

enter image description here

enter image description here I would appreciate any hints to other running solutions - thank you!

推荐答案

我不确定我是否完全遵循您的例子,但您可以通过传递transform=ax.transData来告诉inset_axes使用数据坐标.

import numpy as np
import matplotlib.pyplot as plt


target_x = [12, 12, 42, 42]
target_y = [7, 13, 13, 7]

fig, ax = plt.subplots()

ax.scatter(target_x, target_y, color='lime')

x0 = target_x[0]
y0 = target_y[0]
width = target_x[2] - target_x[0]
height = target_y[2] - target_y[0]

ax_ins = ax.inset_axes([x0, y0, width, height], transform=ax.transData)

ax.set_xlim(0, 50)
ax.set_ylim(0, 20)

plt.show()

enter image description here

由于Matplotlib v3.6 inset_axes采用projectionpolar关键字,因此您可以通过这种方式创建极图作为插图.

Python相关问答推荐

两极按组颠倒顺序

如何推迟对没有公钥的视图/表的反射?

为什么使用SciPy中的Distance. cos函数比直接执行其Python代码更快?

使用多个性能指标执行循环特征消除

使文本输入中的文本与标签中的文本相同

有什么方法可以避免使用许多if陈述

用gekko解决的ADE方程系统突然不再工作,错误消息异常:@错误:模型文件未找到.& &

如何在Deliveryter笔记本中从同步上下文正确地安排和等待Delivercio代码中的结果?

Python:在类对象内的字典中更改所有键的索引,而不是仅更改一个键

如何标记Spacy中不包含特定符号的单词?

可变参数数量的重载类型(args或kwargs)

Pandas DataFrame中行之间的差异

海上重叠直方图

什么是最好的方法来切割一个相框到一个面具的第一个实例?

Python Pandas获取层次路径直到顶层管理

如何在Python中使用另一个数据框更改列值(列表)

在Python中调用变量(特别是Tkinter)

Polars将相同的自定义函数应用于组中的多个列,

在Python中使用yaml渲染(多行字符串)

Gunicorn无法启动Flask应用,因为无法将应用解析为属性名或函数调用.'"'' "