python - invoke matplotlib toolbar keeping custom views history - Stack Overflow
I have a modified toolbar that has a button that zooms the x-axis by half on each click. I will show the code below. So, after click,click,click I realize I did one more zoom than desirable and would like to use the back button to view one-from-the-last. Had I done this zooming with the toolbar zoom button I would be able to do this. Since I zoomed by a different process this does not happen. So, I want my Zoomx to accumulate its history just as Matplotlib's zoom does. The code is below. Note that if the zoom-to-rectange is invoked the history is accumulated in the usual way. I ant my black-cloud button to do the same. There is a lot of literature suggesting there is a push_current procedure for doing this. Most of the suggestions fail. I am using python 3.12.4 with matplotlib 3.8.4. Depending on where this is run, the icon name and directory may need to be changed.
import matplotlib
matplotlib.rcParams["toolbar"] = "toolmanager"
import matplotlib.pyplot as plt
from matplotlib.backend_tools import ToolBase
class Zoomx(ToolBase):
def plot_axes(self,f=None):
print("f in:",f)
slimx=self.canvas.figure.axes[0].get_xlim()
slimy=self.canvas.figure.axes[0].get_ylim()
qikday=slimx[1]-(slimx[1]-slimx[0])/f
print("newlims:",([qikday,slimx[1]]))
self.canvas.figure.axes[0].set_xlim([qikday,slimx[1]])
self.canvas.draw()
return
#filedir is the location of the icon being added to toolbar
icon="Icon.light.targetsize-44"
filedir="C:\Program Files\WindowsApps\\Microsoft.GamingApp_2412.1001.22.0_x64__8wekyb3d8bbwe\\XCloudPublicAssets\\Icons\\"
handpng=filedir+icon
print(f"{handpng:}")
image = f"{handpng:}"
default_keymap = 'z' # Keyboard shortcut
description = 'zoom-xaxis Tool'
def trigger(self, *args, **kwargs):
xyz=self.plot_axes(f=2) # each button click halves x-range from the left
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], label="legend")
ax.legend()
tm = fig.canvas.manager.toolmanager
tm.add_tool("Zoomx", Zoomx)
fig.canvas.manager.toolbar.add_tool(tm.get_tool("Zoomx"), "toolgroup")
plt.show()
- 千万别反悔 库克称苹果不会推出变形本
- 安卓手机丢了 PC上“谷歌”一下就能找到
- 智能手机与云的强大融合
- 微软Surface平板电脑 是矛盾的存在吗?
- schema - TALLY XML API ISSUE - Stack Overflow
- python - Applying SMOTE-Tomek to nested cross validation with timeseries data - Stack Overflow
- powerbi - I have two table Fact table and Dimention table and I am trying to calculate Past year sales .Value its not working -
- responsive design - How achieve given UI using custom clipper in flutter - Stack Overflow
- google bigquery - How to Load Large Tables Beyond the 10GB Query Limit in Power BI Premium? - Stack Overflow
- django - How to attach a VPC to a Lambda through fromFunctionAttributes in CDK? - Stack Overflow
- How to find kafka broker username and password - Stack Overflow
- rust - Why is the compiler asking for Sized, when I already added it? - Stack Overflow
- c - MPI Program Hangs after MPI_Finalize - Stack Overflow
- How to filter a Drupal View using text entered into a Textfield - Stack Overflow
- Stripe Connect, Register fake account on test mode - Stack Overflow
- css - How can I apply a view-transition to my sidebar only? - Stack Overflow
- swift - Infinite Update Loop with .onGeometryChange and Padding - Stack Overflow