数组中不固定数据按大小按照刻度分布在x轴

假设有一维数组,满足如下条件,需要按每个数字大小(包括小数数字),重新分布到新的一维数组中,以整数为单位,对于数组中的每一个数字,如果不是整数,将其分布在新数组中的位于此数字的向下取整的数字,和向上取整的数字之间

新的数组的数据可用于chart图的按照时间显示的x轴上

the number in array must be:

    # 1. sorted by ascending order

    # 2. each number are not equal each other

    # 3. each number >= 0

    # max length = the last number in tenorsInyears + the counter of non-integer in tenorsInyears??

 

  • python版
# -*- coding: utf-8 -*-
import math
"""

"""


def main():
    print("we are in %s", __name__)

    ## real number
    tenorsInyears = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
    # tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]

    ## Many numbers less than 1 at the beginner
    # tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
    # tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]

    ## Many decimal numbers at the center
    # tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
    # tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 40]

    ## Many decimal numbers at the last
    tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9]
    tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11]
    tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11.1]
    tenorsInyears = [1, 1.5, 2, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11, 11.1]
    tenorsInyears = [0.1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]
    tenorsInyears = [1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]

    ## Many group decimal numbers, Which locate between the two consecutive numbers, Such as 4~5, 10~11
    tenorsInyears = [1, 1.5, 2.0, 2.3, 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 6, 7, 
                     10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]

    ## the beginner number is > 1
    tenorsInyears = [1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
    tenorsInyears = [3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14]
    tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14, 99]
    tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 7, 9, 11, 13, 15, 17]
    tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.81, 5, 7, 10, 14, 19, 25, 32, 40]
    tenorsInyears = [4.1, 4.2, 4.3, 7, 9.5, 11.6, 14.7, 17.8, 21.9, 23, 23.9, 26, 28, 30, 33, 37, 42, 48]
    tenorsInyears = [4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 8]

    ## the last number is not an integer
    tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1]

    ## the beginner number and the last number all are not integer
    tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5,
                     4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1, 8.2, 8.3, 8.4, 8.5]

    ## all number are not integer
    tenorsInyears = [0.1, 2.1, 3.0, 6.2, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10.1, 40.1]
    tenorsInyears = [0.1, 2.1, 3.1, 4.2, 5.2, 6.3, 7.4, 8.5, 9.6, 10.7, 11.8, 19.9, 20.1, 40.1]

    # the number in array must be:
    # 1. sorted by ascending order
    # 2. each number are not equal each other
    # 3. each number >= 0
    # max length = the last number in tenorsInyears + the counter of non-integer in tenorsInyears??

    value2 = []
    value3 = []
    for i in range(1, 100):
        value2.append(str(i) + "Y")
        value3.append(str(i))

    counterOfNonIntegerArr = getCounterOfNonIntegerArr(tenorsInyears)
    maxLength = math.floor(tenorsInyears[-1]) + counterOfNonIntegerArr

    value4 = [""] * maxLength
    value5 = [""] * maxLength

    print(tenorsInyears)

    # if counterOfNonIntegerArr == 0:
    #     # all element are integer number
    #     for i in range(0, len(tenorsInyears)):
    #         item = tenorsInyears[i]
    #         index = item - 1
    #         value4[index] = value2[i]
    #         value5[index] = item
    # else:
    index = 1
    for i in range(0, len(tenorsInyears)):

        item = tenorsInyears[i]
        itemCeil = math.ceil(item)
        if i == 0:
            index = itemCeil - 1
            value4[index] = value2[i]
            value5[index] = item
        else:
            lastNumber = tenorsInyears[i - 1]
            difference = itemCeil - math.ceil(lastNumber)
            if difference == 0:
                index = index + 1
                value4[index] = value2[i]
                value5[index] = item
            elif difference == 1:
                if isNotInteger(lastNumber):
                    index = index + 2
                    value4[index] = value2[i]
                    value5[index] = item
                else:
                    index = index + 1
                    value4[index] = value2[i]
                    value5[index] = item
            else:
                if isNotInteger(lastNumber):
                    index = index + difference + 1
                    value4[index] = value2[i]
                    value5[index] = item
                else:
                    index = index + difference
                    value4[index] = value2[i]
                    value5[index] = item

    print(value5)

