I've been trying out certain mpld3 plots and have come up with a doubt. I know mpld3 plots can be saved locally using savefig()
function. My question is whether it would be possible to provide a download option so as to download the plot as an image in the browser itself.
Attached herewith are the codes and screenshots:
ps = PorterStemmer()
stop_words = set(stopwords.words('english'))
file1 = open("test.txt")
line = file1.read()
words = line.split()
appendFile = open('outputFile.txt','w')
for r in words:
if not r in stop_words:
appendFile = open('outputFile.txt','a')
appendFile.write(" "+r)
file=open("outputFile.txt","r+")
D={}
for word in file.read().split():
if word not in D:
D[word] = 1
else:
D[word] += 1
#print D
fig1=plt1.figure(figsize=(500/96, 400/96))
lists = sorted(D.iteritems(), key=lambda (k,v): (v,k), reverse=True) # sorted by key, return a list of tuples
#print lists
x, y = zip(*lists[:15]) # unpack a list of pairs into two tuples
plt1.title("Top Topics vs Count")
plt1.xlabel('Topics')
plt1.ylabel('Count')
plt1.bar(x, y,align='center',color='#ffd200')
k=sorted(D, key=D.get, reverse=True)
plt1.xticks(range(15), k[:15], fontsize=6)
locs, labels = plt1.xticks()
plt1.setp(labels, rotation=90)
return mpld3.fig_to_html(fig1)
The code plots the text from text file using mpld3(additional info for reference)
Plot Screenshot:
As you can see from the screenshot, the plot is running on flask
and this needs to be converted to an image for downloading.