-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
RcParams instances for matplotlib.style.use #3795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There is already a rcparam context manager |
The intended use-cases are a bit different though.
On the other hand,
To get the same functionality with |
I don't really understand, import matplotlib
matplotlib.rcParams['font.size'] = 314
print matplotlib.rcParams['text.usetex'], matplotlib.rcParams['font.size']
with matplotlib.rc_context(rc={'text.usetex': True}):
print matplotlib.rcParams['text.usetex'], matplotlib.rcParams['font.size']
with matplotlib.rc_context(rc={'text.usetex': False}):
print matplotlib.rcParams['text.usetex'], matplotlib.rcParams['font.size']
with matplotlib.rc_context(rc={'font.size': 12}):
print matplotlib.rcParams['text.usetex'], matplotlib.rcParams['font.size']
print matplotlib.rcParams['text.usetex'], matplotlib.rcParams['font.size']
print matplotlib.rcParams['text.usetex'], matplotlib.rcParams['font.size']
print matplotlib.rcParams['text.usetex'], matplotlib.rcParams['font.size'] The use case in your second post does make this change more compelling. attn @tonysyu |
@@ -45,16 +45,19 @@ def use(name): | |||
|
|||
Parameters | |||
---------- | |||
name : str or list of str | |||
name : str, list of str, or RcParams |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should accept a dict
objects
@tacaswell Yes, that's all I was saying: |
4cddc55
to
9cdc0c6
Compare
I ended up trying:
CPython seems to require a keys attribute in order for update() to work at all. From your suggestion, another alternative could be:
Let me know what you think is better, or if you have a better idea. |
Unit test failures seem unrelated now: |
name = [name] | ||
|
||
for style in name: | ||
if not cbook.is_string_like(style): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems very odd to test not is_string_like
in a is_string_like
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The not is_string_like
was after (not within) a previous call to is_string_like
. The only reason I chose the negated form for the second call was to avoid having to indent everything an extra level. We could also just do the extra indentation:
if cbook.is_string_like(style):
if style in library:
mpl.rcParams.update(library[style])
else:
try:
rc = rc_params_from_file(style, use_default_template=False)
mpl.rcParams.update(rc)
except:
msg = ("'%s' not found in the style library and input is "
"not a valid URL or path. See `style.available` for "
"list of available styles.")
raise ValueError(msg % style)
else:
mpl.rcParams.update(style)
Preferred?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blah, sorry, I misread the diffs, disregard this comment.
Name of style or path/URL to a style file. For a list of available | ||
style names, see `style.available`. If given a list, each style is | ||
applied from first to last in the list. | ||
name : str, dict, list of str and/or dict |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this addition, the name of this argument is a bit misleading (actually, it was a bit misleading before, too). Maybe something like style_spec
, or something similar. (By "spec", I mean specifier.)
Also, the description is a bit difficult to read because of all the different options. Maybe something like:
style_spec : str, dict, or list
str: The name of a style or a path/URL to a style file. For a list of
available style names, see `style.available`.
dict: Dictionary with valid key/value pairs in `matplotlib.rcParams`.
list: List of style specifiers (str or dict) applied from first to
last in the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go for "style" and see what you guys think.
Anything else needed for this? |
Some what out side of the scope of this PR, but this needs to have the bit of logic added to the |
style names, see `style.available`. If given a list, each style is | ||
applied from first to last in the list. | ||
style : str, dict, or list | ||
str: The name of a style or a path/URL to a style file. For a list of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this a rst table? I don't think this is going to render very will in the html docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I guess this doesn't render correctly. Personally, I think a simple restructuredtext-list would be easier to read in plain text.
I would like @tonysyu to sign off on this before it gets merged as well. |
@tacaswell Regarding errors on updating, all the code uses the |
I tried a table. In plain text it looks ok. Let me know what you think. Too bad sphinx isn't more flexible here. |
The table looks fine too. 👍 |
update does validate now, #3564 See #3752 for the issue where On Mon Nov 24 2014 at 10:21:50 PM chebee7i [email protected] wrote:
|
429e51d
to
0fe5f25
Compare
Rebased and added the logic for restore rcParam state. |
b4e6af0
to
ade114f
Compare
ENH : RcParams/dict as input to matplotlib.style.use
@chebee7i Thanks! If you are up for it, some more work done to unify the style and rcparam code. It seems a little silly to have two almost identical context managers and the rcparams code in general could use some cleaning up. I am not sure the best way to go about doing that (to maintain back compatibility and avoid circular dependencies). |
External libraries may want to prepare styles dynamically. For example,
brewer2mpl
provides various colormaps. While it could create it's own context manager for temporarily changing thecolor_cycle
, I think it makes more sense ifmatplotlib.style.use
can work directly withRcParams
instances.