开发者

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

开发者 https://www.devze.com 2023-02-05 10:59 出处:网络
I\'m trying to set line widths for matplotlib in a web application that generates graphs using 开发者_运维问答matplotlib.rc(\'lines\', linewidth=0.5)

I'm trying to set line widths for matplotlib in a web application that generates graphs using

开发者_运维问答matplotlib.rc('lines', linewidth=0.5)

This works fine when working with matplotlib in interactive mode, but in my web application it has no effect, and the only way I can get the correct line widths is by supplying the argument on the individual calls, i.e.:

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)

Is there some trick involved to make the matplotlib.rc call work in web apps?

The code I'm using for getting a figure to draw on looks like this:

@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()


What you've posted should work. Just as a reference, the following works perfectly for me using python 2.6 and 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))

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

Does this example work on your system?

0

精彩评论

暂无评论...
验证码 换一张
取 消