我用可调指数创建函数的热图。这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.colors import LogNorm
alpha0 = 2.
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
bining = np.logspace(1, 10, 1000)
x = y = bining
X, Y = np.meshgrid(x, y)
Z = [[0 for i in xrange(len(x) - 1)] for j in xrange(len(y) - 1) ]
for i in xrange(len(x) - 1):
for j in xrange(len(y) - 1):
Z[i][j] = 1./((x[i]+y[j])**(2+alpha0))
Z = np.array(Z)
im = plt.pcolormesh(X,Y,Z, cmap = plt.cm.Spectral, norm=LogNorm())
plt.xscale('log')
plt.yscale('log')
plt.axes().set_aspect('equal')
axcolor = 'lightgoldenrodyellow'
axexp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
sexp = Slider(axexp, r"$\alpha$", 0.1, 10.0, valinit=alpha0)
def update(val):
alpha = sexp.val
Z = [[0 for i in xrange(len(x) - 1)] for j in xrange(len(y) - 1) ]
for i in xrange(len(x) - 1):
for j in xrange(len(y) - 1):
Z[i][j] = 1./((x[i]+y[j])**(2+alpha))
Z = np.array(Z)
plt.pcolormesh(X,Y,Z, cmap = plt.cm.Spectral, norm=LogNorm())
fig.canvas.draw_idle()
sexp.on_changed(update)
它会创建图形和滑块,但是当我尝试更改指数值时,热图不会受到影响。 所以我的问题,为什么它不起作用,我该如何解决呢?小部件可以保存吗?用什么格式?最终我将在Beamer(LaTeX)演示中使用。 谢谢!