小编use*_*021的帖子

如何在matplotlib中的图例上绘制一个矩形?

我试图在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()
Run Code Online (Sandbox Code Playgroud)

矩形只是不在图中的任何位置绘制.如果我查看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.

因此我的问题是,找到传奇框架的坐标.

如果你可以帮助我,真的很棒.

感谢您阅读这篇文章.

python matplotlib legend

5
推荐指数
1
解决办法
2472
查看次数

标签 统计

legend ×1

matplotlib ×1

python ×1