颜色条轴格式不正确

2024-06-20 15:04:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我的色条有问题。它似乎在颜色条下面输出了一大堆重叠的记号,我不知道为什么。我也不知道如何让色条刻度以科学的符号表示,而不是它们现在是怎样的。这是我的密码

from matplotlib.colors import LogNorm
from matplotlib.ticker import LogFormatter
import matplotlib.colors as mc
import matplotlib.cm as cm
from   mpl_toolkits.axes_grid1 import AxesGrid
import matplotlib.pyplot as plt
import numpy as np

from plotbin.sauron_colormap import register_sauron_colormap    
register_sauron_colormap()


fig = plt.figure(figsize=(15,5))    #This determines the size of the figure
grid = AxesGrid(fig, 111, nrows_ncols=(1, 1), axes_pad=0.0, share_all=True, \
            label_mode="L", cbar_location="right", cbar_mode="each", cbar_size='6%')

A = np.random.rand(58,58)
image = grid[0].imshow(np.rot90(A), cmap='sauron', interpolation=None, norm = LogNorm())

grid.cbar_axes[0].colorbar(image)

以及输出

is this

我该如何解决这个问题


Tags: fromimportmatplotlibasnpcmgridcolors
1条回答
网友
1楼 · 发布于 2024-06-20 15:04:21

这看起来像是LogNorm()由于未知原因与AxesGrid一起引入的错误。但是,由于您可能也看到了弃用警告:

MatplotlibDeprecationWarning: Since 3.2, mpl_toolkits's own colorbar implementation is deprecated; it will be removed two minor releases later. Set the 'mpl_toolkits.legacy_colorbar' rcParam to False to use Matplotlib's default colorbar implementation and suppress this deprecation warning. grid.cbar_axes[0].colorbar(image)

您还可以更改将颜色条打印到指定空间的方法

from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm
from   mpl_toolkits.axes_grid1 import AxesGrid
import numpy as np


fig = plt.figure(figsize=(15,5))    #This determines the size of the figure
grid = AxesGrid(fig, 111, nrows_ncols=(1, 1), axes_pad=0.0, share_all=True, \
            label_mode="L", cbar_location="right", cbar_mode="each", cbar_size='6%')

np.random.seed(123)
A = np.random.rand(58,58)
image = grid[0].imshow(np.rot90(A), cmap='hot', interpolation=None, norm = LogNorm())

plt.colorbar(image, cax=grid.cbar_axes[0])

plt.show()

样本输出: enter image description here

相关问题 更多 >