利用matplotlib为x轴设置不同刻度

2024-04-28 21:59:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为不同的值列表(包含在不同的文件中)绘制CDF。 代码如下:

import argparse, os, math
import fileLibrary
import numpy as np
import matplotlib.pyplot as plt

parser = argparse.ArgumentParser()
parser.add_argument('-d', type=str, dest='intervalsDir', help='the directory which contains all the intervals for the CDF')
arguments = parser.parse_args()
intervalsDir = arguments.intervalsDir

def cdf(dataList, groupName):
    dataLen = len(dataList)
    dataSet = sorted(set(dataList))
    bins = np.append(dataSet, dataSet[-1]+1)
    counts, binEdges = np.histogram(dataList, bins=bins, density=False)
    counts = counts.astype(float) / dataLen
    cdf = np.cumsum(counts)
    plt.plot(binEdges[0:-1], cdf, linestyle='--',  color='b')
    plt.ylim((0,1))
    plt.ylabel("CDF")
    plt.xlabel(groupName)
    plt.grid(True)
    plt.show()

for fileName in os.listdir(intervalsDir):
    parsedFileName = fileName.split(".")
    xLabel = parsedFileName[0]
    filePath = intervalsDir + "/" + fileName
    dataList = fileLibrary.createFileList(filePath)
    myDataList = []
    for d in dataList:
        x = int(d)

我得到以下结果(对于第一个文件,其他文件类似): Result

我从中获取CDF的值列表由0到1000之间的大多数值组成。然后我有很少的较大值,我只有2个接近最后一个25万的数字。每个列表包含超过60000个值。 我希望我的x轴有不同的刻度,主要显示较小的值。 我是python和matplotlib新手,所以我不知道如何正确地做到这一点。 提前感谢您的帮助


Tags: 文件theimportparser列表fornpplt