繁体   English   中英

matplotlib中的RC变量不适用于Web应用程序中的图形,轴

[英]RC variables in matplotlib doesn't apply to graphs, axes in web app

我正在尝试使用生成图形的Web应用程序中设置matplotlib的线宽

matplotlib.rc('lines', linewidth=0.5)

当在交互模式下使用matplotlib时,此方法可以很好地工作,但是在我的Web应用程序中它没有任何作用,而获得正确行宽的唯一方法是在各个调用中提供参数,即:

vals = map(itemgetter(1), sorted(series1.items(), reverse=True))
group1_rects = ax.barh(ind, vals, width, color='r', snap=True, linewidth=0.5)
vals = map(itemgetter(1), sorted(series2.items(), reverse=True))
group2_rects = ax.barh(ind+width, vals, width, color='b', linewidth=0.5)

是否有一些技巧可以使matplotlib.rc调用在Web应用程序中工作?

我用来绘制图形的代码如下所示:

@contextmanager
def render_plot(w=8,h=8):
    fig = Figure(figsize=(w,h))           
    canvas = FigureCanvas(fig)
    response.content_type = 'image/png'
    #Here is where I hope to put RC settings
    matplotlib.rc('lines', linewidth=0.5)
    yield fig
    s = StringIO()
    canvas.print_figure(s)
    response.content = s.getvalue()

您发布的内容应该可以使用。 只是作为参考,以下内容对于使用python 2.6和matplotlib 1.0的我来说非常合适。

from contextlib import contextmanager

import matplotlib as mpl
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

@contextmanager
def render_plot(w=8,h=8):
    fig = Figure(figsize=(w,h))           
    canvas = FigureCanvas(fig)
    mpl.rc('lines', linewidth=5)
    yield fig
    s = file('temp.png', 'w')
    canvas.print_figure(s)

with render_plot() as fig:
    ax = fig.add_subplot(111)
    ax.plot(range(10))

替代文字

此示例在您的系统上工作吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM