Я пытался показать результаты тепловых карт и карт питчей для футболистов, использующих seaborn и matplotlib. В автономном режиме я мог бы их визуализировать, но, похоже, он не работает в Python Dash. Проблема может заключаться в том, что, возможно, Python Dash не может отобразить matplotlib или seaborn .. Как мы можем показать график изменения температуры в Dash ??
Я пробовал разные вещи, такие как mpl_to_plotly (рис.), Но все равно не работал. Я получил сообщение об ошибке, в котором говорится, что не удается получить экстент окна без средства визуализации mpl_to_plotly ..
Вот код:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import json
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from pandas.io.json import json_normalize
import seaborn as sns
import pandas as pd
import urllib.request
with urllib.request.urlopen('https://raw.githubusercontent.com/statsbomb/open-data/master/data/events/7567.json') as data_file:
data = json.load(data_file)
event = json_normalize(data, sep = "_")
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
available_indicators = event['player_name'].unique()
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.H1('WC 2018 - Pitch Pass Heatmap'),
html.Div(''' South Korea vs Germany '''),
dcc.Dropdown(
id='my-dropdown',
options=[{'label': i, 'value': i} for i in available_indicators],
value='Toni Kroos'
),
# dcc.Store(
# id='example-graph'
# )
html.Div(id='example-graph')
])
@app.callback(
dash.dependencies.Output(component_id='example-graph', component_property='children'),
[dash.dependencies.Input(component_id='my-dropdown', component_property='value')]
)
# Let combine all 3: pitch, annotation and heatmap -- draw all of them on the pitch at the same time
def combine_heat_pitch_map(selected_dropdown_value, type_name = "Pass", data = event):
# let's filter data
type_data = data[(data['type_name'] == type_name) & (data['player_name']==selected_dropdown_value)]
movement_data = data[data['player_name']==selected_dropdown_value]
# pitch
fig=plt.figure() #set up the figures
fig.set_size_inches(7, 5)
ax=fig.add_subplot(1,1,1)
# draw_pitch(ax) #overlay our different objects on the pitch
# focus on only half of the pitch
#Pitch Outline & Centre Line
Pitch = plt.Rectangle([0,0], width = 120, height = 80, fill = False)
#Left, Right Penalty Area and midline
LeftPenalty = plt.Rectangle([0,22.3], width = 14.6, height = 35.3, fill = False)
RightPenalty = plt.Rectangle([105.4,22.3], width = 14.6, height = 35.3, fill = False)
midline = mpl.patches.ConnectionPatch([60,0], [60,80], "data", "data")
#Left, Right 6-yard Box
LeftSixYard = plt.Rectangle([0,32], width = 4.9, height = 16, fill = False)
RightSixYard = plt.Rectangle([115.1,32], width = 4.9, height = 16, fill = False)
#Prepare Circles
centreCircle = plt.Circle((60,40),8.1,color="black", fill = False)
centreSpot = plt.Circle((60,40),0.71,color="black")
#Penalty spots and Arcs around penalty boxes
leftPenSpot = plt.Circle((9.7,40),0.71,color="black")
rightPenSpot = plt.Circle((110.3,40),0.71,color="black")
leftArc = mpl.patches.Arc((9.7,40),height=16.2,width=16.2,angle=0,theta1=310,theta2=50,color="black")
rightArc = mpl.patches.Arc((110.3,40),height=16.2,width=16.2,angle=0,theta1=130,theta2=230,color="black")
element = [Pitch, LeftPenalty, RightPenalty, midline, LeftSixYard, RightSixYard, centreCircle,
centreSpot, rightPenSpot, leftPenSpot, leftArc, rightArc]
for i in element:
ax.add_patch(i)
plt.axis('off')
# annotation of type_name
for i in range(len(type_data)):
# annotate draw an arrow from a current position to pass_end_location
color = "blue" if type_data.iloc[i]['period'] == 1 else "red" # If passes are from halftime, color blue, otherwise red
# label = "1st half" if type_data.iloc[i]['period'] == 1 else "2nd half"
ax.annotate("", xy = (type_data.iloc[i]['pass_end_location'][0], type_data.iloc[i]['pass_end_location'][1]), xycoords = 'data',
xytext = (type_data.iloc[i]['location'][0], type_data.iloc[i]['location'][1]), textcoords = 'data',
arrowprops=dict(arrowstyle="->",connectionstyle="arc3", color = color,),)
#ax.legend()
# heatmap of entire movement
x_coord = [i[0] for i in movement_data["location"]]
y_coord = [i[1] for i in movement_data["location"]]
#shades: give us the heat map we desire
# n_levels: draw more lines, the larger n, the more blurry it looks
sns.kdeplot(x_coord, y_coord, shade = "True", color = "green", n_levels = 30)
# Draw all of them on the pitch
plt.ylim(-2, 82)
plt.xlim(-2, 122)
# plt.legend(loc='upper right')
#figure = plt.show()
plt.show()
#return figure
if __name__ == '__main__':
app.run_server()