使用的系统:Windows 7
蟒蛇:2.7.12
填料函:3.14.2
使用的库:matplotlib,networkx
你好
我试图在GTK上显示嵌入matplotlib的图形。滚动窗口上的显示不起作用。然而,当我试图用工具栏保存它时,我实际上可以保存它。
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from figure1 import figure
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
class main_function():
def __init__(self):
interface= Gtk.Builder()
interface.add_from_file("interface1.glade")
interface.connect_signals(self)
# relier les signaux aux fonctions
# on stocke notre fichier Glade dans des variables
self.mainWindow=interface.get_object("mainWindow")
self.aboutchronomap=interface.get_object("aboutchronomap")
self.fichierdialogue=interface.get_object("fichierdialogue")
self.sw=interface.get_object("mainFigure")
self.sw2=interface.get_object("MatplotlibToolbar")
# on stocke les widgets avec les interfaces
canvas = FigureCanvas(figure())
print(type(canvas))
print(type(figure()))
self.sw.add_with_viewport(canvas)
print("self.sw.add_with_viewport(canvas)")
toolbar = NavigationToolbar(canvas, self.mainWindow)
self.sw2.add_with_viewport(toolbar)
print("toolbar display")
main_function()
Gtk.main()
图1.py代码:
from gi.repository import Gtk
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from numpy import sin, cos, pi, linspace
def figure():
fig = Figure(figsize=(5,5), dpi=80)
ax = fig.add_subplot(111)
n = 1000
xsin = linspace(-pi, pi, n, endpoint=True)
xcos = linspace(-pi, pi, n, endpoint=True)
ysin = sin(xsin)
ycos = cos(xcos)
sinwave = ax.plot(xsin, ysin, color='black', label='sin(x)')
coswave = ax.plot(xcos, ycos, color='black', label='cos(x)', linestyle='--')
ax.set_xlim(-pi,pi)
ax.set_ylim(-1.2,1.2)
ax.fill_between(xsin, 0, ysin, (ysin - 1) > -1, color='blue', alpha=.3)
ax.fill_between(xsin, 0, ysin, (ysin - 1) < -1, color='red', alpha=.3)
ax.fill_between(xcos, 0, ycos, (ycos - 1) > -1, color='blue', alpha=.3)
ax.fill_between(xcos, 0, ycos, (ycos - 1) < -1, color='red', alpha=.3)
ax.legend(loc='upper left')
ax = fig.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
return fig
图形通常应显示的滚动窗口的Glade xml代码:
<child>
<object class="GtkScrolledWindow" id="mainFigure">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">always</property>
<property name="vscrollbar_policy">always</property>
<property name="shadow_type">in</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
我的显示器:
如果我使用工具栏上的“保存”按钮,我实际上得到了正确的数字:
在GTK上,我看不到它。
有人能帮我吗?
非常感谢。
米娅