#!/usr/bin/env python # coding: utf-8 # # Ejemplo con library catalogs # ## Preparación y carga de datos # En primer lugar se cargan las bibliotecas `pandas`, `ipywidgets` y la función propia `bar_scatter_plot` del archivo `plot_function.py`. # # In[ ]: import pandas import ipywidgets as widgets from ipywidgets import interact, interactive, fixed import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') from plot_function import bar_scatter_plot # En siguiente lugar cargamos los datos, el fichero `data.csv` ubicado en el directorio `data`. Tras ello se previsualizan las primeras instancias. # In[ ]: datos = pandas.read_csv("data/datos.csv") datos.head() # Por último, modifico los nombres de la columnas para evitar problemas con los espacios. # In[ ]: datos.columns = ['Worldcat_entity', 'Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations'] datos.head() # ## Visualización de los datos # En primer lugar hago una tabla interactiva con la que poder filtrar los datos de las distintas columnas. # In[ ]: @interact def show_entities_more_than(column=['Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations'], x=100): return datos.loc[datos[column] > x] # Tras ello un gráfico de barras interactivo en el que aparecen dos series de datos. En este caso el mismo pero de dos formas diferentes. # In[ ]: # pandas def plot_entities_more_than(Serie1=['Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations'], Serie2=['Publications', 'Works', 'Library_Holdings', 'Google_Scholar_Citations']): grafico_barras = datos.plot.bar(x='Worldcat_entity', y=[Serie1, Serie2], figsize=(20,10), rot=45) grafico_barras = grafico_barras.set_xticklabels(datos.Worldcat_entity, fontdict={'horizontalalignment': 'right', 'size':12}) return grafico_barras w=interactive(plot_entities_more_than) w # In[ ]: # matplotlib def plot_entities_more_than(Serie1=['Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations'], Serie2=['Publications', 'Works', 'Library_Holdings', 'Google_Scholar_Citations']): labels = datos['Worldcat_entity'] x = np.arange(len(labels)) width = 0.25 fig, ax = plt.subplots(figsize=(20,10), dpi= 100) rects1 = ax.bar(x - width/2, datos[Serie1], width, label=Serie1) rects2 = ax.bar(x + width/2, datos[Serie2], width, label=Serie2) ax.set_xticks(x) ax.set_xlabel('Worldcat_entity', fontdict={'size':14}) ax.set_xticklabels(labels, rotation=45, fontdict={'horizontalalignment': 'right', 'size':12}) ax.legend() fig.tight_layout() return plt.show() w=interactive(plot_entities_more_than) w # Por último, ejecuto la función que he preparado para hacer el ranking de autores por library holdings. # In[ ]: bar_scatter_plot(datos)