/ 猿问

# 移位的颜色栏matplotlib

2019-10-30 11:04:02

plt.contourf(x, y, z, label = 'blah', cm = matplotlib.cm.RdBu)

## 3 回答

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.colors import Normalize

class MidpointNormalize(Normalize):

def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):

self.midpoint = midpoint

Normalize.__init__(self, vmin, vmax, clip)

def __call__(self, value, clip=None):

# I'm ignoring masked values and all kinds of edge cases to make a

# simple example...

x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]

data = np.random.random((10,10))

data = 10 * (data - 0.8)

fig, ax = plt.subplots()

norm = MidpointNormalize(midpoint=0)

im = ax.imshow(data, norm=norm, cmap=plt.cm.seismic, interpolation='none')

fig.colorbar(im)

plt.show()

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.colors import from_levels_and_colors

data = np.random.random((10,10))

data = 10 * (data - 0.8)

num_levels = 20

vmin, vmax = data.min(), data.max()

midpoint = 0

levels = np.linspace(vmin, vmax, num_levels)

midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)

vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])

colors = plt.cm.seismic(vals)

cmap, norm = from_levels_and_colors(levels, colors)

fig, ax = plt.subplots()

im = ax.imshow(data, cmap=cmap, norm=norm, interpolation='none')

fig.colorbar(im)

plt.show()

• 3 回答
• 0 关注
• 226 浏览

0/150