python - 单击矩形时如何为矩形着色?
问题描述
我正在使用 Python 进行编程;更准确地说,我正在使用该Matplotlib
库创建两个两个矩形。我想要的是当我点击一个矩形时,矩形变成绿色。我不知道该怎么做。这是我的代码:
from matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
def plot_rectangle():
fig = plt.figure(figsize=(9, 3))
plt.axis('off')
ax = plt.gca().axes
rectangles = []
rectangles.append(ax.add_patch(Rectangle((29, 2), width=.8, height=.8,
color='gray', alpha=.3)))
rectangles.append(ax.add_patch(Rectangle((14, 4), width=.8, height=.8,
color='gray', alpha=.3)))
plt.xlim(1, 32)
plt.ylim(1, 13)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.tick_params(top=False, bottom=False, left=False, right=False)
plt.show()
def onclick(event):
# when we click on a rectangle the rectangle becomes green
pass
fig.canvas.mpl_connect("button_press_event", onclick)
plot_rectangle()
解决方案
推荐阅读
- c# - 如何从 Azure AD 获取 TokenCredentials 的访问令牌?
- oauth-2.0 - AWS Amplify SPA React + Cognito(已启用 Microsoft Azure Ad Enterprise SSO)+ Microsoft Graph API
- java - Selenium getAttribute(href) 使用 java - Salesforce 应用程序返回空值
- ios - 问题:TMS-90809:不推荐使用 API - Apple 将停止接受使用 UIWebView API 的应用提交
- python - metrics.r2_score 和 acccuracy_score 有什么区别
- javascript - How to add read less button on my code after click on read more?
- ios - 数据未保存在 sql lite 核心数据库中
- python - 如果 enumerate() 循环后面跟着一个中断,那么修改它是否安全?
- mysql - MariaDB 10.3.18:如何获得 2 条具有随机和不同值的记录?
- c - 在 C 中重复数字序列