简体   繁体   English

嵌入matplotlib图形时如何更新轴限制?

[英]How to update axes limits when embedding matplotlib figure?

I have embedded a matplotlib figure in a PySide app. 我已经在PySide应用程序中嵌入了matplotlib图形。 I'm trying to expose some of the functionality to the user (via gui buttons etc) to help them customise the figure. 我正在尝试向用户展示一些功能(通过gui按钮等)以帮助他们自定义图形。

I'm having trouble to get the x and y axis limits to update when told to. 我被告知无法更新x和y轴限制。 I have this function: 我有这个功能:

def set_xlimits(self, lower, upper):
    """ Convenience method to canvas.axes.set_xlim """
    self.canvas.axes.set_xlim(lower, upper)
    self.canvas.draw()

where axes is a matplotlib.axes.Axes instance and canvas is inherited from FigureCanvasQTAgg 其中axesmatplotlib.axes.Axes实例和canvas从继承FigureCanvasQTAgg

When I call this method, with new limits, either nothing happens or new ticks are added to the axis, but no new tick labels (ie if changing from limits of 0,1 to 0,10, it will still remain labelled from 0-1, but with some extra ticks beyond 1) 当我以新的限制调用此方法时,什么也没有发生或没有向轴添加新的刻度线,但是没有新的刻度线标签(即,如果从0,1的限制更改为0,10,它将仍然标记为0- 1,但除1之外还有一些额外的滴答声)

Any ideas on how to always enforce the change? 关于如何始终执行更改的任何想法?

Edit: It seems that the axes limits are updated, but the ticks are not. 编辑:似乎轴限制已更新,但刻度没有更新。 So, if I change the limits to 0-16, the ticks will still stay 0-1, but all data in the range 0-16 is displaedy ?! 因此,如果将限制更改为0-16,则滴答声仍将保持0-1,但是0-16范围内的所有数据都消失了!

When I subsequently call another method, such as this one to edit the tick label font: 当我随后调用另一种方法(例如此方法)来编辑刻度标签字体时:

def set_tick_font(self, font):
    self.canvas.axes.set_xticklabels(self.canvas.axes.get_xticks(), **font)
    self.canvas.axes.set_yticklabels(self.canvas.axes.get_yticks(), **font)
    self.canvas.draw()

The previous call to update the axis limits is finally drawn. 最终调用了更新轴极限的上一个调用。 This isnt ideal - it should have been drawn the first time. 这不是理想的-应该是第一次绘制。 Any ideas what is going on? 有什么想法吗?

As tcaswell hinted at, it seems my second method - to set the tick font, was somehow preventing the set_xlim and set_ylim methods from working correctly. 正如tcaswell所暗示的那样,似乎我的第二种方法-设置刻度字体,以某种方式阻止了set_xlim和set_ylim方法的正常工作。

I still need to look at exactly how it works, but for now, changing the second method to: 我仍然需要看一下它是如何工作的,但是现在,将第二种方法更改为:

def set_tick_font(self, font):
    fnt = font_manager.FontProperties(**font)
    for labelx, labely in zip(self.canvas.axes.get_xticklabels(), self.canvas.axes.get_yticklabels()):
        labelx.set_fontproperties(fnt)
        labely.set_fontproperties(fnt)
    self.canvas.draw()

means that the limits will now update correctly when calling the set_xlimits method and I can still independently change the tick label font. 意味着现在可以在调用set_xlimits方法时正确更新限制,并且我仍然可以独立更改刻度标签字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM