我正在编写一个使用Matplotlib进行绘图的PyGTK / Twisted应用程序。使用FigureCanvasGtkAgg将图块嵌入到我的小部件中很容易,但是我注意到画布的背景颜色(在绘图区域本身之外)与我的应用程序的其余部分不匹配,并且字体也没有(对于标签) ,传说等)。
是否有一种简单的方法可以让我的图表尊重用户选择的GTK主题?
答案 0 :(得分:3)
您可以设置,例如pylab.figure(facecolor=SOME_COLOR, ...)
或matplotlib.rcParams['figure.facecolor'] = SOME_COLOR
。看起来它的默认值是hard-coded,所以没有办法告诉MPL尊重GTK主题。
以下是如何在PyGTK中执行此操作的具体示例。这里的部分信息来自"Get colors of current gtk style"和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_panel
是gtk.Alignment
,graph
是FigureCanvasGTKAgg
的子类,根据需要有figure
成员。)