1

我有一个有点像这样的情节:

两条线(红色和蓝色)之间的差异在我的实际数据(ROC 曲线)中最为重要,例如网格单元0.2<x<0.4, 0.8<y<1。现在,我可以裁剪那个网格单元,但假设我宁愿双曲线缩放 x 轴和 y 轴——其中 y 轴双曲线的峰值约为 0.9,x 轴的峰值约为0.3 - 使得 2D 空间被拉伸以用于感兴趣的网格单元并在其他地方被压缩(并保留轴刻度数的含义)。如何做到这一点?我尝试的开始如下。如何修改我的代码以实现我描述的轴缩放?

from   matplotlib import gridspec
from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms
from matplotlib.ticker import FormatStrFormatter
from   matplotlib.ticker import NullFormatter, NullLocator, MultipleLocator
import math
import matplotlib
import matplotlib.patches as mpatches
import matplotlib.pylab as plt
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

import seaborn as sns
sns.set_palette('husl')
sns.set()
plt.rcParams["figure.figsize"] = [5, 5]
x = np.arange(0, 1, step=0.01)
y1 = 1-1/np.exp(10*x)
y2 = 1-1.1/np.exp(10*x)
plt.scatter(x, y1, s=1, facecolor='red')
plt.scatter(x, y2, s=1, facecolor='blue')
plt.show();

class CustomScale(mscale.ScaleBase):
    name = 'custom'
    def __init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        self.thresh = None #thresh
        self.name = 'custom'
    def get_transform(self):
        return self.CustomTransform(self.thresh)
    def set_default_locators_and_formatters(self, axis):
        pass
    class CustomTransform(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True
        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh
        def transform_non_affine(self, a):
            #return np.log(1+a)
            return np.exp(a)-1
            #return 1+(1/2)*a
mscale.register_scale(CustomScale)

plt.scatter(x, y1, s=1, facecolor='red')
plt.scatter(x, y2, s=1, facecolor='blue')
plt.xscale('custom')
plt.show();
4

1 回答 1

2

您可以使用FuncScale(注册为'function')来实现此目的。

f = lambda a: np.exp(a) - 1
g = lambda b: np.log(b + 1)
plt.xscale('function', functions=(f, g))

对于双曲线缩放,您可以同时使用lambda x: 1 / x这两个函数。

请参阅 scales 文档中的示例:https ://matplotlib.org/3.3.4/gallery/scales/scales.html

于 2021-03-08T17:05:45.363 回答