我正在尝试在使用
生成图形的 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))
此示例适用于您的系统吗?
关于python - matplotlib 中的 RC 变量不适用于 Web 应用程序中的图形、轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4723271/