Empty figure window when plotted from Python

3 Ansichten (letzte 30 Tage)
SHC
SHC am 22 Apr. 2024
Beantwortet: Angelo Yeo am 13 Apr. 2025
Hi,
I tried to plot a MATLAB figure using a MATLAB funciton from Python script.
I used the following MATLAB funtion (plot_test.m) to draw a simple line graph:
function h = plot_test()
clear; close all;
h = plot(1:10, 2:11, 'o:')
grid on;
end
I called the MATLAB funciton from Python (plot_test.py):
import matlab.engine
eng = matlab.engine.start_matlab()
h = eng.plot_test()
input("Press Enter to quit...")
eng.quit()
The MATLAB figure window was opened. But it was empty.
Could you help me?
The system environments are as follows:
  • MATLAB: R2023b
  • Python: 3.11.8
  • OS: Fedora Linux 6.8.6-100.fc38.x86_64
  • Shell: bash 5.2.26
  • Resolution: 1680x1050
  • Gnome:44.1
I suspect that the resolution might have some impact on this result because when I run the same code in another Fedora machine with 1920x1080 monitor, it shows the graph.
  1 Kommentar
vidyesh
vidyesh am 22 Apr. 2024
Can you share MATLAB version and python version as well.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Debadipto
Debadipto am 25 Apr. 2024
Hi @SHC,
To ensure that the MATLAB figure is correctly displayed when called from a Python script, you can make modifications to the MATLAB function to explicitly set the figure's visibility and to force immediate rendering of the plot. Here's how you can adjust your "plot_test.m" MATLAB function:
function h = plot_test()
clear; close all;
figure('Visible', 'on'); % Explicitly set the figure to be visible
h = plot(1:10, 2:11, 'o:');
grid on;
drawnow; % Force MATLAB to draw the figure's content immediately
pause(0.1); % Introduce a brief pause to ensure the content is fully rendered
end
Hope it helps!

Angelo Yeo
Angelo Yeo am 13 Apr. 2025
MATLAB has a system requirement for Linux. If the requirements do not meet your system, it might work unexpectedly.

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by