gpt4 book ai didi

python - Matplotlib : the colorbar keeps shrinking

转载 作者:行者123 更新时间:2023-12-01 02:49:03 26 4
gpt4 key购买 nike

我编写了一个代码,其中有一个名为 array2 的数组,其中包含 0.1. 之间的数字。当我单击 imshow 显示的数组时,数组中的单元格的值为 2. 并变为红色。

然后我添加了一个颜色条,但单击它后它不断缩小,并且单元格没有变成红色。

我做错了什么?

没有颜色条的代码(工作正常)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from random import random


def test(n):
array1 = np.zeros((n,n))
for i in range(n):
for j in range(n):
array1[i,j] = random()
return array1


# Array
global array2
array2 = test(10)


# Colormap
greens = cm.Greens(np.linspace(0,1, num=50))
greensfill = cm.Greens(np.ones(25))
red = [(1,0,0,1)]*len(greens)
gray = [(.5,.5,.5,1)]*len(greens)
colors = np.vstack((greens, greensfill, red, gray))
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)


# Matplotlib
fig, axes = plt.subplots(1)
fig.tight_layout()

plt.imshow(array2, animated=True, cmap = mycmap, interpolation="none", vmin=0, vmax=3.5, origin='lower')


def onclick(event):
global x, y
x, y = int(event.xdata), int(event.ydata)
array2[y,x] = 2.

plt.imshow(array2, animated=True, cmap = mycmap, interpolation="none", vmin=0, vmax=3.5, origin='lower')


fig.canvas.mpl_connect('button_press_event', onclick)

enter image description here

带有颜色条的代码(不起作用)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from random import random


def test(n):
array1 = np.zeros((n,n))
for i in range(n):
for j in range(n):
array1[i,j] = random()
return array1


# Array
global array2
array2 = test(10)


# Colormap
greens = cm.Greens(np.linspace(0,1, num=50))
greensfill = cm.Greens(np.ones(25))
red = [(1,0,0,1)]*len(greens)
gray = [(.5,.5,.5,1)]*len(greens)
colors = np.vstack((greens, greensfill, red, gray))
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)


# Matplotlib
fig, axes = plt.subplots(1)
fig.tight_layout()

im = plt.imshow(array2, animated=True, cmap = mycmap, interpolation="none", vmin=0, vmax=3.5, origin='lower')

divider = make_axes_locatable(axes)
cax = divider.append_axes("right", size="13%", pad=0.2)

cb = plt.colorbar(im, cax=cax, boundaries=np.linspace(0,1, num=100), ticks=[0,1])

cb.set_label("Title", fontsize=15, labelpad=-5, y=0.5)

def onclick(event):
global x, y
x, y = int(event.xdata), int(event.ydata)
array2[y,x] = 2.

im = plt.imshow(array2, animated=True, cmap = mycmap, interpolation="none", vmin=0, vmax=3.5, origin='lower')

divider = make_axes_locatable(axes)
cax = divider.append_axes("right", size="13%", pad=0.2)

cb = plt.colorbar(im, cax=cax, boundaries=np.linspace(0,1, num=100), ticks=[0,1])

cb.set_label("Title", fontsize=15, labelpad=-5, y=0.5)


fig.canvas.mpl_connect('button_press_event', onclick)

enter image description here

最佳答案

最好只更新 imshow,而不是每次执行单击时都绘制一个新的。这可以使用 .set_data() 方法来完成。优点是颜色条可以留在原处并且​​不会被触摸。

一般来说,在进行交互式操作时,最好直接使用绘图对象而不是 pyplot。因此,在大多数情况下,使用 figax 而不是 plt

请注意,要准确捕获像素上的点击,您需要先对坐标进行舍入,int(np.round(event.xdata))

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable

global array2
array2 = np.random.rand(10,10)

# Colormap
greens = cm.Greens(np.linspace(0,1, num=50))
greensfill = cm.Greens(np.ones(25))
red = [(1,0,0,1)]*len(greens)
gray = [(.5,.5,.5,1)]*len(greens)
colors = np.vstack((greens, greensfill, red, gray))
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)

# Matplotlib
fig, ax = plt.subplots()
fig.tight_layout()
im = ax.imshow(array2, animated=True, cmap = mycmap, interpolation="none",
vmin=0, vmax=3.5, origin='lower')

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="13%", pad=0.2)
cb = fig.colorbar(im, ax =ax, cax=cax, boundaries=np.linspace(0,1, num=100),
ticks=[0,1])
cb.set_label("Title", fontsize=15, labelpad=-5, y=0.5)

def onclick(event):
x, y = int(np.round(event.xdata)), int(np.round(event.ydata))
array2[y,x] = 2.
im.set_data(array2)
fig.canvas.draw_idle()

fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

关于python - Matplotlib : the colorbar keeps shrinking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44991394/

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