gpt4 book ai didi

matplotlib - matplotlib 中的散点图未更新 xlim 和 ylim

转载 作者:行者123 更新时间:2023-12-02 00:29:35 25 4
gpt4 key购买 nike

鉴于下面的代码,我希望 x 轴在 0 到 3 之间,并添加一些边距。相反,它要大得多。我希望对 scatter 的调用能够自动更新 x 轴限制。我可以自己设置 xlim 和 ylim,但希望它们自动设置。我做错了什么?

import matplotlib.pyplot as plt

if __name__ == '__main__':

fig = plt.figure()
ax = fig.add_subplot(111)

x = [0, 4000, 8000, 100000]
y = [0, 10, 100, 150]
ax.scatter(x, y)

x = [0, 1, 2, 3]
y = x

ax.clear()

ax.scatter(x, y)
plt.show()

enter image description here

最佳答案

您可以清除图形,然后打开一个新的子图,然后轴将根据您的需要进行调整。

fig = plt.figure()
ax = fig.add_subplot(111)

x = [0, 4000, 8000, 100000]
y = [0, 10, 100, 150]
ax.scatter(x, y)

plt.clf()
ax = fig.add_subplot(111)

x = [0, 1, 2, 3]
y = x

ax.scatter(x, y)
plt.show()

编辑:在这个版本中,图形没有关闭,只是用clf函数清除。

关于matplotlib - matplotlib 中的散点图未更新 xlim 和 ylim,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688761/

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