matplotlib.widgets.TextBox 在包含多个子图的图中交互速度较慢。

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

matplotlib.widgets.TextBox interaction is slow when figure contains several subplots

问题

以下是演示问题的Python代码。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.gridspec as gridspec
  4. from matplotlib.widgets import TextBox
  5. class Textbox_Demo(object):
  6. def __init__(self):
  7. self.fig = plt.figure(figsize=(8,8))
  8. self.string = 'label'
  9. self.rows = 5 # reducing rows speeds up textbox interaction
  10. self.cols = 5 # reducing cols speeds up textbox interaction
  11. self.plot_count = self.rows * self.cols
  12. self.gs = gridspec.GridSpec(self.rows, self.cols,
  13. left=0.05, right=1-0.02, top=1-0.02, bottom=0.10, wspace=0.3, hspace=0.4)
  14. for k in range(self.plot_count):
  15. ax = self.fig.add_subplot(self.gs[k])
  16. #ax.set_xticks([]) # showing axes slows textbox interaction
  17. #ax.set_yticks([]) # showing axes slows textbox interaction
  18. data = np.atleast_2d(np.sin(np.linspace(1,255,255) * 50))
  19. ax.imshow(data, aspect="auto", cmap='ocean')
  20. # this is the user-input textbox
  21. tb_axis = plt.axes([0.125, 0.02, 0.8, 0.05])
  22. self.tb = TextBox(tb_axis, 'Enter label:', initial=self.string, label_pad=0.01)
  23. self.tb.on_submit(self.on_submit)
  24. plt.show()
  25. def on_submit(self, text):
  26. pass
  27. if __name__ == "__main__":
  28. Textbox_Demo()
英文:

