1

I am trying to make a pie chart in python and i keep getting this traceback. Here is my code

#!/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')

This is how i am running it

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

And the traceback i'm getting is

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
upendra
  • 2,141
  • 9
  • 39
  • 64

1 Answers1

2

I ran your program with a simplified all list.

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()

And I could get the following plot. So I think your python program is fine. But I checked DISPLAY env variable and it is

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

Perhaps you may want to check your DISPLAY and set it properly.

pie chart

Hun
  • 3,707
  • 2
  • 15
  • 15
  • Great. How do i set the $DISPLAY variable then? When i typed echo $DISPLAY in ubuntu, got nothing. – upendra Apr 11 '16 at 20:13
  • I use OSX so my situation is different from yours. But basically it is the environment variable used to tunnel display data to a remote screen. Do some google search for your specific system. – Hun Apr 11 '16 at 20:46
  • I found the answer in here - http://stackoverflow.com/questions/2801882/generating-a-png-with-matplotlib-when-display-is-undefined – upendra Apr 12 '16 at 02:45
  • Yes, that's right... – upendra Apr 12 '16 at 02:52