python 在 matplotlib 中在子图周围绘制边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2027592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
draw a border around subplots in matplotlib
提问by Adam Fraser
Anyone know how to draw a border around an individual subplot within a figure in matplotlib? I'm using pyplot.
任何人都知道如何在 matplotlib 中的图形中围绕单个子图绘制边框?我正在使用 pyplot。
eg:
例如:
import matplotlib.pyplot as plt
f = plt.figure()
ax1 = f.add_subplot(211)
ax2 = f.add_subplot(212)
# ax1.set_edgecolor('black')
..but Axes objects have no 'edgecolor', and I can't seem to find a way to outline the plot from the figure level either.
..但是 Axes 对象没有“edgecolor”,我似乎也找不到从图形级别勾勒出情节的方法。
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.
我实际上是在包装 mpl 代码并添加一个带有控件的 wx UI,我希望根据选择的子图来设置上下文。即用户点击图形画布中的子图——子图被“选择”(在它周围画了一个轮廓,最好是锯齿)——GUI 更新以呈现控件来修改特定的子图。
采纳答案by Mark
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')
Produces:
产生: