Python Matplotlib:颜色栏不断缩小
我编写了一个代码,其中有一个名为Python Matplotlib:颜色栏不断缩小,python,matplotlib,colorbar,Python,Matplotlib,Colorbar,我编写了一个代码,其中有一个名为array2的数组,其中的数字介于0.和1.之间。当我点击imshow显示的数组时,数组中的单元格取值2.并变为红色 然后我添加了一个色条,但一旦我点击它,色条就一直缩小,细胞没有变红 我做错了什么 不带色条的代码(工作正常) 使用颜色条编码(不起作用) 最好只更新imshow,而不是每次单击时都绘制一个新的。这可以使用.set\u data()方法完成。优点是颜色条可以保持在原来的位置,不会被触摸 一般来说,在进行交互式工作时,最好直接使用绘图对象,而不是py
array2
的数组,其中的数字介于0.
和1.
之间。当我点击imshow
显示的数组时,数组中的单元格取值2.
并变为红色
然后我添加了一个色条,但一旦我点击它,色条就一直缩小,细胞没有变红
我做错了什么
不带色条的代码(工作正常)
使用颜色条编码(不起作用)
最好只更新
imshow
,而不是每次单击时都绘制一个新的。这可以使用.set\u data()
方法完成。优点是颜色条可以保持在原来的位置,不会被触摸
一般来说,在进行交互式工作时,最好直接使用绘图对象,而不是pyplot。因此,在大多数情况下,使用fig
和ax
而不是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
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)
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)
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()