Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

function to save current matplotlib rcParams to mpltools style file. #2

Closed
wants to merge 4 commits into from

2 participants

@arsenovic

ability save the current matplotlibrc settings, stored in the rcParams, into a style file.

the out put of this function works with ConfigObj but mpltools doesnt seem to automatically load the files, even when they are in .mplstyle for some reason i am unsure

along these lines, if the mpltools style syntax is different from matplotlib, then ability to translate between the two would be nice.

i also made a small change to intro doc which you may want to adjust. i thought explicity listing the directory to store the user style files would be useful

@arsenovic arsenovic added a function to write a dictionary of parameters to a rc file
this is useful to translate the current matplotlibrc settings,
which is stored in the rcParams, which is an objectb but
effectively a  dictionary, into a useable style file.
065e10a
@tonysyu
Owner

Thanks for the PR!

The save_param_dict function is a good idea, but, currently, it will not work as you expect it to. At the moment, ~/.mplstyle and ./mplstyle are files, where styles are stored under various headings (see README.md). This is in contrast to mpltools.style, where styles are stored in separate *.rc files. This is why mpltools isn't loading your styles.

I would like to be able to handle both a single file with multiple style sections and a directory with multiple style files, but I'm not sure how to do it well. I thought about allowing mplstyle to be either a directory or a file, but I thought this might get confusing. Maybe we could allow mplstyle and mplstyles (note the "s"): one could be a file and the other a directory. Thoughts?

@arsenovic

ohh, i see. this makes sense. i was unsure because in the example you have styles defined by headers, such as [style1], but in the actual style/ module, you have different files for each such as ieee.transactions.fullpage, ieee.transactions., etc

i would recomend a one or the other approach.

@tonysyu
Owner

I agree that it is a bit confusing. But I'm not convinced that the alternative is necessarily more attractive.

If I put all the pre-defined styles in a single file, it could get quite long (right now there aren't very many styles, but in theory, this would grow).

On the other hand, any given user will probably only have a few custom styles or a few changes to the default styles, so it makes a bit more sense to put them all in one file.

That's my thinking at the moment, but I'll have to think about this some more.

@arsenovic

yeah, i see your point.

i was thinking to automatically read the matplotlibrc (via rcParams ), into a style, allowing writing of styles in memory to disk would then be sufficient.

@tonysyu
Owner

This sounds like a good idea, but I think we'll have to figure out a way to reduce the size of these things---especially if it all goes into a single file (i.e. mplstyle) instead of separate files under some config directory (I'm still open to adding a directory in the home path where separate *.rc files could live). Even if files are separate, there would be a lot of noise from unchanged rc params.

I think it would be easy enough to remove settings that are the same as the factory matplotlib-defaults, but it may be a little more difficult to remove the user's matplotlib-defaults (factory defaults + user settings in ~/.matplotlib/matplotlibrc).

@ivanov ivanov referenced this pull request in matplotlib/matplotlib
Merged

Add rcfile function (which loads rc params from a given file). #861

arsenovic added some commits
@arsenovic arsenovic Merge branch 'master' of github.com:tonysyu/mpltools 5468aa0
@arsenovic arsenovic added a new function, `func_on_all_figs`, which loops through all
open figures and runs a command.

this is useful for formating many open plots in the same way and
the settings are not settable in  matplotlibrc.
060157b
@arsenovic

tony,
im wasnt sure where this should go, i though util.py but i see its going away, so i just put it in layout.

@arsenovic arsenovic added a couple more functions,
`save_all_figs` save all open figures in numerous formats
	with filename based on plot title
`add_markers_to_line` add markers to all lines on an existing plot.
ae6c5d3
@arsenovic

tony,
you may or may not want to include these, i find them useful, so i figured id submit them.

@tonysyu
Owner

Hey Alex, could you break up these 3 functions into 3 separate PRs on 3 separate branches?

Also, it looks like you're putting your changes in your master branch, but it's usually best to keep that branch tracking the main repo (in this case, my master branch); for example, see IPython's development workflow.

If you want to fix this, I think the best way is to rename your master branch (branch -m for "move"), then create a new master branch based off of my master. On your current master execute:

git branch -m experimental
git checkout -b master tonysyu/master

This last line creates a new branch (-b) based off my master (tonysyu/master), and switches to that branch (checkout). This assumes that you're tracking my repo as tonysyu. Depending on how you've forked this, my repo could instead be listed as origin or origin could be your repo. You can list remote repos with git remote.

After you're on a clean master, you can create clean 3 branches, copy each function onto a separate branch, and then submit each branch as a separate PR. Sorry, if I'm over explaining all of this.

As for the functions themselves:

  1. func_on_all_figs looks interesting, but I'm not sure what the use-case is. An example (preferably added to the gallery) would be helpful here. Also, I think it might be cleaner just to write a generator to do this (let me know if that's too vague).
  2. save_all_figs is definitely useful. I have some comments on the implementation, but that's better done in a PR specifically for that function.
  3. add_markers_to_lines doesn't seem like a common use-case, but I'm open to it if you can make a case for it.

