gpt4 book ai didi

matplotlib - 注释框不会出现在 matplotlib 中

转载 作者:行者123 更新时间:2023-12-03 11:23:40 24 4
gpt4 key购买 nike

计划的注释框没有出现在我的绘图上,但是,我已经尝试了广泛的坐标值。

那有什么问题?!

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def f(s,t):
a = 0.7
b = 0.8
Iext= 0.5
tau = 12.5
v = s[0]
w = s[1]
dndt = v - np.power(v,3)/3 - w + Iext
dwdt = (v + a - b * w)/tau
return [dndt, dwdt]

t = np.linspace(0,200)
s0=[1,1]

s = odeint(f,s0,t)

plt.plot(t,s[:,0],'b-', linewidth=1.0)
plt.xlabel(r"$t(sec.)$")
plt.ylabel(r"$V (volt)$")
plt.legend([r"$V$"])

annotation_string = r"$I_{ext}=0.5$"
plt.text(15, 60, annotation_string, bbox=dict(facecolor='red', alpha=0.5))

plt.show()

最佳答案

坐标为plt.text默认情况下是数据坐标。这意味着为了出现在图中,它们不应超过图的数据限制(此处,x 方向为 ~0..200,y 方向为 ~-2..2)。

plt.text(10,1.8)应该管用。

这样做的问题是,一旦数据限制发生变化(因为您绘制不同的东西或添加另一个图),文本项将位于 Canvas 内的不同位置。

如果不希望这样做,您可以在轴坐标中指定文本(在两个方向上从 0 到 1)。为了将文本始终放置在轴的左上角,独立于您在那里绘制的内容,您可以使用例如

plt.text(0.03,0.97, annotation_string, bbox=dict(facecolor='red', alpha=0.5), 
transform=plt.gca().transAxes, va = "top", ha="left")

这里 transform关键字告诉文本使用轴坐标, va = "top", ha="left"意味着,文本的左上角应该是 anchor 。

关于matplotlib - 注释框不会出现在 matplotlib 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com