【问题标题】:How do I change the orientation of my x,y plane?如何更改 x,y 平面的方向?
【发布时间】:2022-10-23 18:58:04
【问题描述】:

我使用带有 Raspberry Pi 的 MLX90640 热像仪创建了代码。

代码如下所示:

import time,board,busio
import numpy as np
import adafruit_mlx90640
import matplotlib.pyplot as plt

print("Initializing MLX90640")
i2c = busio.I2C(board.SCL, board.SDA, frequency=800000) # setup I2C
mlx = adafruit_mlx90640.MLX90640(i2c) # begin MLX90640 with I2C comm
mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ # set refresh rate
mlx_shape = (24,32)
print("Initialized")

# setup the figure for plotting
plt.ion() # enables interactive plotting
fig,ax = plt.subplots(figsize=(12,7))
therm1 = ax.imshow(np.zeros(mlx_shape),vmin=0,vmax=60) #start plot with zeros
cbar = fig.colorbar(therm1) # setup colorbar for temps
cbar.set_label('Temperature [$^{\circ}$C]',fontsize=14) # colorbar label

frame = np.zeros((24*32,)) # setup array for storing all 768 temperatures
t_array = []
print("Starting loop")
while True:
    t1 = time.monotonic()
    try:
        mlx.getFrame(frame) # read MLX temperatures into frame var
        data_array = (np.reshape(frame,mlx_shape)) # reshape to 24x32
        therm1.set_data(np.fliplr(data_array)) # flip left to right
        therm1.set_clim(vmin=np.min(data_array),vmax=np.max(data_array)) # set bounds
        cbar.update_normal(therm1) # update colorbar range
        plt.title(f"Max Temp: {np.max(data_array):.1f}C")
        plt.pause(0.001) # required
        #fig.savefig('mlx90640_test_fliplr.png',dpi=300,facecolor='#FCFCFC', bbox_inches='tight') # comment out to speed up
        t_array.append(time.monotonic()-t1)
        print('Sample Rate: {0:2.1f}fps'.format(len(t_array)/np.sum(t_array)))
    except ValueError:
        continue # if error, just read again

它展示了这个输出:

Output of video

在右上角,您可以看到检测到的最高热量的 x 和 y 坐标。

我遇到的问题是根据图像中间而不是左下角来定位坐标。

Here is an example of what I'm trying to get

我正在尝试将 x 和 y 点从输出中间的 (0,0) 定位,并与图形分开打印 x 和 y 坐标。

我确定有一行代码我可以输入或更改,但我遇到了最严重的麻烦。

【问题讨论】:

    标签: python numpy matplotlib coordinates figure


    【解决方案1】:

    使用extent

    extent = (-16, 16, 12.5, -12.5)
    

    您可以在imshow 中使用它:

    therm1 = ax.imshow(np.zeros(mlx_shape),vmin=0,vmax=60, extent=extent) #start plot with zeros
    

    或直接在therm1

    therm1.set(extent=extent) # matplotlib version 3.5+
    therm1.set_extent(extent) # version 3.4
    

    【讨论】:

      【解决方案2】:

      这是你想要的吗?

      In [76]: fig, ax = plt.subplots(layout='constrained')
          ...: ax.imshow(img, extent=(-12.5,11.5,-17.5,18.5))
          ...: ax.spines[["left", "bottom"]].set_position(("data", 0))
          ...: ax.spines[["top", "right"]].set_visible(False)
          ...: ax.spines[["left", "bottom"]].set_color('w')
          ...: ax.tick_params(axis='both', which='both', color='w')
          ...: for label in ax.xaxis.get_majorticklabels():
          ...:     label.set_color('w')
          ...:     label.set_bbox(dict(facecolor='k', alpha=.3))
          ...: for label in ax.yaxis.get_majorticklabels():
          ...:      label.set_color('w')
          ...:      label.set_bbox(dict(facecolor='k', alpha=.3))
      

      哦,是的,刻度标签周围的较暗边缘很烦人,但目前我不知道如何删除它们:我可以调查 OP 是否告诉我这是几乎他们想要什么...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-16
        • 1970-01-01
        • 2012-03-28
        • 2017-07-27
        • 1970-01-01
        • 2021-12-02
        • 2013-02-26
        相关资源
        最近更新 更多