Skip to content
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

matplotlib.style.available not updated upon adding/deleting .mplstyle files #3601

Closed
RubenGeert opened this issue Oct 1, 2014 · 7 comments
Closed
Milestone

Comments

@RubenGeert
Copy link

Dear all,

I've been having the problem that the available styles only seem to be updated upon rebooting my system. I discussed the issue here: http://stackoverflow.com/questions/26106552/matplotlib-style-library-not-updating-when-mplstyle-files-added-deleted.

Is this normal? What should I do?

@tacaswell tacaswell added this to the v1.4.x milestone Oct 1, 2014
@tacaswell
Copy link
Member

attn @tonysyu

@tonysyu
Copy link
Contributor

tonysyu commented Oct 1, 2014

Thanks for pinging me @tacaswell.

@RubenGeert: Does the following show a different number of styles for the last print statement?

from __future__ import print_function

import os
from contextlib import contextmanager

import matplotlib.pyplot as plt
from matplotlib.style.core import USER_LIBRARY_PATHS


@contextmanager
def temp_style_file(name):
    """ A context manager for creating an empty style file in the expected path.
    """
    filename = os.path.join(USER_LIBRARY_PATHS[0], name)
    with open(filename, 'w'):
        pass
    yield
    os.remove(filename)


print('# styles available:', len(plt.style.available))

with temp_style_file('dummy.mplstyle'):
    print('# before reload:', len(plt.style.available))

    plt.style.reload_library()
    print('# after reload:', len(plt.style.available))

Aside: I thought I added os.path.curdir to USER_LIBRARY_PATHS but apparently not. It's an easy addition, but I'm not eager to add it at the moment (mainly b/c I don't want to write a test for it :P).

@RubenGeert
Copy link
Author

Hi Tony! On running the last commands, I got:

IOError: [Errno 2] No such file or directory: u'C:\\Users\\Ruben\\.matplotlib\\stylelib\\dummy.mplstyle'

Is style.reload.library() also going to reload the stylesheets themselves? Plan A for me was to develop one by trial and error - edit sheet, reload it, run graph, and so on.

Should I replace a line in ..\style\core.py? If so, which line should I replace with what?

Sorry if those my questions are somewhat basic, I'm not exactly a Python expert.

@tonysyu
Copy link
Contributor

tonysyu commented Oct 1, 2014

The test script is complaining that you haven't created a stylelib directory (this is where matplotlib is looking for style files). Could you replace the temp_style_file function with

@contextmanager
def temp_style_file(name):
    """ A context manager for creating an empty style file in the expected path.
    """
    stylelib_path = USER_LIBRARY_PATHS[0]
    if not os.path.exists(stylelib_path):
        os.makedirs(stylelib_path)

    filename = os.path.join(stylelib_path, name)
    with open(filename, 'w'):
        pass
    yield
    os.remove(filename)

and see if that prints out the expected values? The last print statement should list one more file than the others.

That said, since that directory doesn't already exist, it suggests you weren't creating files in the correct location. Where were you putting your custom style files?

Changes to style files should be visible after calling style.reload_library(). That said, it's probably not the best way to iterate on a style. It's easier to manipulate the rc parameters directly in your script and then copy over the desired settings to a style file afterwords.

@RubenGeert
Copy link
Author

@tonysyu : yes, the modification solved the problem. After style.reload.library() the number of sheets increased from 6 to 7.

I first put the new stylesheet into ...\matplotlib\stylelib but when that didn't have any effect, I next tried \matplotlib\mpl-data\stylelib.

And, indeed, I came a long way by using mpl.rc(...).

Thanks again for the assistance!

@tonysyu
Copy link
Contributor

tonysyu commented Oct 13, 2014

I've added a response to stackoverflow just for posterity. This issue should probably be closed.

@tacaswell
Copy link
Member

I thought there was a user space folder for adding style files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants