gpt4 book ai didi

python - 给定范围内的 matplotlib 等高线图

转载 作者:行者123 更新时间:2023-11-28 22:53:34 25 4
gpt4 key购买 nike

我需要像下图那样画椭圆。代码取自一个教程。但我需要在 X:[-10,-10] 和 Y: [-2, -2] 范围内绘制椭圆。我应该如何修改我的代码?

fig = plt.figure()
x = linspace(0, 10, 51)
y = linspace(0, 8, 41)
(X, Y) = meshgrid(x, y)
a = exp(-((X - 2.5) ** 2 + (Y - 4) ** 2) / 4)
c = plt.contour(x, y, a)
plt.show()

enter image description here

最佳答案

plt.xlim(-10, 10)
plt.ylim(-2, 2)

将绘图限制在该区域。


import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
x = np.linspace(-10, 10, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
a = np.exp(-((X - 0) ** 2 + 3*(Y - 0) ** 2) / 4)
c = plt.contour(x, y, a)
plt.xlim(-10, 10)
plt.ylim(-2, 2)
plt.show()

enter image description here


1*(X - A) ** 2 + 3*(Y - B) ** 2) == 0

是中心在 (A, B) 的椭圆的方程。调整常数(例如 1 和 3),看看它如何在 X 和 Y 方向上拉伸(stretch)椭圆。

关于python - 给定范围内的 matplotlib 等高线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19335057/

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