def getCounterOfNonIntegerArr(arr):
    counterNonInteger = 0
    for i in range(0, len(arr)):
        if isNotInteger(arr[i]):
            counterNonInteger += 1
    return counterNonInteger

## the number is not an integer
## Such as 2.1, 3.1, etc are not integer
##     and 2.0, 3.0, etc are let it as be integer here
def isNotInteger(number):
    return not isinstance(number, int) and math.ceil(number) != number

if __name__ == '__main__':
    main()

一些测试数据

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', '', '', '', '', '', '', '', '', 20, '', '', '', '', '', '', '', '', '', 30, '', '', '', '', '', '', '', '', '', 40]

[1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
[1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', '', '', '', '', '', '', '', '', 20, '', '', '', '', '', '', '', '', '', 30, '', '', '', '', '', '', '', '', '', 40]


[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', 12, '', 14]

[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, '', '', '', '', '', '', '', '', '', 20, '', '', '', '', '', '', '', '', '', 30, '', '', '', '', '', '', '', '', '', 40]


[3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14]
['', '', 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, '', '', 7, 8, 9, 10, '', 12, '', 14]

[1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
['', 1.5, 2, 3, 4, 5, 6, 7, '', '', 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, '', 12, '', 14]

[1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
[1, 1.5, 2, 3, 4, 5, 6, 7, '', '', 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, '', 12, '', 14]
  • Javascript版


function main() {
     real number
    let tenorsInyears = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
    // tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]

     Many numbers less than 1 at the beginner
    // tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14]
    // tenorsInyears = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40]

     Many decimal numbers at the center
    // tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
    // tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 40]

     Many decimal numbers at the last
    // tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9]
    // tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11]
    // tenorsInyears = [1, 1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11.1]
    // tenorsInyears = [1, 1.5, 2, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11, 11.1]
    // tenorsInyears = [0.1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]
    // tenorsInyears = [1, 2.1, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10]

     Many group decimal numbers, Which locate between the two consecutive numbers, Such as 4~5, 10~11
    // tenorsInyears = [1, 1.5, 2.0, 2.3, 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 6, 7, 
    //                  10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]

     the beginner number is > 1
    // tenorsInyears = [1.5, 2, 3, 4, 5, 6, 7, 10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 12, 14]
    // tenorsInyears = [3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14]
    // tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7, 8, 9, 10, 12, 14, 99]
    // tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 5, 7, 9, 11, 13, 15, 17]
    // tenorsInyears = [4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.81, 5, 7, 10, 14, 19, 25, 32, 40]
    // tenorsInyears = [4.1, 4.2, 4.3, 7, 9.5, 11.6, 14.7, 17.8, 21.9, 23, 23.9, 26, 28, 30, 33, 37, 42, 48]
    // tenorsInyears = [4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 8]

     the last number is not an integer
    // tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1]

     the beginner number and the last number all are not integer
    // tenorsInyears = [0.1, 0.2, 1, 2.1, 2.9, 3, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5,
    //                  4.6, 4.7, 4.8, 4.9, 5, 7.1, 7.2, 8, 8.1, 8.2, 8.3, 8.4, 8.5]

     all number are not integer
    // tenorsInyears = [0.1, 2.1, 3.0, 6.2, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10.1, 40.1]
    // tenorsInyears = [0.1, 2.1, 3.1, 4.2, 5.2, 6.3, 7.4, 8.5, 9.6, 10.7, 11.8, 19.9, 20.1, 40.1]

    // the number in array must be:
    // 1. sorted by ascending order
    // 2. each number are not equal each other
    // 3. each number >= 0
    // max length = the last number in tenorsInyears + the counter of non-integer in tenorsInyears??


    let value2 = [];
    let value3 = [];
    for (let i = 1; i < 100; i++) {
        value2.push(i + "Y");
        value3.push(i);
    }

    const counterOfNonIntegerArr = getCounterOfNonIntegerArr(tenorsInyears);
    const maxLength = Math.floor(tenorsInyears[tenorsInyears.length - 1]) + counterOfNonIntegerArr;

    value4 = []
    value5 = []
    for (let i = 0; i < maxLength; i++) {
        value4.push('');
        value5.push('');
    }

    console.log(tenorsInyears);

    // if counterOfNonIntegerArr == 0:
    //     // all element are integer number
    //     for i in range(0, len(tenorsInyears)):
    //         item = tenorsInyears[i]
    //         index = item - 1
    //         value4[index] = value2[i]
    //         value5[index] = item
    // else:
    let index = 1;
    for (let i = 0; i < tenorsInyears.length; i++) {

        let item = tenorsInyears[i];
        let itemCeil = Math.ceil(item);
        if (i == 0) {
            index = itemCeil - 1;
            value4[index] = value2[i];
            value5[index] = item;
        } else {
            let lastNumber = tenorsInyears[i - 1];
            difference = itemCeil - Math.ceil(lastNumber);
            if (difference === 0) {
                index = index + 1;
                value4[index] = value2[i];
                value5[index] = item;
            } else if (difference === 1) {
                if (isNotInteger(lastNumber)) {
                    index = index + 2;
                    value4[index] = value2[i];
                    value5[index] = item;
                } else {
                    index = index + 1;
                    value4[index] = value2[i];
                    value5[index] = item;
                }
            } else {
                if (isNotInteger(lastNumber)) {
                    index = index + difference + 1;
                    value4[index] = value2[i];
                    value5[index] = item;
                } else {
                    index = index + difference;
                    value4[index] = value2[i];
                    value5[index] = item;
                }
            }
        }
    }
    console.log(value5);
}

function getCounterOfNonIntegerArr(arr) {
    counterNonInteger = 0;
    for (let i = 0; i < arr.length; i++) {
        if (isNotInteger(arr[i])) {
            counterNonInteger += 1;
        }
    }
    return counterNonInteger;
}

 the number is not an integer
 Such as 2.1, 3.1, etc are not integer
     and 2.0, 3.0, etc are let it as be integer here
function isNotInteger(number) {
    // return not isinstance(number, int) and Math.ceil(number) != number
    return Math.round(number) !== number;
}

main();

 

直方图是统计学中一种用条形的长度表示数据分布情况的图表。在直方图中,x通常用来表示数据的区间或类别,而y表示对应区间或类别的频数或频率。要使x下标和宽度对齐,需要确保每个条形的宽度代表一个固定的区间宽度,而x刻度(下标)则需要与这些宽度相对应。 在很多绘图软件或编程库中,例如Python的matplotlib,可以通过设置x刻度位置和条形宽度来实现这一点。具体步骤如下: 1. 确定每个条形的宽度,这个宽度需要反映数据的区间间隔。例如,如果你的数据是按每10的间隔划分的,那么每个条形的宽度应该是10。 2. 使用绘图函数时,通过参数明确指定每个条形的宽度。比如在matplotlib中,可以使用`bar()`函数的`width`参数来设置。 3. 设置x刻度位置,使其与条形宽度的中点对齐。这通常需要计算每个条形中心的位置,并使用`xticks()`函数来设置。 4. 确保x刻度标签显示的是对应的区间,而不是具体的数值,除非区间本身就是单一点(例如,年龄区间0-10, 11-20等)。 下面是一个使用Python和matplotlib库创建直方图并使x下标与宽度对齐的简单示例代码: ```python import matplotlib.pyplot as plt import numpy as np # 假设有一组数据 data = np.random.normal(0, 1, 1000) # 设置区间数量,即x刻度的数量 bins = np.linspace(-3, 3, 13) # 从-3到3,分为12个区间 # 绘制直方图 plt.hist(data, bins=bins, edgecolor='black') # 计算条形的宽度和位置 bar_width = bins[1] - bins[0] # 假设区间是均匀的 bar_centers = (bins[:-1] + bins[1:]) / 2 # 计算每个条形中心的位置 # 设置x刻度和标签 plt.xticks(bar_centers, [f'{center-bar_width/2:.1f}-{center+bar_width/2:.1f}' for center in bar_centers]) plt.show() ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值