0

I am working on a big dataframe where each row includes the values of various signals and all the rows should visualize based on their signals. Controlling this process on the single core takes a huge amount of time. Therefore I wanted to split the dataset into multiple cores for accelerating the plotting process.

I have a class that is inherited from FigureCanvasBase and includes a matplotlib figure. Objects from this class are generated from multiple different processes and then added to the layout for shown in pyqt5-based GUI. It was working when I inherited it from FigureCanvas but due to using multiprocess, I can use FigureCanvasBase but not FigureCanvas anymore.

class Canvas(FigureCanvasBase):
    def __init__(self, x, y):
        fig = Figure(figsize=(5, 3))
        super().__init__(fig)
        self.figure = fig

        ax = self.figure.subplots()
        for key in y.keys():
            ax.plot(x, [abs(number) for number in y[key]])

        self.figure.tight_layout()

def generate_class_func(list_of_dfs, x, y):     
    list_of_custom_classes = list()
        for df in list_of_dfs:
        canvas = Canvas(df, x, y)
        list_of_custom_classes.append(canvas)
    return list_of_custom_classes

import multiprocess as mp
with mp.Pool() as p:
    from itertools import repeat
    list_of_classes_list = p.starmap(generate_class_func, [[0,{a:1}],[2,{b:3}],[4,{c:5}],[6,{d:7}]])
    p.close()
    p.join()

canvas = Canvas([1,2], [a:3,b:4])
layout = QHBoxLayout()
layout.addWidget(canvas)

>>> {TypeError}addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = 0): argument 1 has unexpected type 'Canvas'

Any recommendations for adding Canvas objects to layouts?

9
  • FigureCanvasBase is the base class that each backend then implements in their own FigureCanvas subclass. You cannot obviously use it directly. Your relation between having to use the base class and multiprocessing is completely obscure and unclear. Please explain why you made that assumption. Commented Aug 5, 2022 at 10:29
  • @musicamante thank you for your comment. Now I updated the code and question based on your suggestions. Please let me know if you need more info. Commented Aug 5, 2022 at 11:56
  • If I'm understanding this correctly, you're trying to use multiprocessing to speed up the canvas creation. Unfortunately, this won't work, because canvasses are UI objects and as such cannot be pickled (which is a requirement for multiprocessing), which is probably the reason for which you couldn't use the standard FigureCanvas class. Commented Aug 5, 2022 at 16:40
  • @musicamante I see, thanks for your comment. Last but not least, is there any possibility to convert figurecanvasbase to figurecanvas? Maybe I could create a list of figurecanvasbases and I can convert them to figurecanvases on the same process with GUI. Commented Aug 8, 2022 at 9:42
  • 1
    @musicamante As you said it looks like artists of figure can be sharable to subprocesses but figures must be on the same process with GUI and I don't think this method will accelerate the process since there will be a bottleneck. Seems this software design will not work. Thanks for your effort and answers so far! Commented Aug 10, 2022 at 14:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.