python饼图回溯

时间:2016-04-11 19:05:44

标签: python matplotlib pie-chart

我正在尝试在python中创建一个饼图,并且我继续得到这个回溯。这是我的代码

#!/usr/bin/python

import sys
import re
import matplotlib.pyplot as plt

file1 = open(sys.argv[1])
file2 = open(sys.argv[2])
file3 = open(sys.argv[3])

all = []

count_1 = 0
count_2 = 0
count_3 = 0

for line in file1:
    line = line.strip()
    if line.startswith(">"):
        count_1 += 1

all.append(count_1)

for line in file2:
        line = line.strip()
        if line.startswith(">"):
                if re.search("CAGE_PLUS", line):
            count_2 += 1

all.append(count_2)

for line in file3:
        line = line.strip()
        if line.startswith(">"):
        if re.search("known", line):
                    count_3 += 1


all.append(count_3)


labels = ["All lincRNA", "CAGE lincRNA", "Known lincRNA"]
sizes = all
colors = ['yellowgreen', 'mediumpurple', 'lightskyblue'] 

plt.pie(sizes,              # data
        labels=labels,      # slice labels
        colors=colors,      # array of colours
        autopct='%1.1f%%'  # print the values inside the wedges
        )
plt.axis('equal')

plt.savefig('lincRNA_piechart')

这就是我的运作方式

python /evolinc_docker/lincRNA_fig.py All.lincRNAs.fa lincRNAs.with.CAGE.support.annotated.fa lincRNAs.overlapping.known.lincs.fa

我得到的追溯是

Traceback (most recent call last):
  File "/evolinc_docker/lincRNA_fig.py", line 50, in <module>
    plt.pie(sizes, labels=labels, colors=colors)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2959, in pie
    ax = gca()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 803, in gca
    ax =  gcf().gca(**kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 450, in gcf
    return figure()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

1 个答案:

答案 0 :(得分:2)

我用简化的所有列表运行你的程序。

import matplotlib.pyplot as plt
labels = ["All lincRNA", "CAGE lincRNA", "Known lincRNA"]   
sizes = [10, 20, 30] # my arbitrary list just for testing
colors = ['yellowgreen', 'mediumpurple', 'lightskyblue'] 
plt.pie(sizes, labels=labels,colors=colors, autopct='%1.1f%%')
plt.axis('equal')
plt.show()

我可以得到以下情节。所以我认为你的python程序很好。但我检查了DISPLAY env变量,它是

$ echo $DISPLAY
/private/tmp/com.apple.launchd.VoooVvQSYh/org.macosforge.xquartz:0

也许您可能需要检查您的DISPLAY并正确设置。

pie chart