问题描述
有谁知道如何在 matplotlib 中的图形中的单个子图周围绘制边框?我正在使用 pyplot.
Anyone know how to draw a border around an individual subplot within a figure in matplotlib? I'm using pyplot.
例如:
import matplotlib.pyplot as plt
f = plt.figure()
ax1 = f.add_subplot(211)
ax2 = f.add_subplot(212)
# ax1.set_edgecolor('black')
..但是 Axes 对象没有边缘颜色",我似乎也找不到从图形级别勾勒出图的方法.
..but Axes objects have no 'edgecolor', and I can't seem to find a way to outline the plot from the figure level either.
我实际上是包装mpl代码并添加一个wx UI,该UI带有我希望根据选择的子图具有上下文的控件.即,用户单击图形画布中的子图-选择"子图(在其周围绘制了轮廓,最好是锯齿形)-GUI更新以显示控件以修改该特定子图.
I'm actually wrapping mpl code and adding a wx UI with controls that I would like to have context depending on which subplot is selected. i.e. User clicks on subplot within figure canvas -- subplot is 'selected' (has an outline drawn around it, ideally sawtooth) -- GUI updates to present controls to modify that specific subplot.
推荐答案
您本质上是想在坐标区之外进行绘制,对吗?
You essentially want to draw outside of the axes, right?
我改编自此处.当我在其中使用一些硬编码的忽悠因素"时,将需要清理.
I adapted this from here. It would need clean up as I used some hard-coded "fudge-factors" in there.
#!/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')
产生:
这篇关于在matplotlib中的子图周围绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!