Vispy Axis在PAN/ZOOM之前的起始位置错误。

huangapple go评论91阅读模式
英文:

Vispy Axis starts in wrong position before PAN/ZOOM

问题

我正在尝试为图像显示 X 和 Y 轴。
在进行平移/缩放操作之前,图像的轴信息显示不正确。
但一旦您平移或缩放图像,轴就会正确显示。

下面我能够复制 Embed Vispy into QT 示例中的相同问题。

请查看修改后的代码如下:

  1. """
  2. 将 VisPy 嵌入 Qt
  3. ===================
  4. 在 PyQt5 应用程序中显示 VisPy 可视化。
  5. """

我能够复制 Embed Vispy into QT 示例中的相同问题。

请查看修改后的代码如下:

  1. """
  2. 将 VisPy 嵌入 Qt
  3. ===================
  4. 在 PyQt5 应用程序中显示 VisPy 可视化。
  5. """

我能够复制 Embed Vispy into QT 示例中的相同问题。

请查看修改后的代码如下:

  1. """
  2. 将 VisPy 嵌入 Qt
  3. ===================
  4. 在 PyQt5 应用程序中显示 VisPy 可视化。
  5. """
英文:

I am trying to show X Y axis for an image.
Axis information of the image shows incorrectly before pan/zoom activity.
But as soon as you pan or zoom the image, axis comes out properly.

Below I was able to replicate same issue with Embed Vispy into QT example.

Please find modified code below:

  1. """
  2. Embed VisPy into Qt
  3. ===================
  4. Display VisPy visualizations in a PyQt5 application.
  5. """
  6. import numpy as np
  7. from PyQt5 import QtWidgets
  8. from vispy.scene import SceneCanvas, visuals, AxisWidget
  9. from vispy.app import use_app
  10. IMAGE_SHAPE = (600, 800) # (height, width)
  11. CANVAS_SIZE = (800, 600) # (width, height)
  12. NUM_LINE_POINTS = 200
  13. class MyMainWindow(QtWidgets.QMainWindow):
  14. def __init__(self, *args, **kwargs):
  15. super().__init__(*args, **kwargs)
  16. central_widget = QtWidgets.QWidget()
  17. main_layout = QtWidgets.QHBoxLayout()
  18. self._controls = Controls()
  19. main_layout.addWidget(self._controls)
  20. self._canvas_wrapper = CanvasWrapper()
  21. main_layout.addWidget(self._canvas_wrapper.canvas.native)
  22. central_widget.setLayout(main_layout)
  23. self.setCentralWidget(central_widget)
  24. class Controls(QtWidgets.QWidget):
  25. def __init__(self, parent=None):
  26. super().__init__(parent)
  27. layout = QtWidgets.QVBoxLayout()
  28. self.colormap_label = QtWidgets.QLabel("Image Colormap:")
  29. layout.addWidget(self.colormap_label)
  30. self.colormap_chooser = QtWidgets.QComboBox()
  31. self.colormap_chooser.addItems(["viridis", "reds", "blues"])
  32. layout.addWidget(self.colormap_chooser)
  33. self.line_color_label = QtWidgets.QLabel("Line color:")
  34. layout.addWidget(self.line_color_label)
  35. self.line_color_chooser = QtWidgets.QComboBox()
  36. self.line_color_chooser.addItems(["black", "red", "blue"])
  37. layout.addWidget(self.line_color_chooser)
  38. layout.addStretch(1)
  39. self.setLayout(layout)
  40. class CanvasWrapper:
  41. def __init__(self):
  42. self.canvas = SceneCanvas(size=CANVAS_SIZE)
  43. self.grid = self.canvas.central_widget.add_grid()
  44. self.view_top = self.grid.add_view(0, 1, bgcolor='cyan')
  45. image_data = _generate_random_image_data(IMAGE_SHAPE)
  46. self.image = visuals.Image(
  47. image_data,
  48. texture_format="auto",
  49. cmap="viridis",
  50. parent=self.view_top.scene,
  51. )
  52. self.view_top.camera = "panzoom"
  53. self.view_top.camera.set_range(
  54. x=(0, IMAGE_SHAPE[1]), y=(0, IMAGE_SHAPE[0]), margin=0)
  55. self.x_axis = AxisWidget(
  56. axis_label="X Axis Label", orientation='bottom')
  57. self.x_axis.stretch = (1, 0.1)
  58. self.grid.add_widget(self.x_axis, row=1, col=1)
  59. self.grid.padding = 0
  60. self.x_axis.link_view(self.view_top)
  61. self.y_axis = AxisWidget(
  62. axis_label="Y Axis Label", orientation='left')
  63. self.y_axis.stretch = (0.1, 1)
  64. self.grid.add_widget(self.y_axis, row=0, col=0)
  65. self.y_axis.link_view(self.view_top)
  66. def _generate_random_image_data(shape, dtype=np.float32):
  67. rng = np.random.default_rng()
  68. data = rng.random(shape, dtype=dtype)
  69. return data
  70. def _generate_random_line_positions(num_points, dtype=np.float32):
  71. rng = np.random.default_rng()
  72. pos = np.empty((num_points, 2), dtype=np.float32)
  73. pos[:, 0] = np.arange(num_points)
  74. pos[:, 1] = rng.random((num_points,), dtype=dtype)
  75. return pos
  76. if __name__ == "__main__":
  77. app = use_app("pyqt5")
  78. app.create()
  79. win = MyMainWindow()
  80. win.show()
  81. app.run()

