使用argparse绘制直方图

时间:2017-11-08 18:19:28

标签: python python-3.x numpy matplotlib histogram

我是使用argparse的新手,在这种情况下我必须使用它,因为代码将通过终端运行。我希望用户能够在终端中输入bin大小,然后程序将绘制直方图。如果我使用输入语句,代码工作正常但是我在使用argparse时遇到问题。这是目前的代码。

from matplotlib import pyplot as plt
import numpy as np
import argparse
import sys

fh = open('dummy2.pepmasses')
values = []


for line in fh:
    data = line.split()
    protname, pep, mz, z, p, sequence = data
    mz = float(mz)
    #print(mz)
    values.append(mz)

parser = argparse.ArgumentParser()
parser.add_argument('-s', '--Bin_size', type=int, metavar='', required=True, help='Size of bin, multiple of 100 between 100-1000')

args = parser.parse_args()

def Histogram(Bin_size):
    if Bin_size ==100:
        bins = [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400,                        3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100]
    elif Bin_size ==200:
        bins = [0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400, 2600, 2800, 3000, 3200, 3400, 3600, 3800, 4000, 4200, 4400, 4600, 4800, 5000, 5200, 5400, 5600, 5800, 6000, 6200]
    elif Bin_size ==300:
        bins = [0, 300, 600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000, 3300, 3600, 3900, 4200, 4500, 4800, 5100, 5400, 5700, 6000, 6300]
    elif Bin_size ==400:
        bins= [0, 400, 800, 1200, 1600, 2000, 2400, 2800, 3200, 3600, 4000, 4400, 4800, 5200, 5600, 6000, 6400]
    elif Bin_size ==500:
        bins = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500]
    elif Bin_size ==600:
        bins = [0, 600, 1200, 1800, 2400, 3000, 3600, 4200, 4800, 5400, 6000, 6600]
    elif Bin_size ==700:
        bins = [0, 700, 1400, 2100, 2800, 3500, 4100, 4900, 5600, 6300]
    elif Bin_size ==800:
        bins = [0, 800, 1600, 2400, 3200, 4000, 4800, 5600, 6400]
    elif Bin_size ==900:
        bins = [0, 900, 1800, 2700, 3600, 4500, 5400, 6300]
    elif Bin_size ==1000:
        bins = [0, 1000, 2000, 3000, 4000, 5000, 6000, 7000]
    else:
        print("Invalid bin size, please enter a multiple of 100 between 100-1000")
    if Bin_size ==100 or Bin_size==200 or Bin_size==300 or Bin_size==400 or Bin_size==500 or Bin_size==600 or Bin_size==700 or Bin_size==800 or Bin_size==900 or Bin_size==1000:
        plt.hist(values, bins, histtype='bar', rwidth=0.8)
        plt.xlabel('m/z value')
        plt.ylabel('Number of proteins')
        plt.title('Histogram of mz')
        plt.legend('Bins')
        plt.show()

if __name__ == '__main__':
    Histogram(args.Bin_size)

这会返回以下错误:

Traceback (most recent call last):
  File "project_test.py", line 54, in <module>
    Histogram(args.Bin_size)
  File "project_test.py", line 46, in Histogram
    plt.hist(values, bins, histtype='bar', rwidth=0.8)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 3066, in hist
    ax = gca()
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 950, in gca
    return gcf().gca(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 586, in gcf
    return figure()
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

我甚至无法理解这个错误的含义。任何帮助将不胜感激。

0 个答案:

没有答案