Also, I don't think any of these functions naturally fit into the layout module, but I'm not quite sure where they should go. Maybe func_on_all_figs should go in utils and save_all_figs should go in a new io module.

@tonysyu tonysyu closed this
@tonysyu
Owner

Closed in favor of separate PRs like PR #4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 24, 2012
  1. @arsenovic

    added a function to write a dictionary of parameters to a rc file

    arsenovic authored
    this is useful to translate the current matplotlibrc settings,
    which is stored in the rcParams, which is an objectb but
    effectively a  dictionary, into a useable style file.
Commits on May 21, 2012
  1. @arsenovic
  2. @arsenovic

    added a new function, `func_on_all_figs`, which loops through all

    arsenovic authored
    open figures and runs a command.
    
    this is useful for formating many open plots in the same way and
    the settings are not settable in  matplotlibrc.
  3. @arsenovic

    added a couple more functions,

    arsenovic authored
    `save_all_figs` save all open figures in numerous formats
    	with filename based on plot title
    `add_markers_to_line` add markers to all lines on an existing plot.
This page is out of date. Refresh to see the latest.
View
4 doc/source/getting_started.rst
@@ -28,8 +28,8 @@ Style names should be specified as sections in "mplstyle" files. A simple
text.fontsize = 10
font.family = 'serif'
-`mpltools` searches the current working directory and your home directory for
-"mplstyle" files. To use a style, you just add::
+`mpltools` searches the current working directory and ~/.mplstyle/ directory
+for "mplstyle" files. To use a style, you just add::
>>> from mpltools import style
>>> style.use('style1')
View
63 mpltools/layout.py
@@ -166,6 +166,69 @@ def _calc_limits(axis, frac):
pad = np.array([-mag*frac, mag*frac])
return limits + pad
+def func_on_all_figs(func, *args, **kwargs):
+ """
+ runs a function after making all open figures current.
+
+ Parameters
+ ----------
+ func : function
+ function to call
+ \*args, \*\*kwargs : pased to func
+
+ Examples
+ ----------
+ >>>rf.func_on_all_figs(grid,alpha=.3)
+ """
+ for fig_n in plt.get_fignums():
+ plt.figure(fig_n)
+ func(*args, **kwargs)
+ plt.draw()
+
+def save_all_figs(dir = './', format=['eps','pdf','png']):
+ """
+ Save all open Figures to disk.
+
+ Parameters
+ ------------
+ dir : string
+ path to save figures into
+ format : list of strings
+ the types of formats to save figures as. The elements of this
+ list are passed to :matplotlib:`savefig`. This is a list so that
+ you can save each figure in multiple formats.
+ """
+ if dir[-1] != '/':
+ dir = dir + '/'
+ for fignum in plb.get_fignums():
+ fileName = plb.figure(fignum).get_axes()[0].get_title()
+ if fileName == '':
+ fileName = 'unamedPlot'
+ for fmt in format:
+ plb.savefig(dir+fileName+'.'+fmt, format=fmt)
+ print (dir+fileName+'.'+fmt)
+
+def add_markers_to_lines(ax=None,marker_list=['o','D','s','+','x'], markevery=10):
+ """
+ adds markers to all lines of a plot, post facto.
+
+ Parameters
+ -----------
+ ax : matplotlib.Axes
+ axis which to add markers to, defaults to gca()
+ marker_list : list of marker characters
+ see matplotlib.plot help for possible marker characters
+ markevery : int
+ markevery number of points with a marker.
+
+ """
+ if ax is None:
+ ax=plb.gca()
+ lines = ax.get_lines()
+ if len(lines) > len (marker_list ):
+ marker_list *= 3
+ [k[0].set_marker(k[1]) for k in zip(lines, marker_list)]
+ [line.set_markevery(markevery) for line in lines]
if __name__ == '__main__':
from yutils.mpl.core import demo_plot
View
27 mpltools/style/core.py
@@ -5,7 +5,7 @@
import matplotlib.pyplot as plt
from .. import _config
-
+from configobj import ConfigObj
__all__ = ['use', 'available', 'lib', 'baselib']
@@ -69,6 +69,31 @@ def update_user_library(base_library):
return library
+def save_param_dict(rc_param_dict, filename):
+ """Save a RcParams class or similar dictionary to a style file
+
+ Parameters
+ -------------
+ rc_param_dict : :class:`matplotlib.RcParams` or dictionary
+ rc_params information
+
+ filename : string
+ ful path and filename to write config to
+
+ Examples
+ --------
+
+ To save current parameters from a ipython -pylab session
+
+ >>> save_param_dict(rcParams, '/home/user/.mplstyle/my_param.rc')
+
+ """
+ current_config = ConfigObj(rc_param_dict)
+ config_file = open(filename, 'w')
+ current_config.write(config_file)
+ config_file.close()
+
+
baselib = load_base_library()
lib = update_user_library(baselib)
Something went wrong with that request. Please try again.