python - In a matplotlib plot is there a way to automatically set the xlim after it has been set then set to None? - Stack Overf

I am working on a GUI where a user can specify the both the min and max x limit. When the value is left

I am working on a GUI where a user can specify the both the min and max x limit. When the value is left blank I would like it to be automatically calculated. One limit can be set while the other is automatically calculated by setting it to None. But after setting one limit and then setting it to None does not automatically update the limit. Both values can be automatically calculated by using set_xlim(auto=True) but this forces both values to be automatically calculated and does not allow only one value to be automatically calculated. Is there a way of having just one limit automatically re-calculated? For example in MATLAB xlim[-Inf, 4] would automatically calculate the first limit.

Below is an example without a GUI

import matplotlib.pyplot as plt

x_data = [1, 2, 3, 4, 5]
y_data = [1, 2, 3, 4, 5]

fig, ax = plt.subplots(1)

ax.plot(x_data, y_data, '.')

# Initially limits are automatically calculated
ax.set_xlim(auto=True)
print(ax.get_xlim()) # prints: (np.float64(0.8), np.float64(5.2))

# User sets value with GUI
ax.set_xlim(2, 4)
print(ax.get_xlim()) # prints: (np.float64(2.0), np.float64(4.0))

# Later user changes their mind and leaves the first limit blank
ax.set_xlim(None, 4) # 
print(ax.get_xlim()) # prints: (np.float64(2.0), np.float64(4.0))

plt.show()

I am working on a GUI where a user can specify the both the min and max x limit. When the value is left blank I would like it to be automatically calculated. One limit can be set while the other is automatically calculated by setting it to None. But after setting one limit and then setting it to None does not automatically update the limit. Both values can be automatically calculated by using set_xlim(auto=True) but this forces both values to be automatically calculated and does not allow only one value to be automatically calculated. Is there a way of having just one limit automatically re-calculated? For example in MATLAB xlim[-Inf, 4] would automatically calculate the first limit.

Below is an example without a GUI

import matplotlib.pyplot as plt

x_data = [1, 2, 3, 4, 5]
y_data = [1, 2, 3, 4, 5]

fig, ax = plt.subplots(1)

ax.plot(x_data, y_data, '.')

# Initially limits are automatically calculated
ax.set_xlim(auto=True)
print(ax.get_xlim()) # prints: (np.float64(0.8), np.float64(5.2))

# User sets value with GUI
ax.set_xlim(2, 4)
print(ax.get_xlim()) # prints: (np.float64(2.0), np.float64(4.0))

# Later user changes their mind and leaves the first limit blank
ax.set_xlim(None, 4) # 
print(ax.get_xlim()) # prints: (np.float64(2.0), np.float64(4.0))

plt.show()
Share Improve this question asked Feb 20 at 23:24 James LondonJames London 233 bronze badges 8
  • stackoverflow/a/66805331/9877065 --> question cited above ; ax.set_ylim(bottom=None, top=7) – pippo1980 Commented Feb 21 at 0:11
  • sorry , was to fast voting to reopen , doesnt workk in matplotlib 3.7 too ; once set None doesnt work – pippo1980 Commented Feb 21 at 0:17
  • see matplotlib./stable/api/_as_gen/… ----> Passing None leaves the limit unchanged. – pippo1980 Commented Feb 21 at 0:31
  • just pass automatically calculated limit to a tuple object --> auto = ax.get_xlim() and then instead of ax.set_xlim(None, 4) use --> ax.set_xlim(auto[0], 4) – pippo1980 Commented Feb 21 at 0:44
  • 2 If you call ax.set_xlim(auto=True) again followed by ax.autoscale_view() then it resets to what you originally had. Then you can go ahead and call ax.set_xlim(None, 4)). – RuthC Commented Feb 21 at 11:42
 |  Show 3 more comments

2 Answers 2

Reset to default 2

As noted in the comment, we can "reset" to automatic axis limits using ax.set_xlim(auto=True) again followed by ax.autoscale_view().

# Later user changes their mind and leaves the first limit blank
ax.set_xlim(auto=True)
ax.autoscale_view()
ax.set_xlim(None, 4)
print(ax.get_xlim()) # prints: (0.8, 4.0)

Got another way to do it, don't now why it works though.

Code:

import matplotlib.pyplot as plt

from matplotlib import __version__

print('\n\nMatplotlib  version  : ', __version__ ,'\n\n')

x_data = [1, 2, 3, 4, 5]
y_data = [1, 2, 3, 4, 5]

fig, ax = plt.subplots(1)

ax.plot(x_data, y_data, '.')

# Initially limits are automatically calculated
ax.set_xlim(auto=True)
print(ax.get_xlim()) # prints: (np.float64(0.8), np.float64(5.2))

# User sets value with GUI
ax.set_xlim(2, 4)
print(ax.get_xlim()) # prints: (np.float64(2.0), np.float64(4.0))

# Later user changes their mind and leaves the first limit blank

print('before None : ', ax.get_xlim())
#ax.set_xlim(auto=True)
#ax.autoscale_view()
#ax.set_xlim(None, 4) #
print('after None : ', ax.get_xlim())
print('before autoscale : ', ax.get_xlim())


ax.autoscale(enable=True, axis='x', tight=None)
ax.set_xlim(None, 4) #
print('after autoscale : ', ax.get_xlim())

print(ax.get_xlim()) # prints: (np.float64(2.0), np.float64(4.0))

plt.show()

Output:

Matplotlib  version  :  3.8.4 


(np.float64(0.8), np.float64(5.2))
(np.float64(2.0), np.float64(4.0))
before None :  (np.float64(2.0), np.float64(4.0))
after None :  (np.float64(2.0), np.float64(4.0))
before autoscale :  (np.float64(2.0), np.float64(4.0))
after autoscale :  (np.float64(0.8), np.float64(4.0))
(np.float64(0.8), np.float64(4.0))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745173876a4615090.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信