Viewing figure is not possible.

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

I've managed to crash my Windows copy of MATLAB running this command, so I can't verify the solution, but matplotlib takes an explicit "show" command. Try:
py.matplotlib.pyplot.show()
Ah You’re absolutely right! Can you post it as an answer?

Sign in to comment.

Answers (1)

matplotlib takes an explicit "show" command. Try:
py.matplotlib.pyplot.show()

3 Comments

Unfortunately I get:
>> py.matplotlib.pyplot.plot([1,2,3,4])
ans =
Python list with no properties.
[<matplotlib.lines.Line2D object at 0x1ac33ea90>]
>> py.matplotlib.pyplot.show()
std::exception
Same here. Any Ideas?
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()

Sign in to comment.

Products

Release

R2018b

Tags

Asked:

on 25 Sep 2020

Commented:

on 17 Oct 2021

Community Treasure Hunt

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

Start Hunting!