Issue
I want to get key:value
pairs of rcParams
for further manipulation.
from matplotlib import rcParams
x = rcParams
print(x)
Yeilds:
_internal.classic_mode: False
agg.path.chunksize: 0
animation.bitrate: -1
animation.codec: h264
animation.convert_args: ['-layers', 'OptimizePlus']
animation.convert_path: convert
...
I tried this approach:
for line in x:
print(line)
It only yielded the keys
but not the values
:
_internal.classic_mode
agg.path.chunksize
animation.bitrate
animation.codec
animation.convert_args
animation.convert_path
...
Lastly I tried this:
for line in x:
for key, value in line:
print(key, value)
And received the following error:
ValueError: not enough values to unpack (expected 2, got 1)
What should I try next?
Solution
rcParams is a subclass of dict, so you should already be able to perform dict methods on the object. That being said, if you need to take an dict-like object like this and turn it into a separate dictionary, you can do something like this:
from matplotlib import rcParams
params = rcParams
new_params = {}
# with a simple for loop
for param in params:
new_params[param] = params[param]
# or with the items() method
for param, value in params.items():
new_params[param] = value
Answered By - Joseph Langford
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.