Viewing figure is not possible.

조회 수: 8 (최근 30일)
madhan ravi
madhan ravi 2020년 9월 25일
댓글: Peter O 2021년 10월 17일
Why can't I view the figure?
P.S: The following is just to familiarise with Python in MATLAB environment.
System: MacOs.
>> py.matplotlib.pyplot.plot([1,2,3,4])
ans =
Python list with no properties.
[<matplotlib.lines.Line2D object at 0x1a9caad90>]
>>
  댓글 수: 3
madhan ravi
madhan ravi 2020년 9월 25일
Ah You’re absolutely right! Can you post it as an answer?
Peter O
Peter O 2020년 9월 25일
Sure!

댓글을 달려면 로그인하십시오.

답변 (1개)

Peter O
Peter O 2020년 9월 25일
matplotlib takes an explicit "show" command. Try:
py.matplotlib.pyplot.show()
  댓글 수: 3
Carlos Rondon
Carlos Rondon 2021년 10월 14일
Same here. Any Ideas?
Peter O
Peter O 2021년 10월 17일
I took a look with R2021aSP5 and a clean install of Python 3.8.10 and it seems to be connected to an incompatibility with TCL/TK -- specifically, the stack trace I'm getting is:
Error using __init__ (line 2270)
Python Error: TclError: Can't find a usable init.tcl in the following directories:
C:/Users/USERNAME/AppData/Local/Programs/Python/Python38/lib/tcl8.6 {C:/Program
Files/MATLAB/R2021a/interprocess/bin/win64/lib/tcl8.6} {C:/Program Files/MATLAB/R2021a/interprocess/bin/lib/tcl8.6}
{C:/Program Files/MATLAB/R2021a/interprocess/bin/win64/library} {C:/Program Files/MATLAB/R2021a/interprocess/bin/library}
{C:/Program Files/MATLAB/R2021a/interprocess/bin/tcl8.6.9/library} {C:/Program
Files/MATLAB/R2021a/interprocess/tcl8.6.9/library}
This probably means that Tcl wasn't installed properly.
Error in _backend_tk>new_figure_manager_given_figure (line 883)
Error in backend_bases>new_figure_manager (line 3545)
Error in pyplot>new_figure_manager (line 316)
Error in pyplot>figure (line 797)
Error in pyplot>gcf (line 854)
Error in pyplot>gca (line 2532)
Error in pyplot>plot (line 3019)
And part of the resason is that Python's TCL library doesn't live under LIB. Instead it can be found in C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\tcl\tcl8.6. And none of those paths in the MATLAB directories actually exist.
I'm wondering if this is due to a misconfiguration by either the MATLAB backend or Windows Python installer (or both). It seems to have trouble with both OutOfProcess and InProcess modes. The same install of Python will create charts with matplotlib just fine in the command window (after installing matplotlib).
Pulling that thread, this llittle gem seems to do the trick:
Basically, brute force it and copy the tk8.6 and tcl8.6 folders from Python38/tcl into Python38/lib.
And voila:
If you're using Anaconda (technically not a supported distribution), you need to override the default renderer from Qt back to Tkinter. There's probaby a way to get Qt to work, but that's an extra bit of wrangling I can't do right now.
  1. Copy those same directories, only now the base install is the Anaconda directory, somewhere like C:\Users\USERNAME\anaconda3
  2. Launch Matlab from an Anaconda command or PowerShell prompt (type "matlab")
  3. Override the qt renderer by to TK: py.matplotlib.use('TkAgg')
  4. py.matplotlib.pyplot.plot([1,2,3,4],[1,2,3,4])
  5. py.matplotlib.pyplot.show()

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by