Viewing figure is not possible.
7 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Peter O
on 25 Sep 2020
matplotlib takes an explicit "show" command. Try:
py.matplotlib.pyplot.show()
3 Comments
Peter O
on 17 Oct 2021
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.
- Copy those same directories, only now the base install is the Anaconda directory, somewhere like C:\Users\USERNAME\anaconda3
- Launch Matlab from an Anaconda command or PowerShell prompt (type "matlab")
- Override the qt renderer by to TK: py.matplotlib.use('TkAgg')
- py.matplotlib.pyplot.plot([1,2,3,4],[1,2,3,4])
- py.matplotlib.pyplot.show()
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!