如何让ax.clear()不清空ax.plot对象
时间: 2024-05-22 14:14:20 浏览: 11
可以使用ax.plot()函数的hold参数,将其设置为True,这样在调用ax.clear()函数时,就不会清空ax.plot对象。例如:
ax.plot(x, y, 'r', label='line 1', linewidth=2.0, hold=True)
ax.clear()
这样,即使调用了ax.clear()函数,之前绘制的线条依然会保留在图像中。
相关问题
matplotlib ax清空
在matplotlib中,可以使用`ax.clear()`方法来清空一个Axes对象(ax)中的所有绘图内容。该方法会将该Axes对象中的所有绘图元素(如曲线、散点等)全部移除,使得该Axes对象变为空白。
以下是使用`ax.clear()`方法清空Axes对象的示例代码:
```python
import matplotlib.pyplot as plt
# 创建一个Figure对象和一个Axes对象
fig, ax = plt.subplots()
# 绘制一条曲线
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
ax.plot(x, y)
# 清空Axes对象
ax.clear()
# 绘制新的曲线
x_new = [1, 2, 3]
y_new = [2, 4, 6]
ax.plot(x_new, y_new)
# 显示图形
plt.show()
```
在上述代码中,首先创建了一个Figure对象和一个Axes对象。然后使用`ax.plot()`方法绘制了一条曲线。接着调用`ax.clear()`方法清空了该Axes对象中的绘图内容。最后再次使用`ax.plot()`方法绘制了一条新的曲线。最后调用`plt.show()`方法显示图形。
from itertools import cycle import matplotlib.pyplot as plt x=[] y=[] #可用的颜色和当前颜色 colors=cycle('rgbcmyk') color=next(colors) def onMonseclick(event): global color if event.button ==1: #单击鼠标左键,绘制新直线 x.append(event.xdata) y.append(event.ydata) if len(x)>1: plt.plot([x[-2],x[-1]],[y[-2],y[-1]],c=color,lw=2) plt.xticks(range(10)) plt.yticks(range(10)) elif event.button ==3: color=next(colors) elif event.button ==2: if ax.lines: del ax.lines[-1] x.pop() y.pop() event.canvas.draw() def onclose(event): print('cLosed') def onclear(event): if event.key =='c': ax.lines.clear() x.clear() y.clear() event.canvas.draw() #创建图形 fig=plt.figure() ax=plt.gca() plt.xticks(range(10)) plt.yticks(range(10)) fig.canvas.mpl_connect('button_press_event', onMonseclick) fig.canvas.mpl_connect('key_press_event', onclear) fig.canvas.mpl_connect('close_event', onclose) plt.show()修正
这段代码是一个简单的绘图程序,使用鼠标左键点击画布可以绘制一条直线,右键可以更改颜色,中键可以删除上一条直线。同时,按下键盘上的 "c" 键可以清空画布。不过这段代码有一些错误,需要进行修正。下面是修正后的代码:
```python
from itertools import cycle
import matplotlib.pyplot as plt
x = []
y = []
# 可用的颜色和当前颜色
colors = cycle('rgbcmyk')
color = next(colors)
def onMouseClick(event):
global color
if event.button == 1:
# 单击鼠标左键,绘制新直线
x.append(event.xdata)
y.append(event.ydata)
if len(x) > 1:
plt.plot([x[-2], x[-1]], [y[-2], y[-1]], c=color, lw=2)
elif event.button == 3:
color = next(colors)
elif event.button == 2:
if ax.lines:
del ax.lines[-1]
x.pop()
y.pop()
event.canvas.draw()
def onClose(event):
print('Closed')
def onClear(event):
if event.key == 'c':
ax.lines.clear()
x.clear()
y.clear()
event.canvas.draw()
# 创建图形
fig = plt.figure()
ax = plt.gca()
plt.xticks(range(10))
plt.yticks(range(10))
fig.canvas.mpl_connect('button_press_event', onMouseClick)
fig.canvas.mpl_connect('key_press_event', onClear)
fig.canvas.mpl_connect('close_event', onClose)
plt.show()
```
主要的修改如下:
1. `onMonseclick` 改为 `onMouseClick`,修正了单词拼写错误;
2. 在 `onMouseClick` 函数中,增加了 `event.button == 2` 的判断,即中键的点击事件;
3. `onclose` 改为 `onClose`,修正了单词拼写错误;
4. `onclear` 改为 `onClear`,修正了单词拼写错误;
5. 在 `onClear` 函数中,增加了事件处理后的画布重绘操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)