# Importamos pandas de la manera habitual import pandas as pd # Cargamos el archivo. Recordemos que pandas, y Python en general, es 0-indexed. data = pd.io.parsers.read_csv('brandshatch_lap27.csv', sep=';', header=13, index_col=0, skiprows=[14,15,16]) # Otra forma alternativa de leer los datos de un archivo CSV sería a través del # propio DataFrame de pandas, pero éste no nos permite saltarnos filas de datos: # data = pd.DataFrame.from_csv('brandshatch_lap27.csv', sep=';', header=13, index_col=0) data.head(5) import matplotlib.pyplot as plt import matplotlib as mpl %matplotlib inline # Color map tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120), (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150), (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148), (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199), (188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)] tableau10 = [tableau20[i*2] for i in range(10)] # Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts. for i in range(len(tableau20)): r, g, b = tableau20[i] tableau20[i] = (r / 255., g / 255., b / 255.) for i in range(len(tableau10)): r, g, b = tableau10[i] tableau10[i] = (r / 255., g / 255., b / 255.) # Set the default color cycle mpl.rcParams['axes.color_cycle'] = tableau10 fig, ax = plt.subplots(3, sharex=True, figsize=(10,6)) # Revoluciones del motor data['Engine RPM'].plot(ax=ax[0], linewidth=2) ax[0].set_xlabel('') ax[0].set_ylabel('Engine RPM') ax[0].legend(loc='lower right') # Velocidad del vehículo data['Ground Speed'].plot(ax=ax[1], linewidth=2) ax[1].set_xlabel('') ax[1].set_ylabel('Speed (kph)') ax[1].legend(loc='lower right') # Pedales data['Throttle Pos'].plot(ax=ax[2], linewidth=2) data['Brake Pedal Pos'].plot(ax=ax[2], linewidth=2) ax[2].set_xlabel('Time (s)') ax[2].set_ylabel('Position (%)') ax[2].set_ylim(-10,105) ax[2].legend(loc='center right') for j in range(3): ax[j].set_axisbelow(True) # Quitamos los bordes superior y derecho ax[j].spines["top"].set_visible(False) ax[j].spines["right"].set_visible(False) # Dejamos sólo los ticks abajo y a la izquierda ax[j].get_xaxis().tick_bottom() ax[j].get_yaxis().tick_left() fig1, ax1 = plt.subplots(figsize=(10,6)) for key, grp in data.groupby(['Gear']): grp.plot('Ground Speed','Engine RPM',kind='scatter', ax=ax1, label=key, color=tableau10[key]) ax1.set_xlabel('Ground Speed (kph)') ax1.set_axisbelow(True) ax1.legend(title='Gear', loc='upper left') # Quitamos los márgenes derecho y superior ax1.spines["top"].set_visible(False) ax1.spines["right"].set_visible(False) # Dejamos sólo los ticks abajo y a la izquierda ax1.get_xaxis().tick_bottom() ax1.get_yaxis().tick_left() fig2, ax2 = plt.subplots(figsize=(10,6)) data['Throttle Pos'].hist(ax=ax2) ax2.set_xlabel('Throttle position (%)') ax2.set_xlim(0,100) ax2.set_ylabel('Time spent (@ 60 Hz)') ax2.set_axisbelow(True) # Quitamos los márgenes derecho y superior ax2.spines["top"].set_visible(False) ax2.spines["right"].set_visible(False) # Dejamos sólo los ticks abajo y a la izquierda ax2.get_xaxis().tick_bottom() ax2.get_yaxis().tick_left() import plotly.plotly as py from plotly.graph_objs import * my_creds = py.get_credentials() username = my_creds['username'] api_key = my_creds['api_key'] py.sign_in(username, api_key) # Publicar una figura de matplotlib en la web unique_url = py.iplot_mpl(fig, filename='pybonacci/velocidades', strip_style=True) unique_url1 = py.iplot_mpl(fig1, filename='pybonacci/rpm', strip_style=True) unique_url2 = py.iplot_mpl(fig2, filename='pybonacci/acelerador', strip_style=True) # Herramientas Python/Plotly import plotly.tools as tls # Objetos para componer Leyendas from plotly.graph_objs import Legend py_fig = tls.mpl_to_plotly(fig, strip_style=True) # Borrar anotaciones py_fig['layout'].pop('annotations',None) # Añadir leyenda py_fig['layout'].update(dict(showlegend=True)) unique_url3 = py.iplot(py_fig, filename='pybonacci/velocidades_edit') py_fig1 = tls.mpl_to_plotly(fig1, strip_style=True) # Borrar anotaciones py_fig1['layout'].pop('annotations',None) # Añadir leyenda py_fig1['layout'].update(dict(showlegend=True, legend=Legend(x=0,y=1), hovermode='closest')) # Editar leyenda n=0 for key, grp in data.groupby(['Gear']): py_fig1['data'][n].update(dict(name='Gear {}'.format(key))) n += 1 unique_url4 = py.iplot(py_fig1, filename='pybonacci/rpm_edit') py_fig2 = tls.mpl_to_plotly(fig2, strip_style=True) # Borrar leyenda py_fig2['layout'].update(dict(showlegend=False, bargap=0.01)) unique_url5 = py.iplot(py_fig2, filename='pybonacci/acelerador_edit')