- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个代码,其中有一个名为 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)
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)
最佳答案
最好只更新 imshow
,而不是每次执行单击时都绘制一个新的。这可以使用 .set_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
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/
目的 在各种各样的理论计算中,常常需要绘制各种填充图,绘制完后需要加渐变填充的colorbar。可是有些软件如VMD,colorbar渲染后颜色分布有些失真,不能较准确的表达各颜色对应的数值。用p
我正在尝试做一些我通常认为微不足道但在 Bokeh 中似乎非常困难的事情:将垂直颜色条添加到绘图中,然后将颜色条的标题(也就是颜色映射背后的变量)显示在一侧颜色条,但从水平方向顺时针旋转 90 度。
我有一个与 this 中的问题类似的问题发布。 我有一个灰度图像并在其上绘制点。来回绘制点我使用 colormap('jet') 但是因为我希望图像是灰度的,所以在绘制点之后我重置了颜色图,color
我在 matlab 中有二维线图,其中每条线都根据一个值着色。我想添加一个颜色条,显示与这些值对应的颜色。 我得到了一个根据我想要的值绘制线条的解决方案,但是我无法弄清楚如何正确获取颜色条。我一直在搜
我有一个颜色列表 ( created from values ) 并想在颜色栏中显示它们(作为图例,每种颜色的含义)。像这样。 一种方法是使用 1 行/n 列 (n = 25-100) 的表格,每列代
我用一个愚蠢的问题来打扰你,但我找不到遮阳篷。我正在尝试使用 matplotlib 向图像添加颜色条。当我尝试格式化颜色条的刻度时,问题就来了。我想要一个科学记数法格式,但同时控制刻度中的小数位数。例
创建 matplotlib 颜色条时,可以将 drawedges 设置为 True,用黑线分隔颜色条的颜色。但是,当使用 extend='both' 扩展颜色栏时,四肢的黑线不会显示。那是一个错误吗?
我有一些数据,比如x、y和z。全部都是1D数组。我已经制作了一个散点图,其中 z 的颜色为; import matplotlib.pyplot as plt plt.scatter(x,y,c=z
我尝试使用 matplotlib 限制颜色条的范围。旧的行为是,可以使用 plot 函数的 vmin 和 vmax 关键字来缩放颜色条。这会影响颜色本身和颜色条的标签。 现在的行为似乎是,只有颜色被缩
这个问题在这里已经有了答案: How do I use colorbar with hist2d in matplotlib.pyplot? (2 个答案) 关闭 5 年前。 我正在尝试用 Pyth
如何在 matplotlib 中准确指定颜色条标签?我经常需要创建非常具体的色标,但颜色条标签显示效果很差,您无法分辨色标是什么。我想手动定义颜色条刻度线旁边的文本,或者至少让它们以科学记数法显示。
在下面的简单代码中: dat = np.linspace(0.1,0.9,4)*np.ones((4,1)) fig, ax = plt.subplots() cax = ax.imshow(dat,
我正在使用下面的代码 d3 = vals; n = datesmonth; figure plot(n,d3); colormap(jet(12)); hold on plot(n, d3,'b-')
考虑以下示例: [ X, Y, Z ] = peaks( 30 ); figure( 100 ); surfc( X, Y, Z ); zlabel( 'Absolute Values' ); col
我有一个 pcolormesh 图,其中通过将 vmin 和 vmax 严格设置在绘制值的范围内来裁剪颜色图.有没有办法让关联的 colorbar 从底部和顶部分离一个 block ,以表示相关颜色超
绘制颜色条时,顶部标签(我想这将称为偏移量)居中错误。这在过去不会发生,我有旧代码的示例,它位于颜色条上方的中心,但我不知道发生了什么变化。 例子: import numpy as np import
我想更改下图中颜色栏中的刻度定位器和标签。 import pandas as pd import matplotlib.pyplot as plt from matplotlib import dat
我正在尝试在我的 matpllotlib contourf 图上自定义颜色条。虽然我能够使用科学记数法,但我正在尝试更改记数法的基础 - 本质上是让我的刻度在 (-100,100) 范围内,而不是 (
直到最近,我一直在使用 Mathematica 绘制绘图。虽然这真的很痛苦,而且一切都必须手动完成,但结果非常接近我想要的。一个例子如下: 我非常喜欢颜色栏背景中的灰色圆角矩形。虽然一切都必须在 Ma
我正在尝试更新 Bokeh 中的 ColorBar,但标题和颜色本身都不会更新。这是一个最小的功能示例(Python 3.6.9、Bokeh 2.3.0、Tornado 6.1): from boke
我是一名优秀的程序员,十分优秀!