我有一个matplotlib图形嵌入一个带有几个sizer的wxpython框架中。一切正常,直到我包含一个传奇,但是sizer似乎没有使用传奇。
即使我通过在角落拖动来调整窗口大小,主图也会改变大小,但只会显示图例的边缘。
也就是说,请注意,图例在wxFrame中不可见。
import wx
import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
from random import shuffle
class PlotFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, title="Plot", size=(-1, -1))
self.main_panel = wx.Panel(self, -1)
self.plot_panel = PlotPanel(self.main_panel)
s0 = wx.BoxSizer(wx.VERTICAL)
s0.Add(self.main_panel, 1, wx.EXPAND)
self.SetSizer(s0)
self.s0 = s0
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
self.main_sizer.Add(self.plot_panel, 1, wx.EXPAND)
self.main_panel.SetSizer(self.main_sizer)
class PlotPanel(wx.Panel):
def __init__(self, parent, id = -1, dpi = None, **kwargs):
wx.Panel.__init__(self, parent, id=id, **kwargs)
self.figure = mpl.figure.Figure(dpi=dpi, figsize=(2,2))
self.canvas = Canvas(self, -1, self.figure)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas,1,wx.EXPAND)
self.SetSizer(sizer)
sizer.SetMinSize((600, 500))
self.sizer = sizer
def test(plot_panel):
axes = plot_panel.figure.gca()
for c in ['r', 'b', 'k']:
vals = [20, 30, 40, 50, 80, 20, 50, 60, 70, 70, 80]
shuffle(vals)
axes.plot(range(len(vals)), vals, "-o", color=c, label=c*10)
legend = axes.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
return legend
if __name__=="__main__":
app = wx.PySimpleApp()
frame = PlotFrame()
legend = test(frame.plot_panel)
frame.Fit()
print "legend frame pre show: ", legend.get_frame()
frame.Show(True)
print "legend frame post show:", legend.get_frame()
frame.Fit()
app.MainLoop()
编辑:
对于对我有用的解决方案,我希望在程序自动绘制图形时看起来很好,因此调整参数可以在程序中进行硬编码,或者,例如,在窗口调整大小事件上,但不是每个情节用手调整。我期望在这里改变的主要内容是: 1)标签的长度(从1到25个字符), 2)窗口大小(通常由用户拖动拐角,并 3)点和线的数量。 (另外,如果重要的话,最终,我希望在底部轴上有日期。)
我将图例放在轴外,这样它就不会覆盖任何数据点,我宁愿它留在轴的右边。
我正在使用Python 2.6.6,wxPython 2.8.12.1和matplotlib 1.1.0,现在我仍然坚持使用它们。
答案 0 :(得分:4)
它正在重新调整大小,你只是没有告诉它做你想做的事。
问题在于这一行:
axes.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
非常确定bbox_to_anchor
kwarg超越了loc
kwarg并且您正在以轴为单位将图例的左下角固定为(1.05,0.5)。如果轴展开以填充窗口,则图例的左边缘将始终为轴的右边缘右侧宽度轴的5%,因此始终不在视野范围内。
您需要将传奇放在其他地方或缩小轴(图中的分数)。
axes.legend(bbox_to_anchor=(0.5, 0.5)) #find a better place this is in the center
axes.set_position([.1, .1, .5, .8]) # units are in figure fraction
fig = figure()
axes = fig.add_subplot(111)
for c in ['r', 'b', 'k']:
vals = [20, 30, 40, 50, 80, 20, 50, 60, 70, 70, 80]
shuffle(vals)
axes.plot(range(len(vals)), vals, "-o", color=c, label=c*10)
legend = axes.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
# adjust the figure size (in inches)
fig.set_size_inches(fig.get_size_inches() * np.array([1.5, 1]), forward=True)
# and the axes size (in figure fraction)
# to (more-or-less) preserve the aspect ratio of the original axes
# and show the legend
pos = np.array(axes.get_position().bounds)
pos[2] = .66
axes.set_position(pos)
fig = figure() # use plt to set this up for demo purposes
axes = fig.add_subplot(111) # add a subplot
# control paramters
left_pad = .05
right_pad = .05
# plot data
for c in ['r', 'b', 'k']:
vals = [20, 30, 40, 50, 80, 20, 50, 60, 70, 70, 80]
shuffle(vals)
axes.plot(range(len(vals)), vals, "-o", color=c, label=c*10)
# set axes labels
axes.set_xlabel('test x')
axes.set_ylabel('test y')
# make the legend
legend = axes.legend(loc='center left', bbox_to_anchor=(1 + left_pad, 0.5))
# function to 'squeeze' the legend into place
def squeeze_legend(event):
fig.tight_layout()
right_frac = 1 - legend.get_window_extent().width / fig.get_window_extent().width - left_pad - right_pad
fig.subplots_adjust(right=right_frac)
fig.canvas.draw()
# call it so the first draw is right
squeeze_legend()
# use the resize event call-back to make sure it works even if the window is re-sized
fig.canvas.mpl_connect('resize_event', squeeze_legend)