Anonymous
Как отобразить цветовую панель с помощью специальной нормализованной цветовой карты?
Сообщение
Anonymous » 22 ноя 2024, 09:27
Я построил разброс с нормализацией пользовательской цветовой карты. Скаттер появляется правильно. После этого я добавил цветовую полосу и хочу, чтобы цвета на этой цветовой панели были такими же, как на разбросе. Вот мой код:
Код: Выделить всё
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
class SigmoidNormalize(mpl.colors.Normalize):
def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False):
if vcenter is None:
self.vcenter = (vmax+vmin) / 2
self.vcenter = vcenter
super().__init__(vmin, vmax, clip)
def __call__(self, value, clip=None):
threshold = self.vcenter
value = (value - threshold) * 10
norm = 1 / (1+np.exp(-value))
return norm
def inverse(self, value):
threshold = self.vcenter
nonzero = np.logical_and(value != 0, value != 1)
norm = np.empty_like(value)
norm[nonzero] = -np.log(1/value[nonzero] - 1)
norm[nonzero] = norm[nonzero] / 10 + threshold
norm[value==0] = self.vmin
norm[value==1] = self.vmax
return norm
class SigmoidNormalize_r(SigmoidNormalize):
def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False):
super().__init__(vmin, vmax, vcenter, clip)
def __call__(self, value, clip=None):
return super().inverse(value)
def inverse(self, value):
return super().__call__(value)
def plot_predictions(predictions,
actual,
threshold=0.5,
threshold_gap=0.1):
fig = plt.figure()
ax = fig.add_subplot()
normals = np.where(np.logical_and(actual == 0, predictions < threshold))[0]
abnormals = np.where(np.logical_and(actual == 1, predictions >= threshold))[0]
normals_wrong = np.where(np.logical_and(actual == 0, predictions > threshold))[0]
abnormals_wrong = np.where(np.logical_and(actual == 1, predictions
Подробнее здесь: [url]https://stackoverflow.com/questions/79213866/how-to-display-a-colorbar-with-custom-normalized-colormap[/url]
1732256876
Anonymous
Я построил разброс с нормализацией пользовательской цветовой карты. Скаттер появляется правильно. После этого я добавил цветовую полосу и хочу, чтобы цвета на этой цветовой панели были такими же, как на разбросе. Вот мой код: [code]import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl class SigmoidNormalize(mpl.colors.Normalize): def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False): if vcenter is None: self.vcenter = (vmax+vmin) / 2 self.vcenter = vcenter super().__init__(vmin, vmax, clip) def __call__(self, value, clip=None): threshold = self.vcenter value = (value - threshold) * 10 norm = 1 / (1+np.exp(-value)) return norm def inverse(self, value): threshold = self.vcenter nonzero = np.logical_and(value != 0, value != 1) norm = np.empty_like(value) norm[nonzero] = -np.log(1/value[nonzero] - 1) norm[nonzero] = norm[nonzero] / 10 + threshold norm[value==0] = self.vmin norm[value==1] = self.vmax return norm class SigmoidNormalize_r(SigmoidNormalize): def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False): super().__init__(vmin, vmax, vcenter, clip) def __call__(self, value, clip=None): return super().inverse(value) def inverse(self, value): return super().__call__(value) def plot_predictions(predictions, actual, threshold=0.5, threshold_gap=0.1): fig = plt.figure() ax = fig.add_subplot() normals = np.where(np.logical_and(actual == 0, predictions < threshold))[0] abnormals = np.where(np.logical_and(actual == 1, predictions >= threshold))[0] normals_wrong = np.where(np.logical_and(actual == 0, predictions > threshold))[0] abnormals_wrong = np.where(np.logical_and(actual == 1, predictions Подробнее здесь: [url]https://stackoverflow.com/questions/79213866/how-to-display-a-colorbar-with-custom-normalized-colormap[/url]