在matplotlib中围绕子块绘制边框

2024-05-15 15:47:11 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何在matplotlib中的一个图形中围绕单个子块绘制边界吗?我在用pyplot。

例如:

import matplotlib.pyplot as plt
f = plt.figure()
ax1 = f.add_subplot(211)
ax2 = f.add_subplot(212)
# ax1.set_edgecolor('black')

…但是Axes对象没有“edgecolor”,而且我似乎也找不到从图形级别勾勒出绘图的方法。

实际上,我正在包装mpl代码,并添加一个wx UI,其中包含一些控件,根据选择的子块,我希望具有上下文。i、 e.用户点击图形画布中的子块——子块被“选中”(有一个围绕它的轮廓,理想情况下是锯齿状的)——图形用户界面更新,以显示修改特定子块的控件。


Tags: importadd图形matplotlibas绘制plt控件
1条回答
网友
1楼 · 发布于 2024-05-15 15:47:11

你基本上想画出轴的外面,对吧?

我改编自here。它需要清理,因为我在那里使用了一些硬编码的“软糖因素”。

#!/usr/bin/env python
from pylab import *

def f(t):
    s1 = cos(2*pi*t)
    e1 = exp(-t)
    return multiply(s1,e1)

t1 = arange(0.0, 5.0, 0.1)
t2 = arange(0.0, 5.0, 0.02)
t3 = arange(0.0, 2.0, 0.01)

figure(figsize=(4, 4))
sub1 = subplot(211)
l = plot(t1, f(t1), 'bo', t2, f(t2), 'k--', markerfacecolor='green')
grid(True)
title('A tale of 2 subplots')
ylabel('Damped oscillation')

## I ADDED THIS
autoAxis = sub1.axis()
rec = Rectangle((autoAxis[0]-0.7,autoAxis[2]-0.2),(autoAxis[1]-autoAxis[0])+1,(autoAxis[3]-autoAxis[2])+0.4,fill=False,lw=2)
rec = sub1.add_patch(rec)
rec.set_clip_on(False)

subplot(212)
plot(t3, cos(2*pi*t3), 'r.')
grid(True)
xlabel('time (s)')
ylabel('Undamped')

savefig('test.png')

产生:

enter image description here

相关问题 更多 >