我创建了数据透视表,并尝试将其绘制在Flask中,如下所示:
def create_figure():
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
data = pd.read_excel('C:/Users/Ahmed Mustafa/FlaskProject/app/templates/sample.xlsx',0)
data.set_index(['TicketNumber'], inplace=True)
data.index.name=None
tickets = data.loc[data.Status=='Closed']
Pivotcountg = data.groupby("NewGroup")
Pivotcountg = Pivotcountg.agg({"NewStatus": "nunique"})
Pivotcountg = Pivotcountg.reset_index()
Pivotcount = data.groupby('NewGroup').count()
Pivotcountp = Pivotcountg.plot.bar()
但是当我尝试如下绘制时:
@main_blueprint.route('/plot.png')
def plot_png():
fig = create_figure()
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype='image/png')
def create_figure():
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
data = pd.read_excel('C:/Users/Ahmed Mustafa/FlaskProject/app/templates/sample.xlsx',0)
data.set_index(['TicketNumber'], inplace=True)
data.index.name=None
tickets = data.loc[data.Status=='Closed']
Pivotcountg = data.groupby("NewGroup")
Pivotcountg = Pivotcountg.agg({"NewStatus": "nunique"})
Pivotcountg = Pivotcountg.reset_index()
Pivotcount = data.groupby('NewGroup').count()
Pivotcountp = Pivotcountg.plot.bar()
axis.plot(Pivotcountp)
return fig
它不起作用!
我在我的html文件的行下面添加了
<img src="/plot.png" alt="my plot">
答案 0 :(得分:0)
我能够解决此问题;下面是.py文件中的修改
Sub TRY()
Columns("N:N").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Dim row1 As Long
With Worksheets("Sheet1")
row1 = .Range("N2").End(xlDown).Row
For i = 2 To row1
.Cells(i, "N").Value = Trim(.Cells(i, "O").Value)
Next i
End With
End Sub
此外,我能够通过在HTML页面中包含以下内容来显示结果图:
@main_blueprint.route('/plot.png')
def plot_png():
fig = create_figure()
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype='image/png')
def create_figure():
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
data = pd.read_excel('C:/Users/Ahmed
Mustafa/FlaskProject/app/templates/Repo/RAWDATA/sample.xlsx',0)
data.set_index(['TicketNumber'], inplace=False)
data.index.name=None
tickets = data.loc[data.Status=='Closed']
Pivotcountg = data.groupby("NewGroup")
Pivotcountg = Pivotcountg.agg({"NewStatus": "nunique"})
Pivotcountg = Pivotcountg.reset_index()
Pivotcount = data.groupby('NewGroup').count()
Pivotcountp = Pivotcountg.plot.bar()
xs = Pivotcountg.iloc[:, 0]
ys = Pivotcountg.iloc[:, 1]
xxx = np.arange(len(xs)) # the label locations
axis.set_title('Referrals between Groups')
axis.set_xlabel('Referred To Groups')
axis.set_ylabel('Number of Referrals')
axis.set_xticks(xxx)
axis.set_xticklabels(xs, rotation=45)
axis.tight_layout()
axis.legend()
axis.plot(xs, ys)
fig.tight_layout()
return fig