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

标签 python pylons matplotlib

我正在尝试在使用

生成图形的 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 调用在网络应用程序中工作?

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

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

alt text

此示例适用于您的系统吗?

关于python - matplotlib 中的 RC 变量不适用于 Web 应用程序中的图形、轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4723271/

相关文章:

python - Pylons:不小心输入了 'setup.py install' ,我该如何修复?

python - WSGI 中间件推荐

python - matplotlib 使用什么 GUI 库?

python - 编码风格 - 将括号保持在同一行或新行上?

python - 如何将复杂字典的所有值设置为相同的值?

python - 内存数据帧的昂贵计算

python - 解析日期时 %y 指令到底是如何解释的?

python - 当我在 Windows Vista 命令提示符下运行 'import pylons' 时,为什么无法识别 Pylons?

matplotlib - 在 matplotlib 的等高线图中孵化 NaN 区域

python - 将两个二维数组组合成一个图像图