用线条制作 Visible/Unvisible 个注释 (matplotlib)
Make Visible/Unvisible Annotations with Lines (mathplotlib)
我有一个包含 2 条线的图表,并使用复选按钮制作了 visible/invisible 条线。但是,我无法对注释做同样的事情。我想当我检查 label2 时也做 visible/invisible 注释。我无法从这里或网上找到任何解决方案。有什么想法吗?
这是我的代码示例:
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
kansekeri=[333, 111, 111]
inshiz=[3.0, 3.0, 2.5]
zaman=['01-04-2021 04:02', '01-04-2021 04:02', '01-04-2021 04:02']
fig = plt.figure(figsize=(10, 5))
fig.canvas.set_window_title('-')
ax1 = fig.add_subplot()
l0,=ax1.plot(zaman, inshiz, color='tab:blue', marker='.', label='İnsülin Hızı')
ax1.set_xlabel('Tarih - Saat')
ax1.set_ylabel('İnsülin Hızı (u/h)', color='tab:blue')
ax1.tick_params(axis='y',labelcolor='tab:blue')
ax1.tick_params(axis='x', labelsize=7)
ax2 = ax1.twinx()
l1,=ax2.plot(zaman, kansekeri, color='tab:red', marker='.', label="Kan Şekeri")
ax2.set_ylabel('Kan Şekeri',color='tab:red')
ax2.tick_params(labelcolor='tab:red')
for x,y in zip(zaman,kansekeri):
label = "{:.1f}".format(y)
ann=plt.annotate(label, # this is the text
(x,y),
textcoords="offset points",
xytext=(0,7),
ha='center',fontsize=7)
lines = [l0, l1]
rax = plt.axes([0.05, 0.7, 0.1, 0.15])
labels = [str(line.get_label()) for line in lines]
visibility = [line.get_visible() for line in lines]
check = CheckButtons(rax, labels, visibility)
def func(label):
index = labels.index(label)
lines[index].set_visible(not lines[index].get_visible())
plt.draw()
check.on_clicked(func)
plt.subplots_adjust(left=0.2)
plt.grid()
plt.show()
您也可以将注释保存在一个列表中:annotations = [plt.annotate(f"{y:.1f}", (x, y), ...) for x, y in zip(zaman, kansekeri)]
然后做一些与线条类似的事情。
下面是一些示例代码:
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
kansekeri = [333, 111, 111]
inshiz = [3.0, 3.0, 2.5]
zaman = ['01-04-2021 04:02', '01-04-2021 04:02', '01-04-2021 04:02']
fig = plt.figure(figsize=(10, 5))
fig.canvas.set_window_title('-')
ax1 = fig.add_subplot()
l0, = ax1.plot(zaman, inshiz, color='tab:blue', marker='.', label='İnsülin Hızı')
ax1.set_xlabel('Tarih - Saat')
ax1.set_ylabel('İnsülin Hızı (u/h)', color='tab:blue')
ax1.tick_params(axis='y', labelcolor='tab:blue')
ax1.tick_params(axis='x', labelsize=7)
ax2 = ax1.twinx()
l1, = ax2.plot(zaman, kansekeri, color='tab:red', marker='.', label="Kan Şekeri")
ax2.set_ylabel('Kan Şekeri', color='tab:red')
ax2.tick_params(labelcolor='tab:red')
annotations = [plt.annotate(f"{y:.1f}",
(x, y),
textcoords="offset points",
xytext=(0, 7),
ha='center', fontsize=7)
for x, y in zip(zaman, kansekeri)]
lines = [l0, l1]
rax = plt.axes([0.05, 0.7, 0.1, 0.15])
labels = [str(line.get_label()) for line in lines]
visibility = [line.get_visible() for line in lines]
check = CheckButtons(rax, labels, visibility)
def func(label):
index = labels.index(label)
new_line_visibility = not lines[index].get_visible()
lines[index].set_visible(new_line_visibility)
if index == 1:
for ann in annotations:
ann.set_visible(new_line_visibility)
plt.draw()
check.on_clicked(func)
plt.subplots_adjust(left=0.2)
plt.grid()
plt.show()
我有一个包含 2 条线的图表,并使用复选按钮制作了 visible/invisible 条线。但是,我无法对注释做同样的事情。我想当我检查 label2 时也做 visible/invisible 注释。我无法从这里或网上找到任何解决方案。有什么想法吗?
这是我的代码示例:
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
kansekeri=[333, 111, 111]
inshiz=[3.0, 3.0, 2.5]
zaman=['01-04-2021 04:02', '01-04-2021 04:02', '01-04-2021 04:02']
fig = plt.figure(figsize=(10, 5))
fig.canvas.set_window_title('-')
ax1 = fig.add_subplot()
l0,=ax1.plot(zaman, inshiz, color='tab:blue', marker='.', label='İnsülin Hızı')
ax1.set_xlabel('Tarih - Saat')
ax1.set_ylabel('İnsülin Hızı (u/h)', color='tab:blue')
ax1.tick_params(axis='y',labelcolor='tab:blue')
ax1.tick_params(axis='x', labelsize=7)
ax2 = ax1.twinx()
l1,=ax2.plot(zaman, kansekeri, color='tab:red', marker='.', label="Kan Şekeri")
ax2.set_ylabel('Kan Şekeri',color='tab:red')
ax2.tick_params(labelcolor='tab:red')
for x,y in zip(zaman,kansekeri):
label = "{:.1f}".format(y)
ann=plt.annotate(label, # this is the text
(x,y),
textcoords="offset points",
xytext=(0,7),
ha='center',fontsize=7)
lines = [l0, l1]
rax = plt.axes([0.05, 0.7, 0.1, 0.15])
labels = [str(line.get_label()) for line in lines]
visibility = [line.get_visible() for line in lines]
check = CheckButtons(rax, labels, visibility)
def func(label):
index = labels.index(label)
lines[index].set_visible(not lines[index].get_visible())
plt.draw()
check.on_clicked(func)
plt.subplots_adjust(left=0.2)
plt.grid()
plt.show()
您也可以将注释保存在一个列表中:annotations = [plt.annotate(f"{y:.1f}", (x, y), ...) for x, y in zip(zaman, kansekeri)]
然后做一些与线条类似的事情。
下面是一些示例代码:
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
kansekeri = [333, 111, 111]
inshiz = [3.0, 3.0, 2.5]
zaman = ['01-04-2021 04:02', '01-04-2021 04:02', '01-04-2021 04:02']
fig = plt.figure(figsize=(10, 5))
fig.canvas.set_window_title('-')
ax1 = fig.add_subplot()
l0, = ax1.plot(zaman, inshiz, color='tab:blue', marker='.', label='İnsülin Hızı')
ax1.set_xlabel('Tarih - Saat')
ax1.set_ylabel('İnsülin Hızı (u/h)', color='tab:blue')
ax1.tick_params(axis='y', labelcolor='tab:blue')
ax1.tick_params(axis='x', labelsize=7)
ax2 = ax1.twinx()
l1, = ax2.plot(zaman, kansekeri, color='tab:red', marker='.', label="Kan Şekeri")
ax2.set_ylabel('Kan Şekeri', color='tab:red')
ax2.tick_params(labelcolor='tab:red')
annotations = [plt.annotate(f"{y:.1f}",
(x, y),
textcoords="offset points",
xytext=(0, 7),
ha='center', fontsize=7)
for x, y in zip(zaman, kansekeri)]
lines = [l0, l1]
rax = plt.axes([0.05, 0.7, 0.1, 0.15])
labels = [str(line.get_label()) for line in lines]
visibility = [line.get_visible() for line in lines]
check = CheckButtons(rax, labels, visibility)
def func(label):
index = labels.index(label)
new_line_visibility = not lines[index].get_visible()
lines[index].set_visible(new_line_visibility)
if index == 1:
for ann in annotations:
ann.set_visible(new_line_visibility)
plt.draw()
check.on_clicked(func)
plt.subplots_adjust(left=0.2)
plt.grid()
plt.show()