用于暴露matplotlib图形的渲染器
项目描述
关于tgext.matplotrender
tgext.matplotrender是TurboGears2的一个扩展,它提供了一个Matplotlib图形的渲染引擎。
安装
tgext.matplotrender可以从pypi安装
pip install tgext.matplotrender
应该对大多数用户来说都能正常工作。
启用
要启用tgext.matplotrender,请将matplotfig渲染器添加到您的配置中
base_config.renderers.append('matplotfig')
并在您的config/app_cfg.py中连接渲染引擎
import tgext.matplotrender
tgext.matplotrender.plugme(base_config)
使用方法
使用tgext.matplotrender就像暴露一个带有matplotfig模板的操作,并在操作返回字典的fig键中返回图形本身一样简单。
给定以下图形
def _make_fig():
fig = matplotlib.figure.Figure(figsize=(9, 6))
fig.Name = "Sinewave"
ax = fig.add_subplot(111)
ax.set_xlabel("angle")
ax.set_ylabel("amplitude")
t = numpy.arange(0.0, 2.0, 0.01)
s1 = numpy.sin(2 * numpy.pi * t)
ax.plot(t, s1, color="k")
return fig
可以通过TurboGears操作进行暴露,如下所示
class RootController(TGController):
@expose('matplotfig')
def figure(self, *args, **kwargs):
return dict(fig=_make_fig())
@expose('matplotfig', render_params=dict(dpi=36))
def lowres(self, *args, **kwargs):
return dict(fig=_make_fig())
@expose('matplotfig')
def customres(self, *args, **kwargs):
options = {}
try:
options['dpi'] = int(kwargs['dpi'])
except:
pass
return dict(fig=_make_fig(), **options)
字典中提供的任何其他值都将作为matplotlib的print_figure参数使用