Below is python code to demonstrate the problem.<br>
If there are 2 rows and 2 columns of images, for example, typing/erasing in the textbox is reasonably fast. However, if there are 5 rows and 5 columns, typing/erasing in the textbox is quite slow. If the xticks and yticks are drawn, interaction is even slower. So, it seems as if the entire figure is redrawn after every keystroke.<br>
Is there a solution for this (apart from putting the textbox on a separate figure)?<br>
(My development platform is MacOS Mojave, Python 3.7.5.)

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.gridspec as gridspec
  4. from matplotlib.widgets import TextBox
  5. class Textbox_Demo(object):
  6. def __init__(self):
  7. self.fig = plt.figure(figsize=(8,8))
  8. self.string = &#39;label&#39;
  9. self.rows = 5 # reducing rows speeds up textbox interaction
  10. self.cols = 5 # reducing cols speeds up textbox interaction
  11. self.plot_count = self.rows * self.cols
  12. self.gs = gridspec.GridSpec(self.rows, self.cols,
  13. left=0.05, right=1-0.02, top=1-.02, bottom=0.10, wspace=0.3, hspace=0.4)
  14. for k in range(self.plot_count):
  15. ax = self.fig.add_subplot(self.gs[k])
  16. #ax.set_xticks([]) # showing axes slows textbox interaction
  17. #ax.set_yticks([]) # showing axes slows textbox interaction
  18. data = np.atleast_2d(np.sin(np.linspace(1,255,255) * 50))
  19. ax.imshow(data, aspect=&quot;auto&quot;, cmap=&#39;ocean&#39;)
  20. # this is the user-input textbox
  21. tb_axis = plt.axes([0.125, 0.02, 0.8, 0.05])
  22. self.tb = TextBox(tb_axis, &#39;Enter label:&#39;, initial=self.string, label_pad=0.01)
  23. self.tb.on_submit(self.on_submit)
  24. plt.show()
  25. def on_submit(self, text):
  26. pass
  27. if __name__ == &quot;__main__&quot;:
  28. Textbox_Demo()

答案1

得分: 2

Matplotlib的TextBox本质上较慢,因为它使用matplotlib本身提供的绘图工具,因此在更改时重新绘制整个图形。

我建议使用GUI工具包中的文本框。例如,对于PyQt,可能会像这样:

  1. import numpy as np
  2. import sys
  3. from matplotlib.backends.backend_qt5agg import (
  4. FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
  5. from matplotlib.backends.qt_compat import QtCore, QtWidgets
  6. import matplotlib.gridspec as gridspec
  7. from matplotlib.figure import Figure
  8. class Textbox_Demo(QtWidgets.QMainWindow):
  9. def __init__(self):
  10. super().__init__()
  11. self._main = QtWidgets.QWidget()
  12. self.setCentralWidget(self._main)
  13. layout = QtWidgets.QVBoxLayout(self._main)
  14. layout.setContentsMargins(0, 0, 0, 0)
  15. layout.setSpacing(0)
  16. self.fig = Figure(figsize=(8, 8))
  17. self.canvas = FigureCanvas(self.fig)
  18. layout.addWidget(self.canvas)
  19. self.addToolBar(NavigationToolbar(self.canvas, self))
  20. self._textwidget = QtWidgets.QWidget()
  21. textlayout = QtWidgets.QHBoxLayout(self._textwidget)
  22. self.textbox = QtWidgets.QLineEdit(self)
  23. self.textbox.editingFinished.connect(self.on_submit)
  24. # or, if wanting to have changed apply directly:
  25. # self.textbox.textEdited.connect(self.on_submit)
  26. textlayout.addWidget(QtWidgets.QLabel("Enter Text: "))
  27. textlayout.addWidget(self.textbox)
  28. layout.addWidget(self._textwidget)
  29. self.fill_figure()
  30. def fill_figure(self):
  31. self.string = 'label'
  32. self.rows = 5 # reducing rows speeds up textbox interaction
  33. self.cols = 5 # reducing cols speeds up textbox interaction
  34. self.plot_count = self.rows * self.cols
  35. self.gs = gridspec.GridSpec(self.rows, self.cols,
  36. left=0.05, right=1-0.02, top=1-.02, bottom=0.10, wspace=0.3, hspace=0.4)
  37. for k in range(self.plot_count):
  38. ax = self.fig.add_subplot(self.gs[k])
  39. # ax.set_xticks([]) # showing axes slows textbox interaction
  40. # ax.set_yticks([]) # showing axes slows textbox interaction
  41. data = np.atleast_2d(np.sin(np.linspace(1, 255, 255) * 50))
  42. ax.imshow(data, aspect="auto", cmap='ocean')
  43. def on_submit(self):
  44. text = self.textbox.text()
  45. print(text)
  46. pass
  47. if __name__ == "__main__":
  48. qapp = QtWidgets.QApplication(sys.argv)
  49. app = Textbox_Demo()
  50. app.show()
  51. qapp.exec_()

matplotlib.widgets.TextBox 在包含多个子图的图中交互速度较慢。

英文:

Matplotlib's TextBox is inherently slow, because it uses the drawing tools provided by matplotlib itself and hence redraws the complete figure upon changes.

I would propose to use a text box of a GUI kit instead. For example for PyQt this might look like:

matplotlib.widgets.TextBox 在包含多个子图的图中交互速度较慢。

  1. import numpy as np
  2. import sys
  3. from matplotlib.backends.backend_qt5agg import (
  4. FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
  5. from matplotlib.backends.qt_compat import QtCore, QtWidgets
  6. import matplotlib.gridspec as gridspec
  7. from matplotlib.figure import Figure
  8. class Textbox_Demo(QtWidgets.QMainWindow):
  9. def __init__(self):
  10. super().__init__()
  11. self._main = QtWidgets.QWidget()
  12. self.setCentralWidget(self._main)
  13. layout = QtWidgets.QVBoxLayout(self._main)
  14. layout.setContentsMargins(0,0,0,0)
  15. layout.setSpacing(0)
  16. self.fig = Figure(figsize=(8,8))
  17. self.canvas = FigureCanvas(self.fig)
  18. layout.addWidget(self.canvas)
  19. self.addToolBar(NavigationToolbar(self.canvas, self))
  20. self._textwidget = QtWidgets.QWidget()
  21. textlayout = QtWidgets.QHBoxLayout(self._textwidget)
  22. self.textbox = QtWidgets.QLineEdit(self)
  23. self.textbox.editingFinished.connect(self.on_submit)
  24. # or, if wanting to have changed apply directly:
  25. # self.textbox.textEdited.connect(self.on_submit)
  26. textlayout.addWidget(QtWidgets.QLabel(&quot;Enter Text: &quot;))
  27. textlayout.addWidget(self.textbox)
  28. layout.addWidget(self._textwidget)
  29. self.fill_figure()
  30. def fill_figure(self):
  31. self.string = &#39;label&#39;
  32. self.rows = 5 # reducing rows speeds up textbox interaction
  33. self.cols = 5 # reducing cols speeds up textbox interaction
  34. self.plot_count = self.rows * self.cols
  35. self.gs = gridspec.GridSpec(self.rows, self.cols,
  36. left=0.05, right=1-0.02, top=1-.02, bottom=0.10, wspace=0.3, hspace=0.4)
  37. for k in range(self.plot_count):
  38. ax = self.fig.add_subplot(self.gs[k])
  39. #ax.set_xticks([]) # showing axes slows textbox interaction
  40. #ax.set_yticks([]) # showing axes slows textbox interaction
  41. data = np.atleast_2d(np.sin(np.linspace(1,255,255) * 50))
  42. ax.imshow(data, aspect=&quot;auto&quot;, cmap=&#39;ocean&#39;)
  43. def on_submit(self):
  44. text = self.textbox.text()
  45. print(text)
  46. pass
  47. if __name__ == &quot;__main__&quot;:
  48. qapp = QtWidgets.QApplication(sys.argv)
  49. app = Textbox_Demo()
  50. app.show()
  51. qapp.exec_()

huangapple
  • 本文由 发表于 2020年1月6日 18:27:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610425.html
匿名

发表评论

匿名网友

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

确定