我希望能夠在互動式 matplotlib 圖中重新繪制資料,并保持現有的縮放比例。網上說要使用set_data。這有效。
不起作用的是能夠單擊主頁按鈕查看我剛剛重新繪制的新資料的整個范圍。
如何強制主頁按鈕向我顯示當前繪制的所有資料?這一切都發生在 tKinter 視窗內。
一些示例代碼:
import matplotlib.pyplot as plt
x1 = [1, 2, 3, 4, 5, 6]
y1 = [3, 4, 5, 6, 7, 8]
x2 = [2, 3, 4, 5, 6, 7]
y2 = [8, 7, 6, 5, 4, 3]
# plot data
line, = plt.plot(x1, y1, marker="o")
# zoom in - this actually happens by user interaction with the zoom tool
plt.xlim(2.5, 5.5)
plt.ylim(3, 6)
# replace data, maintaining zoom. This actually happens by the user choosing new data to plot
line.set_data(x2, y2)
plt.show()
# now, I want to unzoom to show all of x2,y2 by pressing the "home" button
pass
uj5u.com熱心網友回復:
“主頁”按鈕將帶您回傳已注冊為默認值的縮放。您應該以編程方式設定它。
我不知道設定此默認值的直接介面,但是您可以使用 ToolBar 的push_current方法將 Canvas 的當前狀態注冊為默認值。
在您的情況下,您應該在更改資料的地方進行此操作。而不是簡單地改變資料
line.set_data(x2, y2)
你應該做一些簿記:
fig = plt.gcf() # I assume the OP already has this data
ax = fig.gca() # I assume the OP already has this data
# save the current zoom for restoring later
old_x_lim = ax.get_xlim()
old_y_lim = ax.get_ylim()
# let matplotlib calculate the optimal zoom for the new data
ax.relim()
ax.autoscale()
# Now the tricky part... I did not find much documentation on this
toolbar = fig.canvas.manager.toolbar
toolbar.update() # Clear the axes stack
toolbar.push_current() # save the current status as home
ax.set_xlim(old_x_lim) # and restore zoom
ax.set_ylim(old_y_lim)
我希望這適用于您的設定。否則,您需要深入了解 matplotlib 的內部結構。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/380623.html
標籤:Python matplotlib
