How to set the axis limits to be symmetric around a point in matplotlib?

Viewed 304

Is there an option that I can set in matplotlib such that the axis limits will always be symmetric around a given value?

E.g. something like this

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)

but when I add new data, then the limits are adjusted automatically?

0 Answers
Related