【发布时间】:2012-10-03 04:46:44
【问题描述】:
我正在尝试在 matplotlib 中的图例上绘制一个矩形。
为了说明我已经走了多远,我展示了我的最佳尝试,但这不起作用:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
Fig = plt.figure()
ax = plt.subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax.plot(t, s1, 'b-', label = 'dots')
leg = ax.legend()
rectangle = Rectangle((leg.get_frame().get_x(),
leg.get_frame().get_y()),
leg.get_frame().get_width(),
leg.get_frame().get_height(),
fc = 'red'
)
ax.add_patch(rectangle)
plt.show()
矩形只是没有在图中的任何地方绘制。 如果我查看 leg.get_frame().get_x()、leg.get_frame().get_y())、leg.get_frame().get_width() 和 leg.get_frame().get_height() 的值,我看到了他们是 分别为 0.0、0.0、1.0 和 1.0。
因此,我的问题是找到图例框架的坐标。
如果你能帮助我,那就太好了。
感谢您阅读本文。
【问题讨论】:
-
你为什么要这样做?你确定
legend对象中没有内置的东西可以为你做这件事吗?
标签: python matplotlib legend