来自终端输入和输出的命令将保存分配目录

时间:2017-01-24 02:20:53

标签: python-3.x

在命令行中,我想给python hist.py -n 1000 -o / dir,输出将是png在分配目录中。任何人都可以帮忙吗?

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import argparse
import os, sys, errno


def plotData(outputDir):
        outFilename = "hist.pdf"
        outFilepath = os.path.join(outputDir)

        parser = argparse.ArgumentParser()
        parser.add_argument("-n","--number", help="display a square of   a given number",type=int)
        parser.add_argument('-o', '--outputDir', required=True,
                    help='The directory to which plot files should be saved')

        args = parser.parse_args()
        num=(args.number)
        outFilepath=(args.outputDir)
# example data
        mu = 100  # mean of distribution
        sigma = 15  # standard deviation of distribution
#num=input()
        x = mu + sigma * np.random.randn(int(num))

        num_bins = 50
        # the histogram of the data
        n, bins, patches = plt.hist(x, num_bins, normed=1,  facecolor='green', alpha=0.5)
        # add a 'best fit' line
        y = mlab.normpdf(bins, mu, sigma)


        plt.plot(bins, y, 'r--')
        plt.xlabel('Smarts')
        plt.ylabel('Probability')
        plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')

  # Tweak spacing to prevent clipping of ylabel
        plt.subplots_adjust(left=0.15)
        plt.savefig (outFilepath,outFilename)  
        plt.show() 
        plt.close()

我可以从终端命令行给出随机变量而不是目录。

我使用过args.outputDir,但它对我不起作用。 我是学习者

0 个答案:

没有答案