根据绘制的值范围,在 Python 中绘制颜色图会发生意外变化?

Plotting a colormap in Python changes unexpectedly depending on the range of values plotted?

我正在使用 pyplot.imshow 通过以下代码显示具有两个最小点的颜色图。 运行 但是,代码以黑色显示了两个最小点,但位置错误(它们应该位于 (x1,y1) 和 (x2,y2))。更重要的是,如果更改t_max,位置也会发生变化。我做错了什么?

import numpy
import matplotlib.pyplot as pyplot
import matplotlib.cm as cm

a = 0.1
b = 4
X = 1.1*10**7
x1, y1 = 36, 48*numpy.pi/32
x2, y2 = 49, 57*numpy.pi/32
d1 = (-101872 + X - (X - 2122) * (1+(a*(x1-x2)**2 + (b*(y1-y2))**2)))/((1/(1+(a*(x2-x1)**2 + (b*(y2-y1))**2))) - (1+(a*(x1-x2)**2 + (b*(y1-y2))**2)))
d2 = (X - d1 - 2122)*(1+(a*(x1-x2)**2 + (b*(y1-y2))**2))
def E_density(x, y):
    return X - d1/(1+(a*(x-x1)**2 + (b*(y-y1))**2)) - d2/(1+(a*(x-x2)**2 + (b*(y-y2))**2))

t_min, t_max = 14, 104
phi_min, phi_max = 47*numpy.pi/32, 61*numpy.pi/32
dt = 1
dphi = numpy.pi/128
x_axis = numpy.arange(t_min, t_max, dt)
y_axis = numpy.arange(phi_min, phi_max, dphi)

f = numpy.zeros((len(x_axis), len(y_axis)))
for ix, x in enumerate(x_axis):
    for iy, y in enumerate(y_axis):
        f[ix, iy] = E_density(x, y)

fig1 = pyplot.figure()
im = pyplot.imshow(f, extent = (t_min, t_max, phi_min, phi_max), origin = 'lower', cmap = cm.gray, aspect = 'auto', vmin = 0, vmax = 10**7)
pyplot.colorbar(im, orientation = 'vertical')

pyplot.show()

您混合了 X 轴和 Y 轴。有趣的是我只是answered another question,这个问题发生的地方。

import numpy
import matplotlib.pyplot as pyplot
import matplotlib.cm as cm

a = 0.1
b = 4
X = 1.1*10**7
x1, y1 = 36, 48*numpy.pi/32
x2, y2 = 49, 57*numpy.pi/32
d1 = (-101872 + X - (X - 2122) * (1+(a*(x1-x2)**2 + (b*(y1-y2))**2)))/((1/(1+(a*(x2-x1)**2 + (b*(y2-y1))**2))) - (1+(a*(x1-x2)**2 + (b*(y1-y2))**2)))
d2 = (X - d1 - 2122)*(1+(a*(x1-x2)**2 + (b*(y1-y2))**2))
def E_density(x, y):
    return X - d1/(1+(a*(x-x1)**2 + (b*(y-y1))**2)) - d2/(1+(a*(x-x2)**2 + (b*(y-y2))**2))

t_min, t_max = 14, 104
phi_min, phi_max = 47*numpy.pi/32, 61*numpy.pi/32
dt = 1
dphi = numpy.pi/128
x_axis = numpy.arange(t_min, t_max, dt)
y_axis = numpy.arange(phi_min, phi_max, dphi)

f = numpy.zeros((len(y_axis), len(x_axis)))
for ix, x in enumerate(x_axis):
    for iy, y in enumerate(y_axis):
        f[iy,ix] = E_density(x, y)

fig1 = pyplot.figure()
im = pyplot.imshow(f, extent = (t_min, t_max, phi_min, phi_max), origin = 'lower', cmap = cm.gray, aspect = 'auto', vmin = 0, vmax = 10**7)
pyplot.colorbar(im, orientation = 'vertical')

pyplot.show()