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

发布于 2024-10-12 06:47:23 字数 941 浏览 1 评论 0原文

我正在尝试在使用交互模式下使用 matplotlib 时生成图形的 Web 应用程序中设置 matplotlib 的线宽

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

,但在我的 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()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年 2024-10-19 06:47:23

您发布的内容应该有效。作为参考,以下内容非常适合我使用 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

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

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

alt text

Does this example work on your system?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文