Run a Python code in MATLAB 2022b

9 次查看(过去 30 天)
I want ot run a Python code given here : Code Library Link in MATLAB 2022b.
I have installed Pyton 3.8 and installed this library also. I wrote this code
py = py.importlib.import_module('upsetplot');
plot_handel = py.UpSet;
generate_counts=py.generate_samples;
example_counts = generate_counts()
ax_dict = plot_handel(example_counts).plot()
I am getting this error.
example_counts =
sys:1: FutureWarning: is_monotonic is deprecated and will be removed in a future version. Use is_monotonic_increasing instead.
Python Series with properties:
T: [1×1 py.pandas.core.series.Series]
array: [1×1 py.pandas.core.arrays.numpy_.PandasArray]
at: [1×1 py.pandas.core.indexing._AtIndexer]
attrs: [1×1 py.dict]
axes: [1×1 py.list]
dtype: [1×1 py.numpy.dtype[int64]]
dtypes: [1×1 py.numpy.dtype[int64]]
empty: 0
flags: [1×1 py.pandas.core.flags.Flags]
hasnans: 0
iat: [1×1 py.pandas.core.indexing._iAtIndexer]
iloc: [1×1 py.pandas.core.indexing._iLocIndexer]
index: [1×1 py.pandas.core.indexes.multi.MultiIndex]
is_monotonic: 0
is_monotonic_decreasing: 0
is_monotonic_increasing: 0
is_unique: 1
loc: [1×1 py.pandas.core.indexing._LocIndexer]
name: [1×5 py.str]
nbytes: [1×1 py.int]
ndim: [1×1 py.int]
shape: [1×1 py.tuple]
size: [1×1 py.int]
values: [1×1 py.numpy.ndarray]
cat0 cat1 cat2
False False False 56
True 283
True False 1279
True 5882
True False False 24
True 90
True False 429
True 1957
Name: value, dtype: int64
Error using __init__
Python Error: IndexError: list index out of range
Error in _backend_tk>create_with_canvas (line 457)
Error in backend_bases>new_manager (line 1703)
Error in backend_bases>new_figure_manager_given_figure (line 3518)
Error in backend_bases>new_figure_manager (line 3513)
Error in pyplot>new_figure_manager (line 359)
Error in pyplot>figure (line 783)
Error in deprecation>wrapper (line 454)
Error in plotting>plot (line 842)
I tested the code given on the link in Python and it worked. However its not woring in MATLAB. How can I resolve it?
The website says that output example_counts is a pandas.Series with 3-level Multi-index. Does MATALB is changing this thats why its not able to plot?
Thank You.

回答(1 个)

Rushikesh
Rushikesh 2024-9-19
The "IndexError" you are facing while using the 'plot' function of the "upsetplot.UpSet" Python library is likely related to the Tcl/Tk library, which is often used by the 'matplotlib' library for rendering plots when using the TkAgg backend. This issue typically arises when the Python environment is unable to locate the Tcl/Tk installation required for the TkAgg backend.
To resolve this, try specifying the TCL_LIBRARY and TK_LIBRARY environment variables to point to the correct directories in the MATLAB Command Window.
For example, you can use the code given below:
setenv('TCL_LIBRARY', 'C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\tcl\tcl8.6');
setenv('TK_LIBRARY', 'C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\tcl\tk8.6');
Using MATLAB 2022b and Python 3.8, I attempted to replicate the error you encountered. By setting the specified environment variables, I successfully resolved the issue and generated the desired plot. Please find the attached screenshot of the output for your reference.

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by