matplotlib 使用mpl_scatter_density调整窗口大小的鼠标坐标

gijlo24d  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(107)

我在Matplolib中使用自定义工具栏。为了让它工作,我必须设置plot.rcParams['toolbar'] = 'toolmanager',这会增加鼠标坐标字体大小。
问题与mpl_scatter_density结合出现。这些图为密度值向鼠标坐标框架添加了额外的一行。坐标如下所示:

x=2.3535 y=4.565
[2.56] <--- extra row with density value

由于坐标仅在鼠标位于绘图限制范围内时才出现,因此这一额外行增加了工具栏高度,并导致在将鼠标移入和移出绘图时整个窗口不规则地调整大小,这非常烦人。
使用的代码:(但没有自定义工具栏,这是太大,无法粘贴在这里):

import matplotlib.pyplot as plot
import mpl_scatter_density # adds projection='scatter_density'
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

# "Viridis-like" colormap with white background
white_viridis = LinearSegmentedColormap.from_list('white_viridis', [
    (0, '#ffffff'),
    (1e-20, '#440053'),
    (0.2, '#404388'),
    (0.4, '#2a788e'),
    (0.6, '#21a784'),
    (0.8, '#78d151'),
    (1, '#fde624'),
], N=256)

# Fake data for testing
x = np.random.normal(size=100000)
y = x * 3 + np.random.normal(size=100000)

fig = plot.figure()
ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
density = ax.scatter_density(x, y, cmap=white_viridis)
fig.colorbar(density, label='Number of points per pixel')

plot.show()

我解决这个问题的想法是使用plot.rcParams['font.size']设置一个较小的字体大小(在VScode调试器中手动设置时效果很好,但在正常执行脚本时由于某种原因不起作用!)或完全删除密度值。
但是,ax.format_coord只修改了鼠标坐标的第一行的格式,我似乎找不到如何修改密度值的格式。

r7s23pms

r7s23pms1#

format_coord仅格式化x和y表示。要格式化数据表示,请使用format_cursor_data

import matplotlib.pyplot as plot
import mpl_scatter_density  # adds projection='scatter_density'
import numpy as np

# Fake data for testing
x = np.random.normal(size=100000)
y = x * 3 + np.random.normal(size=100000)

fig = plot.figure()
ax = fig.add_subplot(1, 1, 1, projection="scatter_density")
density = ax.scatter_density(x, y)

# replace existing data representation with empty one
density.format_cursor_data = lambda _: ""

fig.colorbar(density, label="Number of points per pixel")

plot.show()

相关问题