让 Matplotlib 的 GTK Agg 后端尊重用户主题
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过例如
pylab 进行设置.figure(facecolor=SOME_COLOR, ...)
或matplotlib.rcParams['figure.facecolor'] = SOME_COLOR
。看起来它的默认值是 硬编码< /a>,所以没有办法告诉 MPL 尊重 GTK 主题。下面是如何在 PyGTK 中执行此操作的具体示例。这里的一些信息是从“获取当前gtk样式的颜色”中收集的以及来自 gdk.Color 文档。我还没有了解设置字体等,但这显示了您需要的基本框架。
首先,定义以下函数:
然后您可以连接到
realize
信号(也可能是map-event
信号,我没有尝试)并重新为图形着色创建包含的小部件时:(这里,
graph_panel
是gtk.Alignment
,graph
是FigureCanvasGTKAgg
的子类> 有一个根据需要使用figure
成员。)You can set it by, for example
pylab.figure(facecolor=SOME_COLOR, ...)
ormatplotlib.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:
You can then connect to the
realize
signal (maybe also themap-event
signal, I didn't try) and re-colour the graph when the containing widget is created:(Here,
graph_panel
is agtk.Alignment
andgraph
is a subclass ofFigureCanvasGTKAgg
that has afigure
member as needed.)