如何将轴限制设置为围绕matplotlib中的点对称?

2024-09-25 06:35:36 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有可以在matplotlib中设置的选项,以便轴限制始终围绕给定值对称

像这样的东西

import numpy as np
import matplotlib.pyplot as plt


x = np.arange(10)
y = np.random.uniform(0,1,10)

center = 0.5

maxdiff = np.max(np.abs(y-center))

ymin = center-maxdiff - 0.1
ymax = center+maxdiff + 0.1



fig,ax = plt.subplots()
plt.plot(x,y)
ax.set_ylim(ymin,ymax)

但当我添加新数据时,限制会自动调整吗


Tags: importnumpymatplotlibas选项nppltrandom