Below is the screenshot of the initial image which is generated.

Vispy Axis在PAN/ZOOM之前的起始位置错误。

Once we do a pan or zoom on the canvas the axis corrects itself.

Kindly let me know what is to be done to get proper axis for the image.

答案1

得分: 0

一个解决方案由David Hoese (@djhoese)提出,作为GitHub问题的评论

> 看起来这似乎与小部件添加到网格的顺序有关,而不是它们创建的时间。以下是__init__方法的新相关部分:

> image_data = _generate_random_image_data(IMAGE_SHAPE)
> self.image = visuals.Image(
> image_data,
> texture_format="auto",
> cmap="viridis",
> # parent=self.view_top.scene,
> )

> self.x_axis = AxisWidget(
> axis_label="X轴标签", orientation='bottom')
> self.x_axis.stretch = (1, 0.1)

> self.y_axis = AxisWidget(
> axis_label="Y轴标签", orientation='left')
> self.y_axis.stretch = (0.1, 1)

> self.grid.add_widget(self.x_axis, row=1, col=1)
> self.grid.add_widget(self.y_axis, row=0, col=0)
> self.view_top = self.grid.add_view(0, 1, bgcolor='cyan')
> self.image.parent = self.view_top.scene

> self.view_top.camera = "panzoom"
> self.x_axis.link_view(self.view_top)
> self.y_axis.link_view(self.view_top)

> self.view_top.camera.set_range(
> x=(0, IMAGE_SHAPE1), y=(0, IMAGE_SHAPE[0]), margin=0)

这对我有用,因为view_top在轴小部件之后添加。请注意,必须在链接视图之前声明/设置相机,否则它们将链接/附加到错误的转换以进行更改。

如果这对你有帮助,请记得感谢David提供的意见。

英文:

A solution was proposed by David Hoese (@djhoese) as a comment in Github issues:

> It really seems it has to do with the order the widgets are added to
> the grid not when they are created. Here's the new relevant portion of
> the init method:
>
> image_data = _generate_random_image_data(IMAGE_SHAPE)
> self.image = visuals.Image(
> image_data,
> texture_format="auto",
> cmap="viridis",
> # parent=self.view_top.scene,
> )
>
> self.x_axis = AxisWidget(
> axis_label="X Axis Label", orientation='bottom')
> self.x_axis.stretch = (1, 0.1)
>
> self.y_axis = AxisWidget(
> axis_label="Y Axis Label", orientation='left')
> self.y_axis.stretch = (0.1, 1)
>
> self.grid.add_widget(self.x_axis, row=1, col=1)
> self.grid.add_widget(self.y_axis, row=0, col=0)
> self.view_top = self.grid.add_view(0, 1, bgcolor='cyan')
> self.image.parent = self.view_top.scene
>
> self.view_top.camera = "panzoom"
> self.x_axis.link_view(self.view_top)
> self.y_axis.link_view(self.view_top)
>
> self.view_top.camera.set_range(
> x=(0, IMAGE_SHAPE1), y=(0, IMAGE_SHAPE[0]), margin=0)
>
> This works for me because the view_top is added after the axes
> widgets. Note that the camera has to be declared/set before the views
> are linked otherwise they are linked/attached to the wrong transform
> for changes.

If this helps you remember to thank David for his input.

huangapple
  • 本文由 发表于 2023年4月10日 21:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定