Last active
August 19, 2021 06:40
-
-
Save winston-wen/a9fd87c864afe0b92c4b4f07a27ad787 to your computer and use it in GitHub Desktop.
matplotlib用法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| import matplotlib.patches as pch | |
| rec = pch.Rectangle((x1, y1), w, h, linewidth=1, edgecolor='r', facecolor='none') | |
| plt.gca().add_patch(rec) | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from skimage.io import imread | |
| from matplotlib.backend_bases import MouseButton | |
| import matplotlib.pyplot as plt | |
| import numpy as np # Not used. | |
| def on_click(event): | |
| ctx = on_click | |
| if event.button is MouseButton.LEFT: | |
| x, y = event.xdata, event.ydata | |
| if x is not None and y is not None: | |
| x, y, i = int(x), int(y), ctx.args | |
| ctx.out = (x, y) | |
| print(f'At {i}: ({x}, {y})') | |
| for i in range(10): | |
| on_click.args = i # 通过给函数对象外挂属性来传递参数. | |
| plt.connect('button_press_event', on_click) | |
| img = imread("https://picsum.photos/200/300") | |
| plt.imshow(img) | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from skimage.io import imread | |
| from matplotlib.backend_bases import MouseButton | |
| import matplotlib.pyplot as plt | |
| import numpy as np # Not used. | |
| def on_click(event): | |
| ctx = on_click | |
| if event.button is MouseButton.LEFT: | |
| x, y = event.xdata, event.ydata | |
| if x is not None and y is not None: | |
| x, y, i = int(x), int(y), ctx.args | |
| ctx.out = (x, y) | |
| scat = plt.scatter(x, y, c="#f15bb5", marker="P", s=144) | |
| if hasattr(ctx, "scat"): | |
| ctx.scat.remove() | |
| ctx.scat = scat | |
| plt.draw() | |
| print(f'At {i}: ({x}, {y})') | |
| elif event.button is MouseButton.RIGHT: | |
| plt.close() | |
| for i in range(10): | |
| on_click.args = i # 通过给函数对象外挂属性来传递参数. | |
| plt.connect('button_press_event', on_click) | |
| img = imread("https://picsum.photos/200/300") | |
| plt.imshow(img) | |
| plt.tight_layout() | |
| plt.get_current_fig_manager().window.showMaximized() | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment