让 Matplotlib 的 GTK Agg 后端尊重用户主题

发布于 2024-11-03 16:25:20 字数 190 浏览 0 评论 0原文

我正在编写一个 PyGTK/Twisted 应用程序,它使用 Matplotlib 进行绘图。使用 FigureCanvasGtkAgg 将绘图嵌入到我的小部件中非常容易,但我注意到画布的背景颜色(绘图区域本身之外)与我的应用程序的其余部分不匹配,字体(用于标签)也不匹配、传说等)。

有没有一种简单的方法可以让我的图表尊重用户选择的 GTK 主题?

I'm writing a PyGTK/Twisted app that uses Matplotlib for graphing. It's easy enough to embed the plots in my widgets using the FigureCanvasGtkAgg, but I notice that the background colour of the canvas (outside the plot area itself) does not match that for the rest of my application, and neither does the font (for labels, legends, etc).

Is there a simple way to get my graphs to respect the user selected GTK theme?

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

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

发布评论

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

评论(1

み青杉依旧 2024-11-10 16:25:20

您可以通过例如 pylab 进行设置.figure(facecolor=SOME_COLOR, ...)matplotlib.rcParams['figure.facecolor'] = SOME_COLOR。看起来它的默认值是 硬编码< /a>,所以没有办法告诉 MPL 尊重 GTK 主题。


下面是如何在 PyGTK 中执行此操作的具体示例。这里的一些信息是从“获取当前gtk样式的颜色”中收集的以及来自 gdk.Color 文档。我还没有了解设置字体等,但这显示了您需要的基本框架。

首先,定义以下函数:

def set_graph_appearance(container, figure):
    """
    Given a GTK container and a Matplotlib "figure" object, this will set the
    figure background colour to be the same as the normal colour of the
    container.
    """
    # "bg" is the background "style helper" object. It contains five different
    # colours, for the five different widget states.
    bg_style = container.get_style().bg[gtk.STATE_NORMAL]
    gtk_color = (bg_style.red_float, bg_style.green_float, bg_style.blue_float)
    figure.set_facecolor(gtk_color)

然后您可以连接到 realize 信号(也可能是 map-event 信号,我没有尝试)并重新为图形着色创建包含的小部件时:(

graph_panel.connect('realize', set_graph_appearance, graph.figure)

这里,graph_panelgtk.AlignmentgraphFigureCanvasGTKAgg 的子类> 有一个根据需要使用 figure 成员。)

You can set it by, for example pylab.figure(facecolor=SOME_COLOR, ...) or matplotlib.rcParams['figure.facecolor'] = SOME_COLOR. It looks like that its default value is hard-coded, so there is no way to tell MPL to respect GTK theme.


Here's a concrete example of how to do this in PyGTK. Some of this information here was gleaned from "Get colors of current gtk style" and from the gdk.Color docs. I haven't gotten as far as setting the font, etc, but this shows the basic framework you need.

First, define the following function:

def set_graph_appearance(container, figure):
    """
    Given a GTK container and a Matplotlib "figure" object, this will set the
    figure background colour to be the same as the normal colour of the
    container.
    """
    # "bg" is the background "style helper" object. It contains five different
    # colours, for the five different widget states.
    bg_style = container.get_style().bg[gtk.STATE_NORMAL]
    gtk_color = (bg_style.red_float, bg_style.green_float, bg_style.blue_float)
    figure.set_facecolor(gtk_color)

You can then connect to the realize signal (maybe also the map-event signal, I didn't try) and re-colour the graph when the containing widget is created:

graph_panel.connect('realize', set_graph_appearance, graph.figure)

(Here, graph_panel is a gtk.Alignment and graph is a subclass of FigureCanvasGTKAgg that has a figure member as needed.)

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