python - 使用 Matplotlib (Python) 绘图内的绘图和按钮
问题描述
我正在尝试自学如何集成plots
,buttons
并且通过该扩展,实际使用Classes
.
我给自己设定的挑战是:
- 创建散点图(完成)
- 创建一个在现有散点图中生成新散点图的按钮(完成)
- 在新散点图中创建一个按钮(完成)
- 为新按钮分配关闭新散点图的功能(卡在这里)
这是我到目前为止的进展:
我的代码如下:
欢迎任何提示!
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
# Junk data for the purpose of my question
x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
fig, (ax1) = plt.subplots()
ax1.scatter(x, y) # plot (x, y) data as a scatter plot
class Initial_plot(object):
def close_plot(self, event):
plt.close()
def add_axis(self, event):
plt.axes([0.3, 0.2, 0.5, 0.51])
x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
plt.scatter(x, y)
class Second_plot(object):
def close_addplot(self, event):
plt.close()
# Button to close the new scatter plot
axclose_addplot = plt.axes([0.4, 0.4, 0.15, 0.1])
bclose_addplot = Button(axclose_addplot, 'Close add plot')
bclose_addplot.on_clicked(callback.close_addplot)
callback = Initial_plot()
# Add button for a new scatter plot
axadd_plot = plt.axes([0.5, 0.01, 0.1, 0.071])
badd_plot = Button(axadd_plot, 'Add Plot')
badd_plot.on_clicked(callback.add_axis)
# Exit Button for inital scatter plot
axclose_plot = plt.axes([0.7, 0.01, 0.1, 0.071]) # co-ords of button
bclose_plot = Button(axclose_plot, 'Close') # name of button
bclose_plot.on_clicked(callback.close_plot) # enables event upon clicking the button
plt.show() #display the plot
解决方案
推荐阅读
- highcharts - 在同一页面中显示多个图表
- javascript - 如何在画布面料JS上绘制线条动画
- python - 无法在 tensorflow-gpu 上使用 GPU:“无法创建 cudnn 句柄:CUDNN_STATUS_INTERNAL_ERROR”
- c# - 我可以在另一个线程上放一个取消按钮吗?
- json - 如何在后端从 JSON 读取数组
- angular - ng-bootstrap 下拉位置不适用于 Angular 7
- wordpress - Apache2 设置多个站点,子域不起作用?
- c# - 如何覆盖 log4net AdoNetAppender 的 CommandText
- swift - NSMallocException:从 CGDataProvider 加载时无法增加缓冲区
- loops - 在 Jekyll 中,如何在循环外捕获年份和月份?