我想繪製一個矩形到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()
矩形恰恰是沒有任何地方在圖繪製。 (),get_x(),leg.get_frame()。get_y()),leg.get_frame()。get_width()和leg.get_frame()。get_height(),I看到它們分別是0.0,0.0,1.0和1.0,。
因此,我的問題是要找到圖例框架的座標。
如果你能幫助我,這將是非常好的。
感謝您閱讀這篇文章。
你爲什麼要這麼做?你確定沒有內置到'legend'對象中的東西可以幫你嗎? – tacaswell