在调整 matlotlib 大小时自动更改视图边界 [英] automatically changing view bounds on resize matlotlib
问题描述
是否可以设置绘图以使其在扩展时显示更多数据?
Is it possible to setup plot to show more data when expanded?
Matplotlib调整大小后会缩放比例.要显示特定区域,可以在轴上使用 set_xlim
等.我有一个类似 ecg 的图显示实时数据,它的 y 限制是预定义的,但是如果我扩展窗口或只有大显示器,我希望看到更多沿 x 的数据.
Matplotlib plots scale when resized. To show specific area one can use set_xlim
and such on axes. I have an ecg-like plot showing realtime data, its y limits are predefined, but I want to see more data along x if I expand window or just have big monitor.
我在 pyside 应用程序中使用它,我可以在调整大小时更改 xlim,但我想要更干净和通用的解决方案.
Im using it in a pyside app and I could just change xlim on resize but I want more clean and generic solution.
推荐答案
一种方法是实现 resize_event
的处理程序.这是一个如何完成的简短示例.您可以根据需要对其进行修改:
One way to do this is to implement a handler for resize_event
. Here is a short example how this might be done. You can modify it for your needs:
import numpy as np
import matplotlib.pyplot as plt
def onresize(event):
width = event.width
scale_factor = 100.
data_range = width/scale_factor
start, end = plt.xlim()
new_end = start+data_range
plt.xlim((start, new_end))
if __name__ == "__main__":
fig = plt.figure()
ax = fig.add_subplot(111)
t = np.arange(100)
y = np.random.rand(100)
ax.plot(t,y)
plt.xlim((0, 10))
cid = fig.canvas.mpl_connect('resize_event', onresize)
plt.show()
这篇关于在调整 matlotlib 大小时自动更改视图边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!