{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ejemplo con library catalogs\n", "## Preparación y carga de datos\n", "En primer lugar se cargan las bibliotecas `pandas`, `ipywidgets` y la función propia `bar_scatter_plot` del archivo `plot_function.py`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas\n", "import ipywidgets as widgets\n", "from ipywidgets import interact, interactive, fixed\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline\n", "\n", "from plot_function import bar_scatter_plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "En siguiente lugar cargamos los datos, el fichero `data.csv` ubicado en el directorio `data`. Tras ello se previsualizan las primeras instancias." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datos = pandas.read_csv(\"data/datos.csv\")\n", "datos.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Por último, modifico los nombres de la columnas para evitar problemas con los espacios." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datos.columns = ['Worldcat_entity', 'Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations']\n", "datos.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualización de los datos\n", "En primer lugar hago una tabla interactiva con la que poder filtrar los datos de las distintas columnas." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@interact\n", "def show_entities_more_than(column=['Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations'], x=100):\n", " return datos.loc[datos[column] > x]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# pandas\n", "def plot_entities_more_than(Serie1=['Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations'],\n", " Serie2=['Publications', 'Works', 'Library_Holdings', 'Google_Scholar_Citations']):\n", " grafico_barras = datos.plot.bar(x='Worldcat_entity', y=[Serie1, Serie2],\n", " figsize=(20,10),\n", " rot=45)\n", " grafico_barras = grafico_barras.set_xticklabels(datos.Worldcat_entity,\n", " fontdict={'horizontalalignment': 'right', 'size':12})\n", " return grafico_barras\n", "\n", "w=interactive(plot_entities_more_than)\n", "w" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# matplotlib\n", "def plot_entities_more_than(Serie1=['Works', 'Publications', 'Library_Holdings', 'Google_Scholar_Citations'],\n", " Serie2=['Publications', 'Works', 'Library_Holdings', 'Google_Scholar_Citations']):\n", " \n", " labels = datos['Worldcat_entity']\n", " x = np.arange(len(labels))\n", " width = 0.25\n", " \n", " fig, ax = plt.subplots(figsize=(20,10), dpi= 100)\n", " rects1 = ax.bar(x - width/2, datos[Serie1], width, label=Serie1)\n", " rects2 = ax.bar(x + width/2, datos[Serie2], width, label=Serie2)\n", " \n", " ax.set_xticks(x)\n", " ax.set_xlabel('Worldcat_entity', fontdict={'size':14})\n", " ax.set_xticklabels(labels, rotation=45, fontdict={'horizontalalignment': 'right', 'size':12})\n", " ax.legend()\n", " \n", " fig.tight_layout()\n", " \n", " return plt.show()\n", "\n", "w=interactive(plot_entities_more_than)\n", "w" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Por último, ejecuto la función que he preparado para hacer el ranking de autores por library holdings." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bar_scatter_plot(datos)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }