def _initialize_instance(cls, obj, profiles, labels, plot_specs, y_log):
from matplotlib.font_manager import FontProperties
obj._font_properties = FontProperties(family='stixgeneral', size=18)
obj._font_color = None
obj.profiles = ensure_list(profiles)
obj.x_log = None
obj.y_log = {}
if y_log is not None:
for field, log in y_log.items():
field, = obj.profiles[0].data_source._determine_fields([field])
obj.y_log[field] = log
obj.y_title = {}
obj.label = sanitize_label(labels, len(obj.profiles))
if plot_specs is None:
plot_specs = [dict() for p in obj.profiles]
obj.plot_spec = plot_specs
obj.plots = PlotContainerDict()
obj.figures = FigureContainer(obj.plots)
obj.axes = AxesContainer(obj.plots)
obj._setup_plots()
return obj
python类font_manager()的实例源码
def __init__(self, data_source, figure_size, fontsize):
from matplotlib.font_manager import FontProperties
self.data_source = data_source
self.ds = data_source.ds
self.ts = self._initialize_dataset(self.ds)
if iterable(figure_size):
self.figure_size = float(figure_size[0]), float(figure_size[1])
else:
self.figure_size = float(figure_size)
font_path = matplotlib.get_data_path() + '/fonts/ttf/STIXGeneral.ttf'
self._font_properties = FontProperties(size=fontsize, fname=font_path)
self._font_color = None
self._xlabel = None
self._ylabel = None
self._minorticks = {}
self._field_transform = {}
def plot_init(self, name, fig_specs=None, save=False):
#add a figure with a single subplot
if save==True:
import matplotlib
matplotlib.use('Agg') # non-interactive backend
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fontP = FontProperties()
fontP.set_size('small')
self.colors = ['-r','-b','-m','-g']
if self.fig==None:
self.fig = plt.figure(figsize=(8,4))
if fig_specs != None:
# fig_spec is a dictionary of fig adjust specs
self.fig.subplots_adjust(**fig_sepcs)
else:
self.fig.subplots_adjust(left = 0.15, bottom = 0.07,
right = 0.94, top = 0.94,
hspace = 0.14, wspace=0.20)
if type(name) != str:
raise TypeError('name should be a string')
layout = 'grid'
n = len(self.fig.axes)
if layout in ('h', 'horizontal'):
n_rows, n_cols = (1, n+1)
elif layout in ('v', 'vertical'):
n_rows, n_cols = (n+1, 1)
elif layout in ('g', 'grid'):
vector_length=n+1
n_cols = int(np.ceil(np.sqrt(vector_length)))
n_rows = int(np.ceil(vector_length/n_cols))
else:
raise ValueError('Bad layout Value')
for i in range(n):
self.fig.axes[i].change_geometry(n_rows, n_cols, i+1)
self.figsaxe[name] = self.fig.add_subplot(n_rows, n_cols, n+1)
def set_font(self, font_dict=None):
"""
Set the font and font properties.
Parameters
----------
font_dict : dict
A dict of keyword parameters to be passed to
:class:`matplotlib.font_manager.FontProperties`.
Possible keys include:
* family - The font family. Can be serif, sans-serif, cursive,
'fantasy', or 'monospace'.
* style - The font style. Either normal, italic or oblique.
* color - A valid color string like 'r', 'g', 'red', 'cobalt',
and 'orange'.
* variant - Either normal or small-caps.
* size - Either a relative value of xx-small, x-small, small,
medium, large, x-large, xx-large or an absolute font size, e.g. 12
* stretch - A numeric value in the range 0-1000 or one of
ultra-condensed, extra-condensed, condensed, semi-condensed,
normal, semi-expanded, expanded, extra-expanded or ultra-expanded
* weight - A numeric value in the range 0-1000 or one of ultralight,
light, normal, regular, book, medium, roman, semibold, demibold,
demi, bold, heavy, extra bold, or black
See the matplotlib font manager API documentation for more details.
http://matplotlib.org/api/font_manager_api.html
Notes
-----
Mathtext axis labels will only obey the `size` and `color` keyword.
Examples
--------
This sets the font to be 24-pt, blue, sans-serif, italic, and
bold-face.
>>> prof = ProfilePlot(ds.all_data(), 'density', 'temperature')
>>> slc.set_font({'family':'sans-serif', 'style':'italic',
... 'weight':'bold', 'size':24, 'color':'blue'})
"""
from matplotlib.font_manager import FontProperties
if font_dict is None:
font_dict = {}
if 'color' in font_dict:
self._font_color = font_dict.pop('color')
# Set default values if the user does not explicitly set them.
# this prevents reverting to the matplotlib defaults.
font_dict.setdefault('family', 'stixgeneral')
font_dict.setdefault('size', 18)
self._font_properties = \
FontProperties(**font_dict)
return self
def set_font(self, font_dict=None):
"""
Set the font and font properties.
Parameters
----------
font_dict : dict
A dict of keyword parameters to be passed to
:class:`matplotlib.font_manager.FontProperties`.
Possible keys include:
* family - The font family. Can be serif, sans-serif, cursive,
'fantasy' or 'monospace'.
* style - The font style. Either normal, italic or oblique.
* color - A valid color string like 'r', 'g', 'red', 'cobalt',
and 'orange'.
* variant - Either normal or small-caps.
* size - Either a relative value of xx-small, x-small, small,
medium, large, x-large, xx-large or an absolute font size, e.g. 12
* stretch - A numeric value in the range 0-1000 or one of
ultra-condensed, extra-condensed, condensed, semi-condensed,
normal, semi-expanded, expanded, extra-expanded or ultra-expanded
* weight - A numeric value in the range 0-1000 or one of ultralight,
light, normal, regular, book, medium, roman, semibold, demibold,
demi, bold, heavy, extra bold, or black
See the matplotlib font manager API documentation for more details.
http://matplotlib.org/api/font_manager_api.html
Notes
-----
Mathtext axis labels will only obey the `size` and `color` keyword.
Examples
--------
This sets the font to be 24-pt, blue, sans-serif, italic, and
bold-face.
>>> slc = SlicePlot(ds, 'x', 'Density')
>>> slc.set_font({'family':'sans-serif', 'style':'italic',
... 'weight':'bold', 'size':24, 'color':'blue'})
"""
from matplotlib.font_manager import FontProperties
if font_dict is None:
font_dict = {}
if 'color' in font_dict:
self._font_color = font_dict.pop('color')
# Set default values if the user does not explicitly set them.
# this prevents reverting to the matplotlib defaults.
font_dict.setdefault('family', 'stixgeneral')
font_dict.setdefault('size', 18)
self._font_properties = \
FontProperties(**font_dict)
return self