-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenplot.py
More file actions
47 lines (43 loc) · 1.51 KB
/
Copy pathgenplot.py
File metadata and controls
47 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from flask import Flask, render_template
import pandas as pd
import numpy as np
import json
import plotly
import plotly.express as px
from wordcloud import WordCloud
import os
import base64
import io
def newLegend(fig, newNames):
newLabels = []
for item in newNames:
for i, elem in enumerate(fig.data[0].labels):
if elem == item:
#fig.data[0].labels[i] = newNames[item]
newLabels.append(newNames[item])
fig.data[0].labels = np.array(newLabels)
return(fig)
def barchart(kamusall):
df3 = pd.DataFrame(kamusall, columns=["term","df"])
short_df3 = df3.sort_values(by='df', ascending=False)
# print(short_df3.head(100))
fig = px.bar(short_df3.head(100), x='term', y='df')
barJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return barJSON
def piechart(dataset):
df2 = pd.DataFrame(dataset, columns=["label"])
grup = df2.groupby('label').size().reset_index(name='size')
# fig = px.pie(grup, values='size' , names='label')
fig = px.pie(grup, values='size' , names='label')
fig=newLegend(fig, {0:"0 - Negatif",
1:"1 - Positif"})
pieJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return pieJSON
def get_wordcloud(dataframe):
pil_img = WordCloud(width=700, height=300)
wordCloud=pil_img.generate_from_frequencies(dataframe).to_image()
img= io.BytesIO()
wordCloud.save(img,"PNG")
img.seek(0)
img_b64=base64.b64encode(img.getvalue()).decode()
return img_b64