1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| def auto_lim(max_value, min_value, is_deviation=False): """ 根据最大值和最小值,自动生成一个较为整齐的 xlim 或 ylim,lim 包括 x 或 y 轴的最大值,x 或 y 轴的最小值, 以及 x 或 y 轴的刻度 locator :param max_value: x 或 y 数据的最大值 :param min_value: x 或 y 数据的最小值 :param is_deviation 是否允许误差,默认为 False :return: 返回一个 auto lim list [lim_min, lim_max, locator] """ split_number = 4 magic_array = [2, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100] temp_gap = (max_value - min_value) / split_number multiple = 10 ** (math.floor(math.log10(temp_gap) - 1)) estep = next((val * multiple for val in magic_array if val > temp_gap / multiple), None) maxi, mini = count_degree(estep, max_value, min_value)
if not is_deviation: while True: temp_split_number = round((maxi - mini) / estep) if (maxi == 0 or mini - min_value <= maxi - max_value) and temp_split_number < split_number: mini -= estep else: maxi += estep if temp_split_number == split_number: break if temp_split_number > split_number: magic_idx = next((i for i, val in enumerate(magic_array) if val * multiple == estep), None) if magic_idx is not None and magic_idx < len(magic_array) - 1: estep = magic_array[magic_idx + 1] * multiple maxi, mini = count_degree(estep, max_value, min_value) else: break else: magic_idx = next((i for i, val in enumerate(magic_array) if val * multiple == estep), None) if magic_idx is not None and magic_idx > 0: estep = magic_array[magic_idx - 1] * multiple maxi, mini = count_degree(estep, max_value, min_value) else: break
interval = (maxi - mini) / split_number
lim = [mini, maxi, interval]
return lim
def count_degree(estep, max_value, min_value, symmetrical=False): """ 求出期望的最大刻度和最小刻度,为 estep 的整数倍 :param estep: 最佳期望的间隔 :param max_value: 数据的最大值 :param min_value: 数据的最小值 :param symmetrical: 是否开启正负刻度 :return: """
maxi = int(max_value / estep + 1) * estep mini = int(min_value / estep - 1) * estep
if max_value == 0: maxi = 0 if min_value == 0: mini = 0 if symmetrical and maxi * mini <= 0: tm = max(abs(maxi), abs(mini)) maxi = tm mini = -tm
return maxi, min
|