如何将matplotlib Axes对象绘制到PyQt5指定Figure中?
Got it, let's work through this problem. You've got an existing matplotlib.axes._axes.Axes object from your black box function, and you want to embed it into your PyQt5 MatplotlibWidget's figure instead of popping up a standalone window. Here are two solid approaches to make this happen:
1. Reparent the Existing Axes to Your Target Figure
Since the Axes you're working with is already tied to its own figure, you can directly move it to your PyQt5 widget's figure by reparenting it. This is the quickest method if you don't need the original figure anymore:
# Grab the Axes object from your black box my_axes = blackBox() # Get the target figure from your PyQt5 MatplotlibWidget # Replace `self.matplotlib_widget` with your actual widget instance name target_fig = self.matplotlib_widget.figure # Remove the Axes from its original parent figure my_axes.remove() # Attach the Axes to your target figure target_fig.add_axes(my_axes) # Force the MatplotlibWidget to redraw and display the moved Axes self.matplotlib_widget.draw()
Quick Notes:
- This moves the Axes outright—so the original figure (from
my_axes.get_figure()) will no longer contain this plot. - The Axes will keep its original position and size. If you want it to fit your target figure's layout (like using a standard subplot grid), you can adjust its position with
my_axes.set_position([left, bottom, width, height])after adding it.
2. Clone Plot Content to a New Axes (Preserve Original Figure)
If you need to keep the original Axes/figure intact, you can duplicate all the plot content into a new Axes in your target figure. This gives you more control over layout:
# Get the original Axes from the black box my_axes = blackBox() # Access your target figure and create a new Axes (or use an existing one) target_fig = self.matplotlib_widget.figure target_axes = target_fig.add_subplot(111) # Creates a 1x1 subplot grid # Copy line plots from original to target for line in my_axes.lines: target_axes.plot(line.get_xdata(), line.get_ydata(), **line.get_properties()) # Copy other plot elements (adjust based on your actual plot type) target_axes.set_xlabel(my_axes.get_xlabel()) target_axes.set_ylabel(my_axes.get_ylabel()) target_axes.set_title(my_axes.get_title()) # Copy legend if it exists if legend := my_axes.get_legend(): target_axes.legend(handles=legend.get_lines()) # Redraw the widget to show the cloned plot self.matplotlib_widget.draw()
Quick Notes:
- This method duplicates content, so both the original figure and your PyQt5 widget will have the plot.
- Depending on your plot type (bars, scatter points, images), you'll need to loop through different collections:
- For bar plots:
my_axes.patches - For scatter plots:
my_axes.collections - For images:
my_axes.images
- For bar plots:
Which Approach Should You Use?
- Go with Method 1 for a fast, direct move when you don't need the original figure anymore.
- Go with Method 2 if you need to preserve the original plot or want to customize the layout in your PyQt5 widget.
内容的提问来源于stack exchange,提问作者Alex42

