与此问题类似change text style inline in matplotlib我想写两个不同风格的标签(粗体和普通,例如" 粗体正常"),好像它们是不使用乳胶的相同标签。
这是它正在发挥作用的东西:
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
def print_label(fig):
l = fig.text(0.5, 0.5, "bold", fontweight='bold')
def on_draw(event):
xmax = l.get_window_extent().inverse_transformed(fig.transFigure).max[0]
print xmax
fig.text(xmax, 0.5, "Normal")
return False
fig.canvas.mpl_connect('draw_event', on_draw)
print_label(fig)
fig.canvas.draw()
plt.show()
但我不满意,因为print_label
的使用不透明:用户必须致电fig.canvas.draw()
。所以我尝试了这个:
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
def print_label(fig):
l = fig.text(0.5, 0.5, "bold", fontweight='bold')
def on_draw(event):
xmax = l.get_window_extent().inverse_transformed(fig.transFigure).max[0]
print xmax
fig.text(xmax, 0.5, "Normal")
fig.canvas.mpl_disconnect(event)
fig.canvas.draw()
return False
fig.canvas.mpl_connect('draw_event', on_draw)
print_label(fig)
plt.show()
问题是我无法断开事件,我得到了:
RuntimeError: maximum recursion depth exceeded while calling a Python object
答案 0 :(得分:1)
我按照第一部分:http://matplotlib.org/users/event_handling.html
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
def print_label(fig):
l = fig.text(0.5, 0.5, "bold", fontweight='bold')
def on_draw(event):
xmax = l.get_window_extent().inverse_transformed(fig.transFigure).max[0]
print xmax
fig.text(xmax, 0.5, "Normal")
fig.canvas.mpl_disconnect(cid)
return False
cid = fig.canvas.mpl_connect('draw_event', on_draw)
fig.canvas.draw()
print_label(fig)
plt.show()