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

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/source/getting_started.rst
Expand Up @@ -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')
Expand Down
63 changes: 63 additions & 0 deletions mpltools/layout.py
Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion mpltools/style/core.py
Expand Up @@ -5,7 +5,7 @@
import matplotlib.pyplot as plt

from .. import _config

from configobj import ConfigObj

__all__ = ['use', 'available', 'lib', 'baselib']

Expand Down Expand Up @@ -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)
Expand Down