Question on Warning: `getindex(o::PyObject, s::AbstractString)` is deprecated in favor of dot overloading

I want to modify rcParams by

rcParams = PyDict(matplotlib["rcParams"])
rcParams["text.usetex"] = true;
rcParams["text.latex.preamble"] = raw"\usepackage{{amsmath}}";

but I get the getindex warning. How do I fix that warning? Just

rcParams = PyDict(matplotlib["rcParams"])
rcParams.text.usetex = true;
rcParams,text.latex.preamble = raw"\usepackage{{amsmath}}";

does not work…

You have a comma , instead of a dot . there.

If that doesn’t fix the issue, please post the full error output you get, including the stack trace. Otherwise it’s very hard to tell what’s wrong and therefore very hard to help you.

1 Like

Use PyDict(matplotlib."rcParams"), as the deprecation message suggests.

Also, I think you want rcParams["text.latex.preamble"] = raw"\usepackage{amsmath}" … the {{...}} double braces are an escaping technique from Python, where {...} has a special meaning in a string (unlike Julia).

i.e.

using PyCall, PyPlot
rcParams = PyDict(matplotlib."rcParams")
rcParams["text.usetex"] = true
rcParams["text.latex.preamble"] = raw"\usepackage{amsmath}"

Also, text.usetex is not an attribute, nor is it nested attributes. It is a key in the dict and the current way to set/get it is via set!/get.

1 Like

Also, are you sure this works? I think it is setting the text.usetex attribute on the dict and not the corresponding key.

Yes, I think that is what is intended here. The Matplotlib rcparams keys are strings with their own dot-structured hierarchy, and rcParams["text.usetex"] is indeed a correct key.

Ah, OK, PyDict is the wrapper that does the right thing with get/setindex already. Personally I’ve been using the update method instead before the default setindex! does the right thing…

1 Like

Sorry, a stupid question: What is the update method?

https://docs.python.org/3/library/stdtypes.html#dict.update . PyDict is fine.

That’s a Python command, right? Interesting!

Yes, and that was the point. The python side currently has a more stable API. And as I said, I didn’t realize PyDict does this in the “final API” form. It should be fine. (Though I do feel like it will also subject to deprecation since I don’t really see why it’ll still be needed after the PyObject get the same API…)